Skip to content

Build your personal knowledge base with Trilium Notes

fix(ckeditor): move line up/down stops working after editor recreation (closes #10095)

Elian Doran2a0575e3e501
diff --git a/packages/ckeditor5/src/plugins/move_block_updown.spec.ts b/packages/ckeditor5/src/plugins/move_block_updown.spec.ts
index 478cedb1d0..17dd360230 100644
--- a/packages/ckeditor5/src/plugins/move_block_updown.spec.ts
+++ b/packages/ckeditor5/src/plugins/move_block_updown.spec.ts
@@ -164,6 +164,27 @@ describe("MoveBlockUpDownPlugin", () => {
expect(spy).toHaveBeenCalledWith("moveBlockDown");
});
+ it("removes the native keydown listener when the editor is destroyed (#10095)", async () => {
+ setModelData(editor.model, "<paragraph>First[]</paragraph>");
+
+ await new Promise<void>((resolve) => {
+ editor.editing.view.once("render", () => resolve());
+ editor.editing.view.forceRender();
+ });
+
+ const domRoot = editor.editing.view.getDomRoot();
+ expect(domRoot).toBeTruthy();
+ if (!domRoot) { return; }
+
+ const removeSpy = vi.spyOn(domRoot, "removeEventListener");
+
+ await editor.destroy();
+
+ // The capturing keydown listener must be torn down so it cannot linger on the
+ // reused editing root and swallow the shortcut after the editor is recreated.
+ expect(removeSpy).toHaveBeenCalledWith("keydown", expect.any(Function), { capture: true });
+ });
+
it("ignores Ctrl+Alt+ArrowUp (only pure Alt should trigger)", async () => {
setModelData(editor.model,
"<paragraph>First</paragraph>" +
diff --git a/packages/ckeditor5/src/plugins/move_block_updown.ts b/packages/ckeditor5/src/plugins/move_block_updown.ts
index d523938cf5..0c311edffb 100644
--- a/packages/ckeditor5/src/plugins/move_block_updown.ts
+++ b/packages/ckeditor5/src/plugins/move_block_updown.ts
@@ -21,7 +21,7 @@ export default class MoveBlockUpDownPlugin extends Plugin {
this.bindMoveBlockShortcuts(editor);
}
- bindMoveBlockShortcuts(editor: any) {
+ bindMoveBlockShortcuts(editor: Editor) {
editor.editing.view.once('render', () => {
const domRoot = editor.editing.view.getDomRoot();
/* v8 ignore next 1 -- domRoot is always set while the editor is alive; only null after destroy */
@@ -42,6 +42,15 @@ export default class MoveBlockUpDownPlugin extends Plugin {
};
domRoot.addEventListener('keydown', handleKeydown, { capture: true });
+
+ // Remove the native listener when the editor is destroyed. The editing root is
+ // reused across editor recreations (e.g. switching the content language rebuilds
+ // the editor in the same container — see #10095). Without this cleanup the orphaned
+ // listener stays attached, captures the keystroke first and runs editor.execute()
+ // against the destroyed editor — silently swallowing the shortcut until a reload.
+ this.listenTo(editor, 'destroy', () => {
+ domRoot.removeEventListener('keydown', handleKeydown, { capture: true });
+ });
});
}