diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2024-10-08 11:32:55 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2024-10-08 12:04:29 +0800 |
| commit | 953dcbfc453cc773b4077dd4f6fdab20a9ddbeb6 (patch) | |
| tree | 1a825f23b5853b4b4c8e1a3ad80abdd2f51f4185 /src | |
| parent | 754f4cb4eed6358a83b22b7700d363e2c1e417c4 (diff) | |
feat: also sync scroll when content changes
Diffstat (limited to 'src')
| -rw-r--r-- | src/editor.mjs | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/src/editor.mjs b/src/editor.mjs index 29574d7..81dd5a8 100644 --- a/src/editor.mjs +++ b/src/editor.mjs | |||
| @@ -291,13 +291,14 @@ new window.MutationObserver(() => { | |||
| 291 | attributeFilter: ['data-scroll-line'] | 291 | attributeFilter: ['data-scroll-line'] |
| 292 | }) | 292 | }) |
| 293 | 293 | ||
| 294 | cm.on('scroll', () => { | 294 | const setScrollLine = () => { |
| 295 | if (dumbyContainer.dataset.scrollLine) return | 295 | if (dumbyContainer.dataset.scrollLine) return |
| 296 | 296 | ||
| 297 | const scrollInfo = cm.getScrollInfo() | 297 | const lineNumber = cm.getCursor()?.line |
| 298 | const lineNumber = cm.lineAtHeight(scrollInfo.top, 'local') | 298 | ?? cm.lineAtHeight(cm.getScrollInfo().top, 'local') |
| 299 | textArea.dataset.scrollLine = lineNumber | 299 | textArea.dataset.scrollLine = lineNumber |
| 300 | }) | 300 | } |
| 301 | cm.on('scroll', setScrollLine) | ||
| 301 | 302 | ||
| 302 | // Sync HTML Contents with CodeMirror LineNumber | 303 | // Sync HTML Contents with CodeMirror LineNumber |
| 303 | new window.MutationObserver(() => { | 304 | new window.MutationObserver(() => { |
| @@ -317,6 +318,7 @@ new window.MutationObserver(() => { | |||
| 317 | p = paragraphs.find(p => Number(p.dataset.sourceLine) === lineNumber) | 318 | p = paragraphs.find(p => Number(p.dataset.sourceLine) === lineNumber) |
| 318 | lineNumber++ | 319 | lineNumber++ |
| 319 | } while (!p && lineNumber < cm.doc.size) | 320 | } while (!p && lineNumber < cm.doc.size) |
| 321 | p = p ?? paragraphs.at(-1) | ||
| 320 | if (!p) return | 322 | if (!p) return |
| 321 | 323 | ||
| 322 | const coords = cm.charCoords({ line: lineNumber, ch: 0 }) | 324 | const coords = cm.charCoords({ line: lineNumber, ch: 0 }) |
| @@ -475,6 +477,8 @@ const updateDumbyMap = () => { | |||
| 475 | htmlHolder.onscroll = htmlOnScroll(htmlHolder) | 477 | htmlHolder.onscroll = htmlOnScroll(htmlHolder) |
| 476 | // Set oncontextmenu callback | 478 | // Set oncontextmenu callback |
| 477 | dumbymap.utils.setContextMenu(menuForEditor) | 479 | dumbymap.utils.setContextMenu(menuForEditor) |
| 480 | // Scroll to proper position | ||
| 481 | setScrollLine() | ||
| 478 | } | 482 | } |
| 479 | updateDumbyMap() | 483 | updateDumbyMap() |
| 480 | 484 | ||