From 953dcbfc453cc773b4077dd4f6fdab20a9ddbeb6 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Tue, 8 Oct 2024 11:32:55 +0800 Subject: feat: also sync scroll when content changes --- src/editor.mjs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src') 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(() => { attributeFilter: ['data-scroll-line'] }) -cm.on('scroll', () => { +const setScrollLine = () => { if (dumbyContainer.dataset.scrollLine) return - const scrollInfo = cm.getScrollInfo() - const lineNumber = cm.lineAtHeight(scrollInfo.top, 'local') + const lineNumber = cm.getCursor()?.line + ?? cm.lineAtHeight(cm.getScrollInfo().top, 'local') textArea.dataset.scrollLine = lineNumber -}) +} +cm.on('scroll', setScrollLine) // Sync HTML Contents with CodeMirror LineNumber new window.MutationObserver(() => { @@ -317,6 +318,7 @@ new window.MutationObserver(() => { p = paragraphs.find(p => Number(p.dataset.sourceLine) === lineNumber) lineNumber++ } while (!p && lineNumber < cm.doc.size) + p = p ?? paragraphs.at(-1) if (!p) return const coords = cm.charCoords({ line: lineNumber, ch: 0 }) @@ -475,6 +477,8 @@ const updateDumbyMap = () => { htmlHolder.onscroll = htmlOnScroll(htmlHolder) // Set oncontextmenu callback dumbymap.utils.setContextMenu(menuForEditor) + // Scroll to proper position + setScrollLine() } updateDumbyMap() -- cgit v1.2.3-70-g09d2