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(text): change an inserted icon without deleting it first
Elian Doran4f90deb2b2c7
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 5a3bfac526..086611463c 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -4271,7 +4271,8 @@"rotate-180": "Rotate 180°","rotate-270": "Rotate 270°","flip-horizontally": "Flip horizontally",- "flip-vertically": "Flip vertically"+ "flip-vertically": "Flip vertically",+ "change-icon": "Change icon"},"task-state-dropped-title": "Invalid task states","task-state-dropped-message_one": "A custom task state definition is invalid and was ignored.",diff --git a/packages/ckeditor5/src/plugins/inline_icon/inline_icon.spec.ts b/packages/ckeditor5/src/plugins/inline_icon/inline_icon.spec.tsindex 35b414f6c3..dc7cf1a996 100644--- a/packages/ckeditor5/src/plugins/inline_icon/inline_icon.spec.ts+++ b/packages/ckeditor5/src/plugins/inline_icon/inline_icon.spec.ts@@ -51,6 +51,20 @@ describe("InlineIcon", () => {` contenteditable="false"></span></p>`);});+ it("replaces a selected icon, keeping its colour", () => {+ setModelData(editor.model, `<paragraph>Press [<inlineIcon fontColor="rgb(255,0,0)"`+ + ` iconClass="bx bx-cog bx-rotate-90"></inlineIcon>] now</paragraph>`);++ editor.execute("insertIcon", { iconClass: "bx bx-star" });++ // `fontColor` is copied from the replaced icon; reading it from the caret instead would+ // leave the replacement uncoloured inside a coloured run. The transform is not copied: it+ // is part of `iconClass`, which the new icon supplies.+ expect(getModelData(editor.model, { withoutSelection: true }))+ .toBe(`<paragraph>Press <inlineIcon fontColor="rgb(255,0,0)" iconClass="bx bx-star">`+ + `</inlineIcon> now</paragraph>`);+ });+it("upcasts a stored icon back, keeping the pack's classes and dropping the marker", () => {editor.setData(`<p>Press <span class="tn-icon bx bx-cog"></span>.</p>`);@@ -238,6 +252,26 @@ describe("the balloon InlineIcon picks in", () => {expect(editor.plugins.get(ContextualBalloon).visibleView).toBeNull();});+ it("picks over the selected icon when the change button is the one pressed", () => {+ setModelData(+ editor.model,+ `<paragraph>Press [<inlineIcon iconClass="bx bx-cog"></inlineIcon>]</paragraph>`+ );++ pressChangeIcon(editor);+ // The picker points at the icon rather than at a caret, since that is what is selected.+ const { target } = editor.plugins.get(ContextualBalloon).getPositionOptions() ?? {};+ const at = typeof target === "function" ? target() : target;++ showIconPicker.mock.calls[0][0].onSelect("bx bx-star");++ expect(editor.getData())+ .toBe(`<p>Press <span class="tn-icon bx bx-star"></span></p>`);+ expect(at).toBeInstanceOf(Range);+ expect((at as Range).collapsed).toBe(false);+ expect(release).toHaveBeenCalledOnce();+ });+it("takes the balloon back down for a host that shows the picker its own way", () => {showIconPicker.mockReturnValue(null);@@ -360,7 +394,16 @@ describe("InlineIcon under the shipped General HTML Support configuration", () =});function pressInsertIcon(editor: ClassicEditor) {- const button = editor.ui.componentFactory.create("insertIcon") as unknown as ButtonView;+ pressPickerButton(editor, "insertIcon");+}++/** The button in the balloon over a selected icon, which opens the same picker. */+function pressChangeIcon(editor: ClassicEditor) {+ pressPickerButton(editor, "changeIcon");+}++function pressPickerButton(editor: ClassicEditor, name: string) {+ const button = editor.ui.componentFactory.create(name) as unknown as ButtonView;button.fire("execute");}diff --git a/packages/ckeditor5/src/plugins/inline_icon/inline_icon_editing.ts b/packages/ckeditor5/src/plugins/inline_icon/inline_icon_editing.tsindex 5c8674bec3..0d8e9ff2f1 100644--- a/packages/ckeditor5/src/plugins/inline_icon/inline_icon_editing.ts+++ b/packages/ckeditor5/src/plugins/inline_icon/inline_icon_editing.ts@@ -107,7 +107,10 @@ export default class InlineIconEditing extends Plugin {}-/** Puts one icon where the selection is. See {@link InlineIconEditing}. */+/**+ * Puts one icon where the selection is, replacing an icon that selection is already on. See+ * {@link InlineIconEditing}.+ */class InsertIconCommand extends Command {override refresh() {@@ -125,10 +128,17 @@ class InsertIconCommand extends Command {const model = this.editor.model;+ // A replacement copies `fontColor` and the rest from the icon it replaces, because a+ // selection on an object element has no attributes of its own. An insert copies them from+ // the caret, so an icon inserted into coloured text is that colour.+ const replaced = selectedIcon(this.editor);+ const inherited = replaced+ ? replaced.getAttributes()+ : model.document.selection.getAttributes();+model.change((writer: ModelWriter) => {const icon = writer.createElement(ICON, {- // The caret's own attributes, so an icon put into coloured text is that colour.- ...Object.fromEntries(model.document.selection.getAttributes()),+ ...Object.fromEntries(inherited),[ICON_CLASS]: iconClass});diff --git a/packages/ckeditor5/src/plugins/inline_icon/inline_icon_toolbar.spec.ts b/packages/ckeditor5/src/plugins/inline_icon/inline_icon_toolbar.spec.tsindex 1f4716bd77..cb1601f98f 100644--- a/packages/ckeditor5/src/plugins/inline_icon/inline_icon_toolbar.spec.ts+++ b/packages/ckeditor5/src/plugins/inline_icon/inline_icon_toolbar.spec.ts@@ -71,7 +71,7 @@ describe("InlineIconToolbar", () => {const definition = toolbarDefinition();const selection = editor.editing.view.document.selection;- expect(definition?.itemsConfig).toEqual([ "iconTransform" ]);+ expect(definition?.itemsConfig).toEqual([ "changeIcon", "iconTransform" ]);selectIcon("bx bx-cog");expect(definition?.getRelatedElement(selection)?.hasClass("tn-icon")).toBe(true);diff --git a/packages/ckeditor5/src/plugins/inline_icon/inline_icon_toolbar.ts b/packages/ckeditor5/src/plugins/inline_icon/inline_icon_toolbar.tsindex 02915977ff..dd6f5e2386 100644--- a/packages/ckeditor5/src/plugins/inline_icon/inline_icon_toolbar.ts+++ b/packages/ckeditor5/src/plugins/inline_icon/inline_icon_toolbar.ts@@ -7,12 +7,13 @@ import {import InlineIconEditing, {ICON, ICON_TRANSFORM_COMMAND, ICON_TRANSFORMS, type IconTransform} from "./inline_icon_editing.js";+import InlineIconUI, { CHANGE_ICON } from "./inline_icon_ui.js";/**- * The balloon shown over a selected icon, and the one control it holds.+ * The balloon shown over a selected icon, and the controls in it.*- * `WidgetToolbarRepository` owns the balloon — where it points, when it appears and when it goes- * away — so this plugin only names the items and says which view element they belong to.+ * `WidgetToolbarRepository` positions the balloon and shows or hides it, so this plugin only lists+ * the items and identifies the view element they apply to.*/export default class InlineIconToolbar extends Plugin {@@ -21,7 +22,7 @@ export default class InlineIconToolbar extends Plugin {}public static get requires() {- return [ WidgetToolbarRepository, InlineIconEditing ] as const;+ return [ WidgetToolbarRepository, InlineIconEditing, InlineIconUI ] as const;}init() {@@ -60,7 +61,7 @@ export default class InlineIconToolbar extends Plugin {afterInit() {this.editor.plugins.get(WidgetToolbarRepository).register(ICON, {ariaLabel: this.editor.t("Icon toolbar"),- items: [ ICON_TRANSFORM_COMMAND ],+ items: [ CHANGE_ICON, ICON_TRANSFORM_COMMAND ],getRelatedElement: (selection) => {const viewElement = selection.getSelectedElement();diff --git a/packages/ckeditor5/src/plugins/inline_icon/inline_icon_ui.ts b/packages/ckeditor5/src/plugins/inline_icon/inline_icon_ui.tsindex 218a69727a..0a3a5206ed 100644--- a/packages/ckeditor5/src/plugins/inline_icon/inline_icon_ui.ts+++ b/packages/ckeditor5/src/plugins/inline_icon/inline_icon_ui.ts@@ -7,7 +7,13 @@ import {import { INSERT_ICON_COMMAND } from "./inline_icon_editing.js";/**- * The button an icon is inserted through, and the balloon the picking happens in.+ * The button `InlineIconToolbar` puts in the balloon over a selected icon. It opens the same+ * picker as the insert button, because `INSERT_ICON_COMMAND` replaces a selected icon.+ */+export const CHANGE_ICON = "changeIcon";++/**+ * The buttons an icon is inserted or swapped through, and the balloon the picking happens in.** The editor owns the balloon — where it points, when it goes away — and the application paints its* own picker into it through `showIconPicker`, which is the one place every installed icon pack is@@ -34,23 +40,8 @@ export default class InlineIconUI extends Plugin {const editor = this.editor;const t = editor.t;- editor.ui.componentFactory.add(INSERT_ICON_COMMAND, (locale) => {- // Always registered: InlineIconEditing is loaded beside this plugin.- const command = editor.commands.get(INSERT_ICON_COMMAND) as Command;- const view = new ButtonView(locale);-- view.set({- label: t("Insert icon"),- icon: bxSticker,- tooltip: true- });-- view.bind("isEnabled").to(command, "isEnabled");-- this.listenTo(view, "execute", () => this.showPicker());-- return view;- });+ this.addPickerButton(INSERT_ICON_COMMAND, t("Insert icon"));+ this.addPickerButton(CHANGE_ICON, t("Change icon"));// Esc reaches the editor only while the caret still has focus; the picker takes focus with// it, so the view carries a handler of its own (see {@link IconPickerView}).@@ -139,6 +130,32 @@ export default class InlineIconUI extends Plugin {this._pickerResize.observe(container);}+ /**+ * One button that opens the picker. Both buttons execute `INSERT_ICON_COMMAND` and take their+ * enabled state from it, so both are disabled where an icon cannot be inserted.+ */+ private addPickerButton(name: string, label: string) {+ const editor = this.editor;++ editor.ui.componentFactory.add(name, (locale) => {+ // Always registered: InlineIconEditing is loaded beside this plugin.+ const command = editor.commands.get(INSERT_ICON_COMMAND) as Command;+ const view = new ButtonView(locale);++ view.set({+ label,+ icon: bxSticker,+ tooltip: true+ });++ view.bind("isEnabled").to(command, "isEnabled");++ this.listenTo(view, "execute", () => this.showPicker());++ return view;+ });+ }+private _hide() {const view = this._pickerView;