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 image action buttons off the translation-key bridge
Elian Doranc5ea19e57f79
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex cf4b0f6df9..9d359498f8 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2569,8 +2569,6 @@"image": {"copied-to-clipboard": "A reference to the image has been copied to clipboard. This can be pasted in any text note.","cannot-copy": "Could not copy the image reference to clipboard.",- "copy-to-clipboard": "Copy image to clipboard",- "download": "Download image","image-copied-to-clipboard": "The image has been copied to clipboard.","cannot-copy-image": "Could not copy the image to clipboard.","cannot-download": "Could not download the image."@@ -3595,6 +3593,7 @@"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",+ "copy-image-to-clipboard": "Copy image to clipboard","create-a-bulleted-list": "Create a bulleted list","create-a-new-footnote-and-reference-it-here": "Create a new footnote and reference it here","create-a-numbered-list": "Create a numbered list",@@ -3604,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",+ "download-image": "Download image","drag-to-reposition-the-collapsible-block": "Drag to reposition the collapsible block","equation-preview": "Equation preview","error-rendering-equation": "Error rendering equation",diff --git a/packages/ckeditor5/src/plugins/image_actions.spec.ts b/packages/ckeditor5/src/plugins/image_actions.spec.tsindex cbd9c91de1..fa1722833c 100644--- a/packages/ckeditor5/src/plugins/image_actions.spec.ts+++ b/packages/ckeditor5/src/plugins/image_actions.spec.ts@@ -165,27 +165,29 @@ describe("ImageActions", () => {expect(spy).toHaveBeenCalledWith("downloadImage");});- it("uses the translate config callback to label buttons when provided", () => {- return (async () => {- const translate = (key: string) => `translated:${key}`;-- editor = await createTestEditor([Essentials, Paragraph, Image, ImageBlock, ImageInline, ImageActions], {- translate- });+ it("labels the buttons from the editor's dictionary when one is configured", async () => {+ // Keyed by `en`, the language the editor resolves messages under when none is configured.+ editor = await createTestEditor([Essentials, Paragraph, Image, ImageBlock, ImageInline, ImageActions], {+ translations: [ {}, { en: { dictionary: {+ "Copy image to clipboard": "Copiază imaginea în clipboard",+ "Download image": "Descarcă imaginea"+ } } } ]+ });- const copyButton = editor.ui.componentFactory.create("copyImageToClipboard") as { label: string };- const downloadButton = editor.ui.componentFactory.create("downloadImage") as { label: string };+ const copyButton = editor.ui.componentFactory.create("copyImageToClipboard") as { label: string };+ const downloadButton = editor.ui.componentFactory.create("downloadImage") as { label: string };- expect(copyButton.label).toBe("translated:image.copy-to-clipboard");- expect(downloadButton.label).toBe("translated:image.download");- })();+ expect(copyButton.label).toBe("Copiază imaginea în clipboard");+ expect(downloadButton.label).toBe("Descarcă imaginea");});- it("falls back to the raw label key when translate config is absent", () => {+ // No dictionary is configured here, so `t()` renders the message id, which is the English+ // label — never a raw `image.…` key, as the retired host bridge would have shown.+ it("labels the buttons in English when no dictionary is configured", () => {const copyButton = editor.ui.componentFactory.create("copyImageToClipboard") as { label: string };const downloadButton = editor.ui.componentFactory.create("downloadImage") as { label: string };- expect(copyButton.label).toBe("image.copy-to-clipboard");- expect(downloadButton.label).toBe("image.download");+ expect(copyButton.label).toBe("Copy image to clipboard");+ expect(downloadButton.label).toBe("Download image");});});diff --git a/packages/ckeditor5/src/plugins/image_actions.ts b/packages/ckeditor5/src/plugins/image_actions.tsindex d25d01b5da..4b18b303fc 100644--- a/packages/ckeditor5/src/plugins/image_actions.ts+++ b/packages/ckeditor5/src/plugins/image_actions.ts@@ -26,32 +26,33 @@ export default class ImageActions extends Plugin {}init() {- this._registerButton("copyImageToClipboard", "image.copy-to-clipboard", copyIcon, "copyToClipboard");- this._registerButton("downloadImage", "image.download", downloadIcon, "download");+ const t = this.editor.t;++ // Translated here rather than inside the helper: each label has to be a literal argument of+ // a `t()` call for the message registry to find it (see `messages.ts`).+ this._registerButton("copyImageToClipboard", t("Copy image to clipboard"), copyIcon, "copyToClipboard");+ this._registerButton("downloadImage", t("Download image"), downloadIcon, "download");}- private _registerButton(name: string, labelKey: string, icon: string, action: ImageAction) {+ /** `label` arrives already translated — see {@link ImageActions#init}. */+ private _registerButton(name: string, label: string, icon: string, action: ImageAction) {const editor = this.editor;- editor.commands.add(name, new ImageActionCommand(editor, action));+ const command = new ImageActionCommand(editor, action);+ editor.commands.add(name, command);editor.ui.componentFactory.add(name, (locale) => {const button = new ButtonView(locale);button.set({- label: this._translate(labelKey),+ label,icon,tooltip: true});- button.bind("isEnabled").to(editor.commands.get(name)!, "isEnabled");+ button.bind("isEnabled").to(command, "isEnabled");this.listenTo(button, "execute", () => editor.execute(name));return button;});}- private _translate(key: string) {- const translate = this.editor.config.get("translate") as ((key: string) => string) | undefined;- return translate ? translate(key) : key;- }-}class ImageActionCommand extends Command {