From 8f578599cfa03608fc94010b92db8b4d49debf6a Mon Sep 17 00:00:00 2001 From: thzxx <1440196015@qq.com> Date: Sat, 25 Jul 2026 14:58:57 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=B5=AE=E5=8A=A8?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E6=A0=8F=20selectionChange=20=E8=BD=AE?= =?UTF-8?q?=E8=AF=A2=E9=97=B4=E9=9A=94=20200ms=E2=86=92500ms=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B5=8B=E8=AF=95=E7=BC=93=E6=85=A2=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core.ts | 38 +++++++++++++++++++------------------- tests/core.test.ts | 2 +- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/core.ts b/src/core.ts index 443955d..90dea25 100644 --- a/src/core.ts +++ b/src/core.ts @@ -747,33 +747,33 @@ export class MarkdownEditor { const hide = () => { this._hideFloatingToolbar(); }; - // Detect selection changes - ta.addEventListener('mouseup', () => setTimeout(show, 0)); - ta.addEventListener('keyup', () => { - if (ta.selectionStart !== ta.selectionEnd) setTimeout(show, 0); - else setTimeout(hide, 0); - // Emit cursorMove - const pos = this.getCursorPosition(); - this._emit('cursorMove', pos); - }); - ta.addEventListener('blur', () => setTimeout(hide, 300)); - ta.addEventListener('click', () => setTimeout(() => { - if (ta.selectionStart === ta.selectionEnd) hide(); - }, 0)); - - // Emit selectionChange on selection changes + // Detect selection changes via events + throttled polling let lastSelStart = ta.selectionStart; let lastSelEnd = ta.selectionEnd; - this._selectionTimer = setInterval(() => { - if (this._destroyed) return; + const check = () => { + if (this._destroyed || !document.body.contains(ta)) return; const s = ta.selectionStart; const e = ta.selectionEnd; if (s !== lastSelStart || e !== lastSelEnd) { lastSelStart = s; lastSelEnd = e; const text = this._value.slice(s, e); this._emit('selectionChange', { start: s, end: e, text }); } - }, 200) as any; - this._cleanups.push(() => { if (this._selectionTimer) clearInterval(this._selectionTimer); }); + }; + // Use longer interval for polling (won't block event loop as much) + this._selectionTimer = setInterval(check, 500) as any; + this._cleanups.push(() => { if (this._selectionTimer) { clearInterval(this._selectionTimer); this._selectionTimer = null; } }); + + ta.addEventListener('mouseup', () => setTimeout(show, 0)); + ta.addEventListener('keyup', () => { + const s = ta.selectionStart; const e = ta.selectionEnd; + if (s !== e) setTimeout(show, 0); + else setTimeout(hide, 0); + this._emit('cursorMove', this.getCursorPosition()); + }); + ta.addEventListener('blur', () => setTimeout(hide, 300)); + ta.addEventListener('click', () => setTimeout(() => { + if (ta.selectionStart === ta.selectionEnd) hide(); + }, 0)); } _buildFloatingToolbar(): void { diff --git a/tests/core.test.ts b/tests/core.test.ts index 9096b98..b4dc857 100644 --- a/tests/core.test.ts +++ b/tests/core.test.ts @@ -2243,7 +2243,7 @@ describe('MarkdownEditor - v0.2.3 浮动工具栏', () => { expect(handler.mock.calls[0][0]).toHaveProperty('text', 'hello'); ed.destroy(); done(); - }, 400); + }, 800); }); test('cursorMove 事件在按键后触发', () => {