summaryrefslogtreecommitdiff
path: root/public/js/lib/editor
diff options
context:
space:
mode:
authorDavid Mehren2021-05-05 22:57:49 +0200
committerDavid Mehren2021-05-05 22:57:49 +0200
commite4b2b6ff73b0f2132c93f242ddf6143cc03a9619 (patch)
tree68dd7d365f044783b858a8abf56642ceb8bf551b /public/js/lib/editor
parentf48e36d2052bcae56081bee3c281bfc69fe7f70d (diff)
Fix insertOnStartOfLines behaviour
A bug in insertOnStartOfLines lead to duplicated text, if the cursor was not at the start of a line. This fixes the behaviour of insertOnStartOfLines to always use the complete first and last line of the selection, even if they were only partially selected. Fixes #1231 Signed-off-by: David Mehren <git@herrmehren.de>
Diffstat (limited to '')
-rw-r--r--public/js/lib/editor/utils.js11
1 files changed, 7 insertions, 4 deletions
diff --git a/public/js/lib/editor/utils.js b/public/js/lib/editor/utils.js
index 70428b28..3d799267 100644
--- a/public/js/lib/editor/utils.js
+++ b/public/js/lib/editor/utils.js
@@ -1,4 +1,5 @@
const wrapSymbols = ['*', '_', '~', '^', '+', '=']
+
export function wrapTextWith (editor, cm, symbol) {
if (!cm.getSelection()) {
return CodeMirror.Pass
@@ -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 })
}