Skip to content

Commit

Permalink
Merge pull request hackmdio#1233 from hedgedoc/fix/insertOnStartOfLines
Browse files Browse the repository at this point in the history
Fix insertOnStartOfLines behaviour
  • Loading branch information
davidmehren authored May 6, 2021
2 parents ad7fade + e4b2b6f commit dc1f621
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions public/js/lib/editor/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const wrapSymbols = ['*', '_', '~', '^', '+', '=']

export function wrapTextWith (editor, cm, symbol) {
if (!cm.getSelection()) {
return CodeMirror.Pass
Expand Down Expand Up @@ -106,12 +107,14 @@ export function insertOnStartOfLines (cm, symbol) {
for (let i = 0; i < ranges.length; i++) {
const range = ranges[i]
if (!range.empty()) {
const from = range.from()
const to = range.to()
let selection = cm.getRange({ line: from.line, ch: 0 }, to)
const cursorFrom = range.from()
const cursorTo = range.to()
const firstLineStart = { line: cursorFrom.line, ch: 0 }
const lastLineEnd = { line: cursorTo.line, ch: cm.getLine(cursorTo.line).length }
let selection = cm.getRange(firstLineStart, lastLineEnd)
selection = selection.replace(/\n/g, '\n' + symbol)
selection = symbol + selection
cm.replaceRange(selection, from, to)
cm.replaceRange(selection, firstLineStart, lastLineEnd)
} else {
cm.replaceRange(symbol, { line: cursor.line, ch: 0 }, { line: cursor.line, ch: 0 })
}
Expand Down

0 comments on commit dc1f621

Please sign in to comment.