diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2024-10-12 21:04:07 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2024-10-13 11:06:46 +0800 |
| commit | 124353ca30b36e69b94e50e6171ff61d7c20709e (patch) | |
| tree | 36c4bc6d5c0b6c0423406ff3af2a52a015a92561 | |
| parent | 2694a56eb896ec4564630e36647d0b53a2278591 (diff) | |
fix: sync scroll from HTML to CodeMirror
* fix initial for lineEnd
* sync horizontal scroll
| -rw-r--r-- | src/editor.mjs | 15 |
1 files changed, 8 insertions, 7 deletions
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', () => { | |||
| 1031 | const content = selection.getRangeAt(0).toString() | 1031 | const content = selection.getRangeAt(0).toString() |
| 1032 | const parentWithSourceLine = selection.anchorNode.parentElement.closest('.source-line') | 1032 | const parentWithSourceLine = selection.anchorNode.parentElement.closest('.source-line') |
| 1033 | const lineStart = Number(parentWithSourceLine?.dataset?.sourceLine ?? NaN) | 1033 | const lineStart = Number(parentWithSourceLine?.dataset?.sourceLine ?? NaN) |
| 1034 | const lineEnd = Number(parentWithSourceLine?.nextSibling?.dataset?.sourceLine ?? NaN) | 1034 | const nextSourceLine = parentWithSourceLine?.nextSibling?.dataset?.sourceLine |
| 1035 | const lineEnd = Number(nextSourceLine) ?? cm.doc.size | ||
| 1035 | // TODO Also return when range contains anchor element | 1036 | // TODO Also return when range contains anchor element |
| 1036 | if (content.includes('\n') || isNaN(lineStart)) { | 1037 | if (content.includes('\n') || isNaN(lineStart)) { |
| 1037 | cm.setSelection(cm.getCursor()) | 1038 | cm.setSelection(cm.getCursor()) |
| @@ -1056,16 +1057,16 @@ document.addEventListener('selectionchange', () => { | |||
| 1056 | while (index === -1) { | 1057 | while (index === -1) { |
| 1057 | anchor.line += 1 | 1058 | anchor.line += 1 |
| 1058 | anchor.ch = 0 | 1059 | anchor.ch = 0 |
| 1059 | if (anchor.line >= lineEnd) { | 1060 | if (anchor.line >= lineEnd) return |
| 1060 | cm.setSelection(cm.setCursor()) | 1061 | |
| 1061 | return | 1062 | index = cm.getLine(anchor.line).indexOf(text) |
| 1062 | } | ||
| 1063 | index = cm.getLine(anchor.line)?.indexOf(text) | ||
| 1064 | } | 1063 | } |
| 1065 | anchor.ch = index + text.length | 1064 | anchor.ch = index + text.length |
| 1066 | }) | 1065 | }) |
| 1067 | 1066 | ||
| 1068 | cm.setSelection({ line: anchor.line, ch: anchor.ch - content.length }, anchor) | 1067 | const focus = { line: anchor.line, ch: anchor.ch - content.length } |
| 1068 | cm.setSelection(focus, anchor) | ||
| 1069 | cm.scrollIntoView(focus) | ||
| 1069 | } | 1070 | } |
| 1070 | }) | 1071 | }) |
| 1071 | 1072 | ||