From 124353ca30b36e69b94e50e6171ff61d7c20709e Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Sat, 12 Oct 2024 21:04:07 +0800 Subject: fix: sync scroll from HTML to CodeMirror * fix initial for lineEnd * sync horizontal scroll --- src/editor.mjs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src/editor.mjs') diff --git a/src/editor.mjs b/src/editor.mjs index 5a51fbe..c3c51ef 100644 --- a/src/editor.mjs +++ b/src/editor.mjs @@ -1031,7 +1031,8 @@ document.addEventListener('selectionchange', () => { const content = selection.getRangeAt(0).toString() const parentWithSourceLine = selection.anchorNode.parentElement.closest('.source-line') const lineStart = Number(parentWithSourceLine?.dataset?.sourceLine ?? NaN) - const lineEnd = Number(parentWithSourceLine?.nextSibling?.dataset?.sourceLine ?? NaN) + const nextSourceLine = parentWithSourceLine?.nextSibling?.dataset?.sourceLine + const lineEnd = Number(nextSourceLine) ?? cm.doc.size // TODO Also return when range contains anchor element if (content.includes('\n') || isNaN(lineStart)) { cm.setSelection(cm.getCursor()) @@ -1056,16 +1057,16 @@ document.addEventListener('selectionchange', () => { while (index === -1) { anchor.line += 1 anchor.ch = 0 - if (anchor.line >= lineEnd) { - cm.setSelection(cm.setCursor()) - return - } - index = cm.getLine(anchor.line)?.indexOf(text) + if (anchor.line >= lineEnd) return + + index = cm.getLine(anchor.line).indexOf(text) } anchor.ch = index + text.length }) - cm.setSelection({ line: anchor.line, ch: anchor.ch - content.length }, anchor) + const focus = { line: anchor.line, ch: anchor.ch - content.length } + cm.setSelection(focus, anchor) + cm.scrollIntoView(focus) } }) -- cgit v1.2.3-70-g09d2