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
feat(ckeditor5): move the collapsible strings off the translation-key bridge
Elian Doranf5664e4a601b
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex f95a0b1eb2..296a77c761 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3591,6 +3591,7 @@"anchor": "Anchor","bulleted-list": "Bulleted list","caution": "Caution",+ "click-on-the-arrow-or-press-0-to-collapse-expand": "Click on the arrow or press %0 to collapse/expand.","collapsible-block": "Collapsible block","copy-formatting": "Copy formatting","create-a-bulleted-list": "Create a bulleted list",@@ -3602,6 +3603,7 @@"decrease-the-indentation-of-the-current-block": "Decrease the indentation of the current block.","display-mode": "Display mode","display-the-content-of-another-note-in-this-note": "Display the content of another note in this note",+ "drag-to-reposition-the-collapsible-block": "Drag to reposition the collapsible block","equation-preview": "Equation preview","error-rendering-equation": "Error rendering equation","footnote": "Footnote",@@ -3648,21 +3650,18 @@"preview": "Preview","quote-a-passage-from-another-source": "Quote a passage from another source.","read-more-about-mermaid-diagram-syntax": "Read more about Mermaid diagram syntax",+ "select-collapsible-block": "Select collapsible block","source-view": "Source view","split-view": "Split view",+ "summary": "Summary","tip": "Tip","to-do-list": "To-do list",+ "toggle-collapsible-block": "Toggle collapsible block","turn-the-current-block-into-a-heading": "Turn the current block into a heading.","turn-the-current-block-into-a-paragraph": "Turn the current block into a paragraph.",+ "type-the-content-here": "Type the content here...","warning": "Warning"},- "collapsible-body-placeholder": "Type the content here...",- "collapsible-button-label": "Collapsible block",- "collapsible-handle-tooltip": "Drag to reposition the collapsible block",- "collapsible-select-label": "Select collapsible block",- "collapsible-title-placeholder": "Summary",- "collapsible-toggle-label": "Toggle collapsible block",- "collapsible-tooltip": "Click on the arrow or press {{- shortcut}} to collapse/expand.","edit-states-tooltip": "Edit task states","unknown-task-state": "Undefined custom task state","task-state-dropped-title": "Invalid task states",diff --git a/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.ts b/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.tsindex 496955f0b1..1b229b1068 100644--- a/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.ts+++ b/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.ts@@ -5,7 +5,13 @@ import BlockDragHandle from "./block_drag_handle.js";import CollapsibleCommand from "./collapsible_command.js";import { OPEN_ATTRIBUTE, TRANSIENT_OPEN_ATTRIBUTE } from "./constants.js";-type TranslateFn = (key: string, params?: Record<string, unknown>) => string;+/**+ * Resolves an i18next key. Still needed for the shortcut key names, which live in the app-wide+ * `keyboard_shortcuts.keys.*` catalog that {@link formatShortcut} reads and the command palette and+ * help dialog share — those are not this package's messages to own. Everything the collapsible says+ * in its own voice goes through `editor.t()` instead.+ */+type TranslateFn = (key: string) => string;/*** The keyboard shortcut for toggling a collapsible's `open` state. Shared as@@ -294,6 +300,7 @@ export default class CollapsibleEditing extends Plugin {});}+ /** See {@link TranslateFn}: shortcut key names only. */private translate(): TranslateFn {return (this.editor.config.get("translate") as TranslateFn | undefined)?? ((key: string) => key);@@ -307,7 +314,7 @@ export default class CollapsibleEditing extends Plugin {private createEditingSummary(writer: any): any {const editor = this.editor;const plugin = this;- const t = this.translate();+ const t = editor.t;const summary = writer.createContainerElement("summary");// Selection / drag handle — non-editable affordance for selecting and@@ -317,7 +324,7 @@ export default class CollapsibleEditing extends Plugin {class: "trilium-collapsible-handle",role: "button",tabindex: "0",- "aria-label": t("text-editor.collapsible-select-label")+ "aria-label": t("Select collapsible block")}, function(this: any, domDocument: any) {const span: HTMLElement = this.toDomElement(domDocument);const resolveDetails = () => plugin.detailsFromDom(span);@@ -364,7 +371,7 @@ export default class CollapsibleEditing extends Plugin {class: "trilium-collapsible-arrow",role: "button",tabindex: "0",- "aria-label": t("text-editor.collapsible-toggle-label"),+ "aria-label": t("Toggle collapsible block"),"aria-expanded": "false"}, function(this: any, domDocument: any) {const span: HTMLElement = this.toDomElement(domDocument);@@ -402,7 +409,7 @@ export default class CollapsibleEditing extends Plugin {enableViewPlaceholder({view: this.editor.editing.view,element: summary,- text: t("text-editor.collapsible-title-placeholder"),+ text: t("Summary"),keepOnFocus: true});return summary;@@ -510,7 +517,7 @@ export default class CollapsibleEditing extends Plugin {private registerBodyPlaceholder() {const editor = this.editor;- const t = this.translate();+ const t = editor.t;editor.conversion.for("editingDowncast").add((dispatcher: any) => {dispatcher.on("insert:paragraph", (_evt: any, data: any, conversionApi: any) => {const paragraph = data.item;@@ -524,7 +531,7 @@ export default class CollapsibleEditing extends Plugin {enableViewPlaceholder({view: editor.editing.view,element: view,- text: t("text-editor.collapsible-body-placeholder"),+ text: t("Type the content here..."),keepOnFocus: true});this.bodyPlaceholdersApplied.add(view);@@ -1001,10 +1008,10 @@ export default class CollapsibleEditing extends Plugin {*/private registerSummaryHints() {const editor = this.editor;- const t = this.translate();- const title = t("text-editor.collapsible-tooltip", {- shortcut: renderToggleShortcut(t)- });+ // Two catalogs meet here: the sentence is ours, the key names inside the shortcut belong to+ // the app-wide `keyboard_shortcuts.keys.*` set. The rendered shortcut is `<kbd>` markup, and+ // nothing escapes it on the way in — the hint's tooltip sets `sanitize: false`.+ const title = editor.t("Click on the arrow or press %0 to collapse/expand.", renderToggleShortcut(this.translate()));const manager = new ContentHintManager({tooltipOptions: {sanitize: false,@@ -1176,8 +1183,8 @@ export default class CollapsibleEditing extends Plugin {*/private registerHandleHints() {const editor = this.editor;- const t = this.translate();- const title = t("text-editor.collapsible-handle-tooltip");+ const t = editor.t;+ const title = t("Drag to reposition the collapsible block");const manager = new ContentHintManager({autoHideAfterMs: HINT_AUTO_HIDE_MS});diff --git a/packages/ckeditor5/src/plugins/collapsible/collapsible_ui.spec.ts b/packages/ckeditor5/src/plugins/collapsible/collapsible_ui.spec.tsindex 0994114fbf..af0adcefc4 100644--- a/packages/ckeditor5/src/plugins/collapsible/collapsible_ui.spec.ts+++ b/packages/ckeditor5/src/plugins/collapsible/collapsible_ui.spec.ts@@ -21,18 +21,21 @@ describe("CollapsibleUI", () => {expect(CollapsibleUI.pluginName).toBe("CollapsibleUI");});- it("falls back to the raw key when the host supplies no translate callback", async () => {+ // No dictionary is configured here, so `t()` renders the message id, which is the English+ // label — never a raw `text-editor.…` key, as the retired host bridge would have shown.+ it("labels the button in English when no dictionary is configured", async () => {const editor = await createTestEditor([Essentials, Paragraph, Collapsible]);- expect(createButton(editor).label).toBe("text-editor.collapsible-button-label");+ expect(createButton(editor).label).toBe("Collapsible block");});- it("labels the button through the host's translate callback when one is configured", async () => {- const translate = vi.fn(() => "Collapsible block");- const editor = await createTestEditor([Essentials, Paragraph, Collapsible], { translate });+ it("labels the button from the editor's dictionary when one is configured", async () => {+ // Keyed by `en`, the language the editor resolves messages under when none is configured.+ const editor = await createTestEditor([Essentials, Paragraph, Collapsible], {+ translations: [ {}, { en: { dictionary: { "Collapsible block": "Bloc pliabil" } } } ]+ });- expect(createButton(editor).label).toBe("Collapsible block");- expect(translate).toHaveBeenCalledWith("text-editor.collapsible-button-label");+ expect(createButton(editor).label).toBe("Bloc pliabil");});it("executes the command and returns focus to the editing view", async () => {diff --git a/packages/ckeditor5/src/plugins/collapsible/collapsible_ui.ts b/packages/ckeditor5/src/plugins/collapsible/collapsible_ui.tsindex c49e6542bc..5eda2c422c 100644--- a/packages/ckeditor5/src/plugins/collapsible/collapsible_ui.ts+++ b/packages/ckeditor5/src/plugins/collapsible/collapsible_ui.ts@@ -9,15 +9,14 @@ export default class CollapsibleUI extends Plugin {public init(): void {const editor = this.editor;- const translate = (editor.config.get("translate") as ((key: string, params?: Record<string, unknown>) => string) | undefined)- ?? ((key: string) => key);+ const t = editor.t;editor.ui.componentFactory.add("collapsible", locale => {const command = editor.commands.get("collapsible");const button = new ButtonView(locale);button.set({- label: translate("text-editor.collapsible-button-label"),+ label: t("Collapsible block"),icon: collapsibleIcon,tooltip: true});