Skip to content

Commit

Permalink
enable link insertion shortcut (#241)
Browse files Browse the repository at this point in the history
- enabled insert link using mod+alt+k
  • Loading branch information
wowthedoge authored Sep 24, 2024
1 parent 6204792 commit 913a941
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/elm/Outgoing.elm
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Msg
| SelectAll String
| FlashPrice
| TextSurround String String
| InsertMarkdownLink String
| SetField String String
| SetCursorPosition Int
| SetFullscreen Bool
Expand Down Expand Up @@ -206,6 +207,9 @@ send info =

TextSurround id str ->
dataToSend "TextSurround" (list string [ id, str ])

InsertMarkdownLink id ->
dataToSend "InsertMarkdownLink" (string id)

SetField id str ->
dataToSend "SetField" (list string [ id, str ])
Expand Down
16 changes: 15 additions & 1 deletion src/elm/Page/Doc.elm
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,20 @@ incoming incomingMsg model =
|> saveCardIfEditing
|> insertAbove activeId beforeText

"mod+alt+k" ->
case vs.viewMode of
Normal _ ->
( model
, Cmd.none
, []
)

_ ->
( model
, send (InsertMarkdownLink activeId)
, []
)

"mod+up" ->
normalMode model (insertAbove activeId "")

Expand Down Expand Up @@ -782,7 +796,7 @@ incoming incomingMsg model =
"mod+shift+down" ->
normalMode model (mergeDown activeId)

"mod+shift+k" ->
"mod+shift+k" ->
normalMode model (mergeUp activeId)

"mod+shift+up" ->
Expand Down
58 changes: 55 additions & 3 deletions src/shared/doc-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ function toHex(s) {
return h;
}

function isValidURL(text) {
try {
new URL(text);
return true;
} catch {
return false;
}
}

/* ===== DOM Manipulation ===== */
let toElm;
const CARD_DATA = Symbol.for("cardbased");
Expand Down Expand Up @@ -439,6 +448,7 @@ var shortcuts = [
"mod+shift+down",
"mod+shift+k",
"mod+shift+up",
"mod+alt+k",
"h",
"j",
"k",
Expand Down Expand Up @@ -581,16 +591,58 @@ var casesShared = (elmData, params) => {
} else {
const start = tarea.selectionStart
const end = tarea.selectionEnd
let cursorPos;
let newValue;
if (start !== end) {
const text = tarea.value.slice(start, end)
const modifiedText = surroundString + text + surroundString
const newValue = tarea.value.substring(0, start) + modifiedText + tarea.value.substring(end)
newValue = tarea.value.substring(0, start) + modifiedText + tarea.value.substring(end)
cursorPos = start + modifiedText.length
} else {
newValue = tarea.value.substring(0, start) + surroundString + surroundString + tarea.value.substring(end)
cursorPos = start + surroundString.length
}
tarea.value = newValue
tarea.setSelectionRange(cursorPos, cursorPos)
params.DIRTY = true
toElm(newValue, 'docMsgs', 'FieldChanged')

if (card !== null) {
card.dataset.clonedContent = newValue
}
}
},

InsertMarkdownLink: () => {
const id = elmData
const tarea = document.getElementById('card-edit-' + id)
const card = document.getElementById('card-' + id)

if (tarea === null) {
console.log('Textarea not found for InsertMarkdownLink command.')
} else {
const start = tarea.selectionStart
const end = tarea.selectionEnd
const text = tarea.value.slice(start, end)

let modifiedText;
let cursorPos;
if (start !== end) {
if (isValidURL(text)) {
modifiedText = "[](" + text + ")"
newValue = tarea.value.substring(0, start) + modifiedText + tarea.value.substring(end)
cursorPos = start + 1
} else {
modifiedText = "[" + text + "]()"
newValue = tarea.value.substring(0, start) + modifiedText + tarea.value.substring(end)
cursorPos = start + modifiedText.length - 1
}

tarea.value = newValue
const cursorPos = start + modifiedText.length
tarea.setSelectionRange(cursorPos, cursorPos)
params.DIRTY = true
toElm(newValue, 'docMsgs', 'FieldChanged')

if (card !== null) {
card.dataset.clonedContent = newValue
}
Expand Down

0 comments on commit 913a941

Please sign in to comment.