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): re-check slash entry `isEnabled` at commit time
Elian Dorand1936344ad6a
diff --git a/packages/ckeditor5/src/plugins/mention/slash_commands.spec.ts b/packages/ckeditor5/src/plugins/mention/slash_commands.spec.tsindex d3f340ff70..e3bd18b925 100644--- a/packages/ckeditor5/src/plugins/mention/slash_commands.spec.ts+++ b/packages/ckeditor5/src/plugins/mention/slash_commands.spec.ts@@ -260,6 +260,27 @@ describe("TriliumSlashCommands", () => {expect(getModelData(editor.model, { withoutSelection: true })).toContain("<heading2>");});++ it("keeps the trigger text when the selected command goes disabled before Enter", async () => {+ type("/quo");+ await settle();++ // The entry was listed while enabled; committing it deletes the `/quo` first, so a+ // command that no-ops here would silently eat the user's input. Disable it after the+ // panel is populated to reproduce the window the query-time filter cannot see.+ const blockQuote = editor.commands.get("blockQuote");++ if (!blockQuote) {+ throw new Error("the editor should register the blockQuote command");+ }++ blockQuote.forceDisabled("spec");+ pressKey(keyCodes.enter);++ const data = getModelData(editor.model, { withoutSelection: true });+ expect(data).not.toContain("<blockQuote>");+ expect(data).toContain("/quo");+ });});describe("row rendering", () => {diff --git a/packages/ckeditor5/src/plugins/mention/slash_commands.ts b/packages/ckeditor5/src/plugins/mention/slash_commands.tsindex 579713283c..d948b5010b 100644--- a/packages/ckeditor5/src/plugins/mention/slash_commands.ts+++ b/packages/ckeditor5/src/plugins/mention/slash_commands.ts@@ -85,6 +85,9 @@ export default class TriliumSlashCommands extends Plugin {dropdownLimit: (editor.config.get("slashCommand.dropdownLimit") as number | undefined) ?? Infinity,feed: (query: string) => matchSlashCommands(this._catalog(), query).map(toFeedItem),itemRenderer: (item) => renderRow(item as SlashCommandItem),+ // The catalog gates on `isEnabled` at query time, but an entry can go stale while the+ // panel is open; re-check here so a no-op commit never costs the user their `/query`.+ canCommit: (editorInstance, item) => isSlashCommandEnabled(editorInstance, (item as SlashCommandItem).definition),commit: (editorInstance, item) => runSlashCommand(editorInstance, (item as SlashCommandItem).definition)});}diff --git a/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.ts b/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.tsindex 667276bfc0..b80d1ee039 100644--- a/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.ts+++ b/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.ts@@ -381,13 +381,19 @@ export default class TriliumMentionUI extends Plugin {// Replace everything from the marker up to the caret, not just the marker itself.const range = model.createRange(model.createPositionAt(marker.getStart()), model.createPositionAt(focus));+ const feed = this._patterns.find((pattern) => pattern.marker === data.marker);this._hide();this._clearDismissal();- const commit = this._patterns.find((feed) => feed.marker === data.marker)?.commit;+ // Bail before deleting anything if the item went stale while it sat in the open panel —+ // a `/` command that lost `isEnabled` as the selection settled. Committing it would drop+ // the trigger text and then no-op, eating what the user typed.+ if (feed?.canCommit && !feed.canCommit(editor, data.item)) {+ return;+ }- if (commit) {+ if (feed?.commit) {// Drop the trigger text first so the callback sees a collapsed selection where the// query used to be, and run it outside the change block — some callbacks open a// balloon (`/math`, `/anchor`) rather than touching the model, which needs a settled@@ -395,7 +401,7 @@ export default class TriliumMentionUI extends Plugin {// a UI takes focus itself, and must do so last.model.change((writer) => model.deleteContent(writer.createSelection(range)));editor.editing.view.focus();- commit(editor, data.item);+ feed.commit(editor, data.item);} else {editor.execute("mention", { mention: data.item, text: data.item.text, marker: data.marker, range });editor.editing.view.focus();diff --git a/packages/ckeditor5/src/plugins/mention/types.ts b/packages/ckeditor5/src/plugins/mention/types.tsindex d6ac2dcff1..3bce10c451 100644--- a/packages/ckeditor5/src/plugins/mention/types.ts+++ b/packages/ckeditor5/src/plugins/mention/types.ts@@ -28,6 +28,18 @@ declare module "@ckeditor/ckeditor5-mention" {*/preselectFirstItem?: boolean;+ /**+ * Whether the picked item can still be committed, checked the instant the user commits it —+ * before the trigger text is removed. Returning `false` closes the panel without touching+ * the document, so the user keeps what they typed. Feeds that omit it always commit.+ *+ * The `/` palette needs it: an entry's command can lose `isEnabled` between the keystroke+ * that listed it and the Enter that commits it, and since committing first deletes the+ * `/query`, an unchecked no-op would eat the input. The query-time filter cannot cover that+ * window, because no keystroke re-runs the feed once the selection has settled.+ */+ canCommit?: ( editor: Editor, item: MentionFeedObjectItem ) => boolean;+/*** What to do when the user picks an item, called with the trigger text (the marker and the* query) already removed and the selection collapsed in its place.