diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2024-10-06 15:13:17 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2024-10-06 19:52:04 +0800 |
| commit | 4b5378a3cbd7350a530bbac4c916f781b809f888 (patch) | |
| tree | 1b824e48d12e0738ec92235c18325ec44fb1dc93 | |
| parent | f783cdba20b2d83bbf3b0aba8ff78d12988887cd (diff) | |
fix: patch 849a9ed
fix logic about syncing by offset
| -rw-r--r-- | src/editor.mjs | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/src/editor.mjs b/src/editor.mjs index dfafaa9..ca8e6b0 100644 --- a/src/editor.mjs +++ b/src/editor.mjs | |||
| @@ -193,18 +193,19 @@ const htmlOnScroll = (ele) => () => { | |||
| 193 | if (!linenumber) return | 193 | if (!linenumber) return |
| 194 | const offset = (line.offsetTop + block.offsetTop - ele.scrollTop) | 194 | const offset = (line.offsetTop + block.offsetTop - ele.scrollTop) |
| 195 | 195 | ||
| 196 | clearTimeout(dumbyContainer.timer) | ||
| 197 | if (linenumber) { | 196 | if (linenumber) { |
| 198 | dumbyContainer.dataset.scrollLine = linenumber + '/' + offset | 197 | dumbyContainer.dataset.scrollLine = linenumber + '/' + offset |
| 199 | dumbyContainer.timer = setTimeout( | ||
| 200 | () => delete dumbyContainer.dataset.scrollLine, | ||
| 201 | 50 | ||
| 202 | ) | ||
| 203 | } | 198 | } |
| 204 | } | 199 | } |
| 205 | 200 | ||
| 206 | // Sync CodeMirror LineNumber with HTML Contents | 201 | // Sync CodeMirror LineNumber with HTML Contents |
| 207 | new window.MutationObserver(() => { | 202 | new window.MutationObserver(() => { |
| 203 | clearTimeout(dumbyContainer.timer) | ||
| 204 | dumbyContainer.timer = setTimeout( | ||
| 205 | () => delete dumbyContainer.dataset.scrollLine, | ||
| 206 | 50 | ||
| 207 | ) | ||
| 208 | |||
| 208 | const line = dumbyContainer.dataset.scrollLine | 209 | const line = dumbyContainer.dataset.scrollLine |
| 209 | if (line) { | 210 | if (line) { |
| 210 | const [lineNumber, offset] = line.split('/') | 211 | const [lineNumber, offset] = line.split('/') |
| @@ -224,16 +225,16 @@ cm.on('scroll', () => { | |||
| 224 | const scrollInfo = cm.getScrollInfo() | 225 | const scrollInfo = cm.getScrollInfo() |
| 225 | const lineNumber = cm.lineAtHeight(scrollInfo.top, 'local') | 226 | const lineNumber = cm.lineAtHeight(scrollInfo.top, 'local') |
| 226 | textArea.dataset.scrollLine = lineNumber | 227 | textArea.dataset.scrollLine = lineNumber |
| 228 | }) | ||
| 227 | 229 | ||
| 230 | // Sync HTML Contents with CodeMirror LineNumber | ||
| 231 | new window.MutationObserver(() => { | ||
| 228 | clearTimeout(textArea.timer) | 232 | clearTimeout(textArea.timer) |
| 229 | textArea.timer = setTimeout( | 233 | textArea.timer = setTimeout( |
| 230 | () => delete textArea.dataset.scrollLine, | 234 | () => delete textArea.dataset.scrollLine, |
| 231 | 1000 | 235 | 1000 |
| 232 | ) | 236 | ) |
| 233 | }) | ||
| 234 | 237 | ||
| 235 | // Sync HTML Contents with CodeMirror LineNumber | ||
| 236 | new window.MutationObserver(() => { | ||
| 237 | const line = textArea.dataset.scrollLine | 238 | const line = textArea.dataset.scrollLine |
| 238 | let lineNumber = Number(line) | 239 | let lineNumber = Number(line) |
| 239 | let p | 240 | let p |
| @@ -246,9 +247,10 @@ new window.MutationObserver(() => { | |||
| 246 | } while (!p && lineNumber < cm.doc.size) | 247 | } while (!p && lineNumber < cm.doc.size) |
| 247 | if (!p) return | 248 | if (!p) return |
| 248 | 249 | ||
| 249 | const coords = cm.charCoords({ line: lineNumber, ch: 0 }, 'window') | 250 | const coords = cm.charCoords({ line: lineNumber, ch: 0 }) |
| 250 | p.scrollIntoView() | 251 | p.scrollIntoView() |
| 251 | dumbymap.htmlHolder.scrollBy(0, -coords.top + 30) | 252 | const top = p.getBoundingClientRect().top |
| 253 | dumbymap.htmlHolder.scrollBy(0, top - coords.top + 30) | ||
| 252 | }).observe(textArea, { | 254 | }).observe(textArea, { |
| 253 | attributes: true, | 255 | attributes: true, |
| 254 | attributeFilter: ['data-scroll-line'] | 256 | attributeFilter: ['data-scroll-line'] |
| @@ -724,6 +726,7 @@ const addSuggestions = (anchor, suggestions) => { | |||
| 724 | cm.on('cursorActivity', _ => { | 726 | cm.on('cursorActivity', _ => { |
| 725 | menu.style.display = 'none' | 727 | menu.style.display = 'none' |
| 726 | const anchor = cm.getCursor() | 728 | const anchor = cm.getCursor() |
| 729 | textArea.dataset.scrollLine = anchor.line | ||
| 727 | 730 | ||
| 728 | if (insideCodeblockForMap(anchor)) { | 731 | if (insideCodeblockForMap(anchor)) { |
| 729 | handleTypingInCodeBlock(anchor) | 732 | handleTypingInCodeBlock(anchor) |