|  | 
|  | 1 | +import {triggerEditorContentChanged} from './Paste.js'; | 
|  | 2 | + | 
|  | 3 | +function handleIndentSelection(textarea, e) { | 
|  | 4 | +  const selStart = textarea.selectionStart; | 
|  | 5 | +  const selEnd = textarea.selectionEnd; | 
|  | 6 | +  if (selEnd === selStart) return; // do not process when no selection | 
|  | 7 | + | 
|  | 8 | +  e.preventDefault(); | 
|  | 9 | +  const lines = textarea.value.split('\n'); | 
|  | 10 | +  const selectedLines = []; | 
|  | 11 | + | 
|  | 12 | +  let pos = 0; | 
|  | 13 | +  for (let i = 0; i < lines.length; i++) { | 
|  | 14 | +    if (pos > selEnd) break; | 
|  | 15 | +    if (pos >= selStart) selectedLines.push(i); | 
|  | 16 | +    pos += lines[i].length + 1; | 
|  | 17 | +  } | 
|  | 18 | + | 
|  | 19 | +  for (const i of selectedLines) { | 
|  | 20 | +    if (e.shiftKey) { | 
|  | 21 | +      lines[i] = lines[i].replace(/^(\t| {1,2})/, ''); | 
|  | 22 | +    } else { | 
|  | 23 | +      lines[i] = `  ${lines[i]}`; | 
|  | 24 | +    } | 
|  | 25 | +  } | 
|  | 26 | + | 
|  | 27 | +  // re-calculating the selection range | 
|  | 28 | +  let newSelStart, newSelEnd; | 
|  | 29 | +  pos = 0; | 
|  | 30 | +  for (let i = 0; i < lines.length; i++) { | 
|  | 31 | +    if (i === selectedLines[0]) { | 
|  | 32 | +      newSelStart = pos; | 
|  | 33 | +    } | 
|  | 34 | +    if (i === selectedLines[selectedLines.length - 1]) { | 
|  | 35 | +      newSelEnd = pos + lines[i].length; | 
|  | 36 | +      break; | 
|  | 37 | +    } | 
|  | 38 | +    pos += lines[i].length + 1; | 
|  | 39 | +  } | 
|  | 40 | +  textarea.value = lines.join('\n'); | 
|  | 41 | +  textarea.setSelectionRange(newSelStart, newSelEnd); | 
|  | 42 | +  triggerEditorContentChanged(textarea); | 
|  | 43 | +} | 
|  | 44 | + | 
|  | 45 | +function handleNewline(textarea, e) { | 
|  | 46 | +  const selStart = textarea.selectionStart; | 
|  | 47 | +  const selEnd = textarea.selectionEnd; | 
|  | 48 | +  if (selEnd !== selStart) return; // do not process when there is a selection | 
|  | 49 | + | 
|  | 50 | +  const value = textarea.value; | 
|  | 51 | + | 
|  | 52 | +  // find the current line | 
|  | 53 | +  // * if selStart is 0, lastIndexOf(..., -1) is the same as lastIndexOf(..., 0) | 
|  | 54 | +  // * if lastIndexOf reruns -1, lineStart is 0 and it is still correct. | 
|  | 55 | +  const lineStart = value.lastIndexOf('\n', selStart - 1) + 1; | 
|  | 56 | +  let lineEnd = value.indexOf('\n', selStart); | 
|  | 57 | +  lineEnd = lineEnd < 0 ? value.length : lineEnd; | 
|  | 58 | +  let line = value.slice(lineStart, lineEnd); | 
|  | 59 | +  if (!line) return; // if the line is empty, do nothing, let the browser handle it | 
|  | 60 | + | 
|  | 61 | +  // parse the indention | 
|  | 62 | +  const indention = /^\s*/.exec(line)[0]; | 
|  | 63 | +  line = line.slice(indention.length); | 
|  | 64 | + | 
|  | 65 | +  // parse the prefixes: "1. ", "- ", "* ", "[ ] ", "[x] " | 
|  | 66 | +  // there must be a space after the prefix because none of "1.foo" / "-foo" is a list item | 
|  | 67 | +  const prefixMatch = /^([0-9]+\.|[-*]|\[ \]|\[x\])\s/.exec(line); | 
|  | 68 | +  let prefix = ''; | 
|  | 69 | +  if (prefixMatch) { | 
|  | 70 | +    prefix = prefixMatch[0]; | 
|  | 71 | +    if (lineStart + prefix.length > selStart) prefix = ''; // do not add new line if cursor is at prefix | 
|  | 72 | +  } | 
|  | 73 | + | 
|  | 74 | +  line = line.slice(prefix.length); | 
|  | 75 | +  if (!indention && !prefix) return; // if no indention and no prefix, do nothing, let the browser handle it | 
|  | 76 | + | 
|  | 77 | +  e.preventDefault(); | 
|  | 78 | +  if (!line) { | 
|  | 79 | +    // clear current line if we only have i.e. '1. ' and the user presses enter again to finish creating a list | 
|  | 80 | +    textarea.value = value.slice(0, lineStart) + value.slice(lineEnd); | 
|  | 81 | +  } else { | 
|  | 82 | +    // start a new line with the same indention and prefix | 
|  | 83 | +    let newPrefix = prefix; | 
|  | 84 | +    if (newPrefix === '[x]') newPrefix = '[ ]'; | 
|  | 85 | +    if (/^\d+\./.test(newPrefix)) newPrefix = `1. `; // a simple approach, otherwise it needs to parse the lines after the current line | 
|  | 86 | +    const newLine = `\n${indention}${newPrefix}`; | 
|  | 87 | +    textarea.value = value.slice(0, selStart) + newLine + value.slice(selEnd); | 
|  | 88 | +    textarea.setSelectionRange(selStart + newLine.length, selStart + newLine.length); | 
|  | 89 | +  } | 
|  | 90 | +  triggerEditorContentChanged(textarea); | 
|  | 91 | +} | 
|  | 92 | + | 
|  | 93 | +export function initTextareaMarkdown(textarea) { | 
|  | 94 | +  textarea.addEventListener('keydown', (e) => { | 
|  | 95 | +    if (e.key === 'Tab' && !e.ctrlKey && !e.metaKey && !e.altKey) { | 
|  | 96 | +      // use Tab/Shift-Tab to indent/unindent the selected lines | 
|  | 97 | +      handleIndentSelection(textarea, e); | 
|  | 98 | +    } else if (e.key === 'Enter' && !e.shiftKey && !e.ctrlKey && !e.metaKey && !e.altKey) { | 
|  | 99 | +      // use Enter to insert a new line with the same indention and prefix | 
|  | 100 | +      handleNewline(textarea, e); | 
|  | 101 | +    } | 
|  | 102 | +  }); | 
|  | 103 | +} | 
0 commit comments