Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(ckeditor): destroy replaced snippet views on live rebuild
Elian Doran505bc2aefdea
diff --git a/packages/ckeditor5/src/plugins/snippets/snippetlistview.ts b/packages/ckeditor5/src/plugins/snippets/snippetlistview.tsindex dd8056d9bf..391aca5152 100644--- a/packages/ckeditor5/src/plugins/snippets/snippetlistview.ts+++ b/packages/ckeditor5/src/plugins/snippets/snippetlistview.ts@@ -64,8 +64,14 @@ export default class SnippetListView extends ListView implements FilteredView {}private _rebuild(definitions: Collection<SnippetDefinition>, insert: (definition: SnippetDefinition) => void): void {- this.rows.length = 0;+ // `ViewCollection.clear()` only detaches its views, so destroy the previous rows (which+ // cascades to their buttons and icon/text subviews) before discarding them — otherwise+ // rebuilding on every snippet change would leak views and their `execute` listeners.+ const previous = this.rows.splice(0);this.items.clear();+ for (const { item } of previous) {+ item.destroy();+ }for (const definition of definitions) {const item = new ListItemView(this.locale);diff --git a/packages/ckeditor5/src/plugins/snippets/snippets.spec.ts b/packages/ckeditor5/src/plugins/snippets/snippets.spec.tsindex 48373276ca..260b91a1c7 100644--- a/packages/ckeditor5/src/plugins/snippets/snippets.spec.ts+++ b/packages/ckeditor5/src/plugins/snippets/snippets.spec.ts@@ -198,6 +198,14 @@ describe("SnippetsUI — menu-bar entry", () => {expect(emptyButton.label).toBe("No templates available.");expect(emptyButton.isEnabled).toBe(false);});++ it("destroys the previous menu items on rebuild, so live updates don't leak views", () => {+ const staleItem = menuButtons()[0] as ListItemView;+ const destroy = vi.spyOn(staleItem, "destroy");++ editor.plugins.get(SnippetsEditing).setDefinitions([DEF_FULL]);+ expect(destroy).toHaveBeenCalled();+ });});describe("SnippetListView", () => {@@ -255,6 +263,14 @@ describe("SnippetListView", () => {collection.remove(DEF_MINIMAL);expect(list.items.length).toBe(1);});++ it("destroys the previous rows on rebuild, so live updates don't leak views", () => {+ const staleRow = list.items.get(0) as ListItemView;+ const destroy = vi.spyOn(staleRow, "destroy");++ collection.remove(DEF_MINIMAL); // any change triggers a full rebuild+ expect(destroy).toHaveBeenCalled();+ });});describe("SnippetListItemButtonView", () => {diff --git a/packages/ckeditor5/src/plugins/snippets/snippetsui.ts b/packages/ckeditor5/src/plugins/snippets/snippetsui.tsindex 7f54aaf265..83097877b1 100644--- a/packages/ckeditor5/src/plugins/snippets/snippetsui.ts+++ b/packages/ckeditor5/src/plugins/snippets/snippetsui.ts@@ -113,7 +113,13 @@ export default class SnippetsUI extends Plugin {const t = editor.t;const definitions = editor.plugins.get(SnippetsEditing).definitions;+ // Destroy the previous menu items before replacing them; `clear()` alone only detaches, so a+ // live snippet change would otherwise leak the old buttons and their listeners.+ const previous = [...listView.items];listView.items.clear();+ for (const item of previous) {+ item.destroy();+ }if (!definitions.length) {const emptyItem = new MenuBarMenuListItemView(locale, menuView);