From fd960135645a6faf5225e88a42a979db8d3f12d5 Mon Sep 17 00:00:00 2001 From: John Traas Date: Wed, 8 Jul 2026 13:01:40 +0200 Subject: [PATCH] fix(text-editor): keep the pending empty change when clear finds an empty editor --- .../prosemirror-adapter/prosemirror-adapter.tsx | 16 +++++++++------- src/components/text-editor/text-editor.e2e.tsx | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/components/text-editor/prosemirror-adapter/prosemirror-adapter.tsx b/src/components/text-editor/prosemirror-adapter/prosemirror-adapter.tsx index 5e157ef890..86deb42402 100644 --- a/src/components/text-editor/prosemirror-adapter/prosemirror-adapter.tsx +++ b/src/components/text-editor/prosemirror-adapter/prosemirror-adapter.tsx @@ -243,21 +243,23 @@ export class ProsemirrorAdapter { return; } - // Drop any pending debounced change so it cannot fire after this - // clear and resurrect the old content. - if (this.changeWaiting) { - this.changeEmitter.cancel(); - this.changeWaiting = false; - } - const currentContent = this.contentConverter.serialize( this.view, this.schema ); if (currentContent === '') { + // A pending change already reflects this empty content; let it + // emit so consumers still learn the document became empty. return; } + // Drop any pending debounced change so it cannot fire after this + // clear and resurrect the old content. + if (this.changeWaiting) { + this.changeEmitter.cancel(); + this.changeWaiting = false; + } + await this.updateView(''); } diff --git a/src/components/text-editor/text-editor.e2e.tsx b/src/components/text-editor/text-editor.e2e.tsx index fdf031d0be..3f87cf04d3 100644 --- a/src/components/text-editor/text-editor.e2e.tsx +++ b/src/components/text-editor/text-editor.e2e.tsx @@ -346,5 +346,20 @@ describe('limel-text-editor', () => { expect(changes).toHaveLength(0); }); + + test('clear on an already-empty editor keeps the pending empty change', async () => { + const { root, editor, changes } = await createEditor({ + value: 'hello', + }); + + editor.focus(); + document.execCommand('selectAll'); + document.execCommand('delete'); + + await root.clear(); + await sleep(DEBOUNCE_WAIT); + + expect(changes).toContain(''); + }); }); });