perf: 优化浮动工具栏 selectionChange 轮询间隔 200ms→500ms,修复测试缓慢问题
This commit is contained in:
+19
-19
@@ -747,33 +747,33 @@ export class MarkdownEditor {
|
|||||||
|
|
||||||
const hide = () => { this._hideFloatingToolbar(); };
|
const hide = () => { this._hideFloatingToolbar(); };
|
||||||
|
|
||||||
// Detect selection changes
|
// Detect selection changes via events + throttled polling
|
||||||
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
|
|
||||||
let lastSelStart = ta.selectionStart;
|
let lastSelStart = ta.selectionStart;
|
||||||
let lastSelEnd = ta.selectionEnd;
|
let lastSelEnd = ta.selectionEnd;
|
||||||
this._selectionTimer = setInterval(() => {
|
const check = () => {
|
||||||
if (this._destroyed) return;
|
if (this._destroyed || !document.body.contains(ta)) return;
|
||||||
const s = ta.selectionStart; const e = ta.selectionEnd;
|
const s = ta.selectionStart; const e = ta.selectionEnd;
|
||||||
if (s !== lastSelStart || e !== lastSelEnd) {
|
if (s !== lastSelStart || e !== lastSelEnd) {
|
||||||
lastSelStart = s; lastSelEnd = e;
|
lastSelStart = s; lastSelEnd = e;
|
||||||
const text = this._value.slice(s, e);
|
const text = this._value.slice(s, e);
|
||||||
this._emit('selectionChange', { start: s, end: e, text });
|
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 {
|
_buildFloatingToolbar(): void {
|
||||||
|
|||||||
+1
-1
@@ -2243,7 +2243,7 @@ describe('MarkdownEditor - v0.2.3 浮动工具栏', () => {
|
|||||||
expect(handler.mock.calls[0][0]).toHaveProperty('text', 'hello');
|
expect(handler.mock.calls[0][0]).toHaveProperty('text', 'hello');
|
||||||
ed.destroy();
|
ed.destroy();
|
||||||
done();
|
done();
|
||||||
}, 400);
|
}, 800);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('cursorMove 事件在按键后触发', () => {
|
test('cursorMove 事件在按键后触发', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user