From 5d9b934a2d984ca84cc5dbb6e1d2f95a91440cae Mon Sep 17 00:00:00 2001 From: thzxx <1440196015@qq.com> Date: Fri, 24 Jul 2026 11:20:47 +0800 Subject: [PATCH] fix(core): fix gutter scroll desync with textarea content - Match gutter font-size to textarea (13.5px) so line heights align - Always sync gutter scrollTop regardless of mode - Sync gutter scroll position after undo/redo history apply --- src/core.js | 12 +++++++++--- src/styles.js | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/core.js b/src/core.js index dbc5198..b79423e 100644 --- a/src/core.js +++ b/src/core.js @@ -351,11 +351,15 @@ class MarkdownEditor { ta.addEventListener('keyup', onCursorActivity); const onScroll = () => { - // 同步行号滚动 - if (this.gutter) this.gutter.scrollTop = ta.scrollTop; + // 同步行号滚动(始终同步,不限于分屏模式) + if (this.gutter) { + this.gutter.scrollTop = ta.scrollTop; + } + // 同步预览滚动(仅分屏模式) if (!this.config.syncScroll || this._mode !== 'split') return; const max = ta.scrollHeight - ta.clientHeight; - const ratio = max > 0 ? ta.scrollTop / max : 0; + if (max <= 0) return; + const ratio = ta.scrollTop / max; const pmax = this.previewPane.scrollHeight - this.previewPane.clientHeight; this.previewPane.scrollTop = ratio * pmax; }; @@ -870,6 +874,8 @@ class MarkdownEditor { this._render(); this._renderGutter(); this._updateWordCount(); + // 同步行号滚动位置 + if (this.gutter) this.gutter.scrollTop = this.textarea.scrollTop; this._emit('change', this._value); if (typeof this.config.onChange === 'function') { try { this.config.onChange(this._value, this); } catch (e) { console.error(e); } diff --git a/src/styles.js b/src/styles.js index 9c3eac1..bba7b6c 100644 --- a/src/styles.js +++ b/src/styles.js @@ -140,7 +140,7 @@ const generateCSS = () => { border-right: 1px solid var(--md-border); color: var(--md-muted); font-family: var(--md-mono); - font-size: 12px; + font-size: 13.5px; line-height: 1.7; text-align: right; user-select: none;