Skip to content

Build your personal knowledge base with Trilium Notes

feat(text): insert icon-pack icons into text notes

Elian Doranee321851b757
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts
index e0ce352d3f..2deb25c72d 100644
--- a/apps/client/src/components/app_context.ts
+++ b/apps/client/src/components/app_context.ts
@@ -21,6 +21,7 @@ import type RootContainer from "../widgets/containers/root_container.js";
import { AddLinkOpts } from "../widgets/dialogs/add_link.jsx";
import type { ConfirmWithMessageOptions, ConfirmWithTitleOptions } from "../widgets/dialogs/confirm.js";
import type { ResolveOptions } from "../widgets/dialogs/delete_notes.js";
+import { IconPickerOpts } from "../widgets/dialogs/icon_picker.jsx";
import { IncludeNoteOpts } from "../widgets/dialogs/include_note.jsx";
import type { InfoProps } from "../widgets/dialogs/info.jsx";
import type { MarkdownImportOpts } from "../widgets/dialogs/markdown_import.jsx";
@@ -267,6 +268,7 @@ export type CommandMappings = {
showUploadAttachmentsDialog: CommandData & { noteId: string };
showIncludeNoteDialog: CommandData & IncludeNoteOpts;
showAddLinkDialog: CommandData & AddLinkOpts;
+ showIconPickerDialog: CommandData & IconPickerOpts;
showPasteMarkdownDialog: CommandData & MarkdownImportOpts;
closeProtectedSessionPasswordDialog: CommandData;
copyImageReferenceToClipboard: CommandData;
diff --git a/apps/client/src/layouts/layout_commons.tsx b/apps/client/src/layouts/layout_commons.tsx
index 2394e17a4b..a67ae8640d 100644
--- a/apps/client/src/layouts/layout_commons.tsx
+++ b/apps/client/src/layouts/layout_commons.tsx
@@ -23,6 +23,7 @@ export function applyModals(rootContainer: RootContainer) {
.child(<LazyDialog triggerEvents={["chooseNoteType"]} loader={() => import("../widgets/dialogs/note_type_chooser.js")} />)
.child(<LazyDialog triggerEvents={["jumpToNote", "commandPalette"]} loader={() => import("../widgets/dialogs/jump_to_note.js")} />)
.child(<LazyDialog triggerEvents={["showAddLinkDialog"]} loader={() => import("../widgets/dialogs/add_link.js")} />)
+ .child(<LazyDialog triggerEvents={["showIconPickerDialog"]} loader={() => import("../widgets/dialogs/icon_picker.js")} />)
.child(<LazyDialog triggerEvents={["cloneNoteIdsTo"]} loader={() => import("../widgets/dialogs/clone_to.js")} />)
.child(<LazyDialog triggerEvents={["moveBranchIdsTo"]} loader={() => import("../widgets/dialogs/move_to.js")} />)
.child(<LazyDialog triggerEvents={["showImportDialog"]} loader={() => import("../widgets/dialogs/import/import_dialog.js")} />)
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 461e0de05a..bc1611826c 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1261,7 +1261,8 @@
"filter-none": "All icons",
"filter-default": "Default icons",
"icon_tooltip": "{{name}}\nIcon pack: {{iconPack}}",
- "no_results": "No icons found."
+ "no_results": "No icons found.",
+ "insert_icon": "Insert icon"
},
"basic_properties": {
"note_type": "Note type",
@@ -4259,7 +4260,9 @@
"upload-an-image-from-your-device": "Upload an image from your device.",
"upload-image": "Upload image",
"warning": "Warning",
- "working": "Working…"
+ "working": "Working…",
+ "insert-icon": "Insert icon",
+ "icon": "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/apps/client/src/widgets/dialogs/icon_picker.tsx b/apps/client/src/widgets/dialogs/icon_picker.tsx
new file mode 100644
index 0000000000..e11a3495f1
--- /dev/null
+++ b/apps/client/src/widgets/dialogs/icon_picker.tsx
@@ -0,0 +1,36 @@
+import { useRef, useState } from "preact/hooks";
+
+import { t } from "../../services/i18n";
+import { useTriliumEvent } from "../react/hooks";
+import { IconPickerModal } from "../react/IconPicker";
+
+export interface IconPickerOpts {
+ /** Receives the class of the icon picked, e.g. `bx bx-star`. */
+ onSelect(iconClass: string): void;
+}
+
+/**
+ * The icon picker on a screen of its own, for the callers that have no button to hang it under —
+ * the text editor's toolbar, whose buttons belong to CKEditor rather than to this application.
+ */
+export default function IconPickerDialog() {
+ const onSelectRef = useRef<IconPickerOpts["onSelect"]>(null);
+ const [ shown, setShown ] = useState(false);
+
+ useTriliumEvent("showIconPickerDialog", ({ onSelect }) => {
+ onSelectRef.current = onSelect;
+ setShown(true);
+ });
+
+ return (
+ <IconPickerModal
+ title={t("note_icon.insert_icon")}
+ show={shown}
+ onHidden={() => setShown(false)}
+ onSelect={(iconClass) => {
+ setShown(false);
+ onSelectRef.current?.(iconClass);
+ }}
+ />
+ );
+}
diff --git a/apps/client/src/widgets/react/IconPicker.tsx b/apps/client/src/widgets/react/IconPicker.tsx
index 88dcbdaa3a..f34c810950 100644
--- a/apps/client/src/widgets/react/IconPicker.tsx
+++ b/apps/client/src/widgets/react/IconPicker.tsx
@@ -217,7 +217,6 @@ function IconPickerModalButton({
icon, title, className, disabled, onSelect, onReset, resetText, onOpened, onClosed
}: IconPickerButtonProps) {
const [ modalShown, setModalShown ] = useState(false);
- const { windowWidth } = useWindowSize();
return (
<div className={clsx("note-icon-widget", className)}>
@@ -236,41 +235,68 @@ function IconPickerModalButton({
disabled={disabled}
/>
- {/* Out of whatever the button stands in — a panel floating over a note holds its own
- stacking context, and the modal belongs to the page rather than to it. */}
- {createPortal((
- <Modal
- title={title}
- size="xl"
- show={modalShown}
- onHidden={() => {
- setModalShown(false);
- onClosed?.();
- }}
- className="icon-switcher note-icon-widget"
- scrollable
- stackable
- >
- {/* As many icons as the screen has room for, rather than the twelve a menu is
- built for (see the CSS, which gives the grid the rest of the screen). */}
- <IconPicker
- columnCount={Math.max(1, Math.floor(windowWidth / ICON_SIZE))}
- resetText={resetText}
- onSelect={(iconClass) => {
- onSelect(iconClass);
- setModalShown(false);
- }}
- onReset={onReset && (() => {
- onReset();
- setModalShown(false);
- })}
- />
- </Modal>
- ), document.body)}
+ <IconPickerModal
+ title={title}
+ show={modalShown}
+ resetText={resetText}
+ onHidden={() => {
+ setModalShown(false);
+ onClosed?.();
+ }}
+ onSelect={(iconClass) => {
+ onSelect(iconClass);
+ setModalShown(false);
+ }}
+ onReset={onReset && (() => {
+ onReset();
+ setModalShown(false);
+ })}
+ />
</div>
);
}
+interface IconPickerModalProps extends Pick<IconPickerProps, "onSelect" | "onReset" | "resetText"> {
+ /** What picking an icon is for, worn as the heading. */
+ title: string;
+ show: boolean;
+ onHidden(): void;
+}
+
+/**
+ * The picker on a screen of its own, for a host with nowhere to hang a menu: a phone, and anything
+ * that asks for an icon without a button of its own to hang one under.
+ *
+ * Closing on a pick is the host's to do, as it is the host that decides whether one pick is the end
+ * of it.
+ */
+export function IconPickerModal({ title, show, onHidden, onSelect, onReset, resetText }: IconPickerModalProps) {
+ const { windowWidth } = useWindowSize();
+
+ // Out of whatever the host stands in — a panel floating over a note holds its own stacking
+ // context, and the modal belongs to the page rather than to it.
+ return createPortal((
+ <Modal
+ title={title}
+ size="xl"
+ show={show}
+ onHidden={onHidden}
+ className="icon-switcher note-icon-widget"
+ scrollable
+ stackable
+ >
+ {/* As many icons as the screen has room for, rather than the twelve a menu is built
+ for (see the CSS, which gives the grid the rest of the screen). */}
+ <IconPicker
+ columnCount={Math.max(1, Math.floor(windowWidth / ICON_SIZE))}
+ resetText={resetText}
+ onSelect={onSelect}
+ onReset={onReset}
+ />
+ </Modal>
+ ), document.body);
+}
+
type IconWithName = (IconRegistry["sources"][number]["icons"][number] & { iconPack: string });
interface IconToCountCache {
diff --git a/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.tsx b/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.tsx
index d06842555d..0049813261 100644
--- a/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.tsx
+++ b/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.tsx
@@ -21,6 +21,8 @@ export interface CKEditorApi {
addHtmlToEditor(html: string): void;
addIncludeNote(noteId: string, boxSize?: BoxSize): void;
addImage(noteId: string): Promise<void>;
+ /** Inserts an icon of an installed icon pack, named by its class, e.g. `bx bx-star`. */
+ insertIcon(iconClass: string): void;
}
/**
@@ -217,6 +219,13 @@ export default function CKEditorWithWatchdog({ containerRef: externalContainerRe
editor?.execute("insertImage", { source: src });
});
},
+ insertIcon(iconClass) {
+ const editor = watchdogRef.current?.editor;
+ if (!editor) return;
+
+ editor.execute("insertIcon", { iconClass });
+ editor.editing.view.focus();
+ },
}));
useLegacyImperativeHandlers({
diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
index c1940cfcec..29733019a7 100644
--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
@@ -138,6 +138,14 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext
editorApi: editorApiRef.current,
});
},
+ insertIconToTextCommand() {
+ if (!editorApiRef.current) return;
+ parentComponent?.triggerCommand("showIconPickerDialog", {
+ onSelect(iconClass) {
+ editorApiRef.current?.insertIcon(iconClass);
+ }
+ });
+ },
insertDateTimeToTextCommand() {
if (!editorApiRef.current) return;
const date = new Date();
diff --git a/apps/client/src/widgets/type_widgets/text/toolbar.ts b/apps/client/src/widgets/type_widgets/text/toolbar.ts
index 06420f84a8..fe17b6546f 100644
--- a/apps/client/src/widgets/type_widgets/text/toolbar.ts
+++ b/apps/client/src/widgets/type_widgets/text/toolbar.ts
@@ -104,7 +104,7 @@ export function buildClassicToolbar(multilineToolbar: boolean, aiAssistant: bool
"footnote",
{
...buildInsertGroup(),
- items: ["link", "linkEmbed", "bookmark", "internallink", "includeNote", "|", "collapsible", "math", "mermaid", "horizontalLine", "pageBreak", "|", "dateTime", "specialCharacters", "emoji"]
+ items: ["link", "linkEmbed", "bookmark", "internallink", "includeNote", "|", "collapsible", "math", "mermaid", "horizontalLine", "pageBreak", "|", "dateTime", "specialCharacters", "emoji", "insertIcon"]
},
"|",
buildAlignmentToolbar(),
@@ -184,7 +184,8 @@ export function buildFloatingToolbar(aiAssistant: boolean) {
"insertTemplate",
"markdownImport",
"specialCharacters",
- "emoji"
+ "emoji",
+ "insertIcon"
]
};
}
diff --git a/packages/ckeditor5/src/icons/insert-icon.svg b/packages/ckeditor5/src/icons/insert-icon.svg
new file mode 100644
index 0000000000..291c3494e0
--- /dev/null
+++ b/packages/ckeditor5/src/icons/insert-icon.svg
@@ -0,0 +1,2 @@
+<!-- `bx-sticker` from Boxicons (https://boxicons.com), licensed under MIT. -->
+<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M21.796 9.982C20.849 5.357 16.729 2 12 2 6.486 2 2 6.486 2 12c0 4.729 3.357 8.849 7.982 9.796a.988.988 0 0 0 .908-.272l10.633-10.633c.238-.238.34-.578.273-.909zM11 18a7.93 7.93 0 0 1 1.365-4.471 8.18 8.18 0 0 1 .978-1.186 8.211 8.211 0 0 1 1.184-.977 8.128 8.128 0 0 1 1.36-.738c.481-.203.986-.36 1.501-.466a8.112 8.112 0 0 1 2.17-.134l-8.529 8.529c-.013-.185-.029-.37-.029-.557zm-7-6c0-4.411 3.589-8 8-8 2.909 0 5.528 1.589 6.929 4.005a9.99 9.99 0 0 0-1.943.198c-.643.132-1.274.328-1.879.583a10.15 10.15 0 0 0-1.699.923c-.533.361-1.03.771-1.479 1.22s-.858.945-1.221 1.48c-.359.533-.67 1.104-.922 1.698A10.013 10.013 0 0 0 9 18c0 .491.048.979.119 1.461C6.089 18.288 4 15.336 4 12z"/></svg>
diff --git a/packages/ckeditor5/src/plugins.ts b/packages/ckeditor5/src/plugins.ts
index 769a6e402e..2fd43a575d 100644
--- a/packages/ckeditor5/src/plugins.ts
+++ b/packages/ckeditor5/src/plugins.ts
@@ -20,6 +20,7 @@ import TriliumMentionUI from "./plugins/mention/trilium_mention_ui.js";
import TriliumSlashCommands from "./plugins/mention/slash_commands.js";
import TriliumFormatPainter from "./plugins/format_painter/format_painter.js";
import IncludeNote from "./plugins/includenote.js";
+import InlineIcon from "./plugins/inline_icon/inline_icon.js";
import LinkEmbed from "./plugins/link_embed/link_embed.js";
import Uploadfileplugin from "./plugins/file_upload/uploadfileplugin.js";
import FindInLinkWidgets from "./plugins/find_in_link_widgets.js";
@@ -71,6 +72,7 @@ const TRILIUM_PLUGINS: typeof Plugin[] = [
IndentBlockShortcutPlugin,
MarkdownImportPlugin,
IncludeNote,
+ InlineIcon,
LinkEmbed,
Uploadfileplugin,
SyntaxHighlighting,
diff --git a/packages/ckeditor5/src/plugins/inline_icon/inline_icon.spec.ts b/packages/ckeditor5/src/plugins/inline_icon/inline_icon.spec.ts
new file mode 100644
index 0000000000..20876b70ab
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/inline_icon/inline_icon.spec.ts
@@ -0,0 +1,104 @@
+import {
+ _getModelData as getModelData, _getViewData as getViewData, _setModelData as setModelData,
+ type ButtonView, type ClassicEditor, GeneralHtmlSupport, Paragraph
+} from "ckeditor5";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import { createTestEditor } from "../../../test/editor-kit.js";
+import { installGlobMock } from "../../../test/globals-test-kit.js";
+import InlineIcon from "./inline_icon.js";
+import InlineIconEditing from "./inline_icon_editing.js";
+import InlineIconUI from "./inline_icon_ui.js";
+
+describe("InlineIcon", () => {
+ let editor: ClassicEditor;
+
+ beforeEach(async () => {
+ editor = await createTestEditor([ Paragraph, InlineIcon ]);
+ });
+
+ it("loads the glue plugin and its editing/UI parts", () => {
+ expect(editor.plugins.get(InlineIcon)).toBeInstanceOf(InlineIcon);
+ expect(editor.plugins.get(InlineIconEditing)).toBeInstanceOf(InlineIconEditing);
+ expect(editor.plugins.get(InlineIconUI)).toBeInstanceOf(InlineIconUI);
+ expect(InlineIcon.pluginName).toBe("InlineIcon");
+ });
+
+ it("registers the icon as an inline object that goes wherever text goes", () => {
+ const schema = editor.model.schema;
+ setModelData(editor.model, "<paragraph>[]</paragraph>");
+ const paragraph = editor.model.document.getRoot()?.getChild(0);
+
+ expect({
+ isInline: schema.isInline("inlineIcon"),
+ isObject: schema.isObject("inlineIcon"),
+ inParagraph: !!paragraph && schema.checkChild(paragraph, "inlineIcon"),
+ carriesIconClass: schema.checkAttribute("inlineIcon", "iconClass")
+ }).toEqual({ isInline: true, isObject: true, inParagraph: true, carriesIconClass: true });
+ });
+
+ it("inserts the icon in the form icons take everywhere else, as a widget while editing", () => {
+ setModelData(editor.model, "<paragraph>Press []</paragraph>");
+
+ editor.execute("insertIcon", { iconClass: "bx bx-cog" });
+
+ expect(editor.getData()).toBe(`<p>Press&nbsp;<span class="tn-icon bx bx-cog"></span></p>`);
+ expect(getViewData(editor.editing.view, { withoutSelection: true }))
+ .toBe(`<p>Press <span class="bx bx-cog ck-widget tn-icon"` +
+ ` contenteditable="false"></span></p>`);
+ });
+
+ 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>`);
+
+ expect(getModelData(editor.model, { withoutSelection: true }))
+ .toBe(`<paragraph>Press <inlineIcon iconClass="bx bx-cog"></inlineIcon>.</paragraph>`);
+ });
+
+ it("ignores an insert that names no icon", () => {
+ setModelData(editor.model, "<paragraph>[]</paragraph>");
+
+ editor.execute("insertIcon", { iconClass: " " });
+
+ expect(editor.getData()).toBe("");
+ });
+
+ it("disables the command in a place that takes no text", () => {
+ const command = editor.commands.get("insertIcon");
+ expect(command?.isEnabled).toBe(true);
+
+ editor.enableReadOnlyMode("spec");
+ expect(command?.isEnabled).toBe(false);
+ });
+
+ it("asks the host to open its icon picker, rather than picking itself", () => {
+ const triggerCommand = vi.fn();
+ installGlobMock({ getComponentByEl: () => ({ triggerCommand }) });
+
+ const button = editor.ui.componentFactory.create("insertIcon") as unknown as ButtonView;
+ button.fire("execute");
+
+ expect(triggerCommand).toHaveBeenCalledWith("insertIconToText");
+ });
+});
+
+describe("InlineIcon under the shipped General HTML Support configuration", () => {
+ // `textNoteHtmlSupportEnabled` ships off, which leaves GHS with an empty allow-list: a span
+ // carrying nothing but classes is dropped unless a plugin claims it.
+ it("keeps an icon that the plugin models, and loses one that nothing does", async () => {
+ const withPlugin = await createTestEditor([ Paragraph, InlineIcon, GeneralHtmlSupport ], {
+ htmlSupport: { allow: [] }
+ });
+ const withoutPlugin = await createTestEditor([ Paragraph, GeneralHtmlSupport ], {
+ htmlSupport: { allow: [] }
+ });
+
+ const stored = `<p>Press <span class="tn-icon bx bx-cog"></span>.</p>`;
+
+ withPlugin.setData(stored);
+ withoutPlugin.setData(stored);
+
+ expect(withPlugin.getData()).toBe(`<p>Press <span class="tn-icon bx bx-cog"></span>.</p>`);
+ expect(withoutPlugin.getData()).toBe("<p>Press .</p>");
+ });
+});
diff --git a/packages/ckeditor5/src/plugins/inline_icon/inline_icon.ts b/packages/ckeditor5/src/plugins/inline_icon/inline_icon.ts
new file mode 100644
index 0000000000..72bcc87b06
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/inline_icon/inline_icon.ts
@@ -0,0 +1,31 @@
+import { Plugin } from "ckeditor5";
+
+import InlineIconEditing from "./inline_icon_editing.js";
+import InlineIconUI from "./inline_icon_ui.js";
+
+/**
+ * The icon feature: inserts one icon of an installed icon pack into the text, for writing about the
+ * application's own controls without a screenshot of each one.
+ *
+ * This is a "glue" plugin which loads the {@link InlineIconEditing} and {@link InlineIconUI}
+ * plugins.
+ */
+export default class InlineIcon extends Plugin {
+
+ public static get requires() {
+ return [ InlineIconEditing, InlineIconUI ] as const;
+ }
+
+ public static get pluginName() {
+ return "InlineIcon" as const;
+ }
+
+}
+
+declare module "ckeditor5" {
+ interface PluginsMap {
+ [InlineIcon.pluginName]: InlineIcon;
+ [InlineIconEditing.pluginName]: InlineIconEditing;
+ [InlineIconUI.pluginName]: InlineIconUI;
+ }
+}
diff --git a/packages/ckeditor5/src/plugins/inline_icon/inline_icon_editing.ts b/packages/ckeditor5/src/plugins/inline_icon/inline_icon_editing.ts
new file mode 100644
index 0000000000..1935828f91
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/inline_icon/inline_icon_editing.ts
@@ -0,0 +1,116 @@
+import {
+ Command, ModelElement, type ModelWriter, Plugin, toWidget, type ViewElement, Widget
+} from "ckeditor5";
+
+/** The model element an icon is; it draws whatever {@link ICON_CLASS} names. */
+const ICON = "inlineIcon";
+
+/** The icon pack's own class, such as `bx bx-star`. */
+const ICON_CLASS = "iconClass";
+
+/** The class every icon in the app wears beside its pack's, and the one the upcast matches on. */
+const MARKER_CLASS = "tn-icon";
+
+export const INSERT_ICON_COMMAND = "insertIcon";
+
+/**
+ * The icon editing feature: an inline widget that stands for one icon of one installed icon pack,
+ * written as `<span class="tn-icon bx bx-star"></span>` — the form icons take everywhere else in
+ * the application, so the share theme and the in-app help draw it without knowing about the editor.
+ *
+ * Which icon to insert is the {@link InlineIconUI} plugin's business; this one only inserts it.
+ */
+export default class InlineIconEditing extends Plugin {
+
+ public static get pluginName() {
+ return "InlineIconEditing" as const;
+ }
+
+ public static get requires() {
+ return [ Widget ] as const;
+ }
+
+ init() {
+ const editor = this.editor;
+
+ editor.model.schema.register(ICON, {
+ allowWhere: "$text",
+ isInline: true,
+ // Self-contained: the caret cannot be put inside it, and it is selected as a unit.
+ isObject: true,
+ allowAttributes: [ ICON_CLASS ]
+ });
+
+ editor.conversion.for("upcast").elementToElement({
+ view: {
+ name: "span",
+ classes: [ MARKER_CLASS ]
+ },
+ model: (viewElement, { writer }) => writer.createElement(ICON, {
+ [ICON_CLASS]: readIconClass(viewElement)
+ })
+ });
+
+ // An empty element rather than a container: the data pipeline fills an empty container
+ // with an `&nbsp;`, which would be drawn as a space beside the icon.
+ editor.conversion.for("dataDowncast").elementToElement({
+ model: ICON,
+ view: (modelElement, { writer }) => writer.createEmptyElement("span", {
+ class: viewClasses(modelElement)
+ })
+ });
+
+ editor.conversion.for("editingDowncast").elementToElement({
+ model: ICON,
+ view: (modelElement, { writer }) => toWidget(
+ writer.createContainerElement("span", { class: viewClasses(modelElement) }),
+ writer,
+ { label: editor.t("Icon") }
+ )
+ });
+
+ editor.commands.add(INSERT_ICON_COMMAND, new InsertIconCommand(editor));
+ }
+
+}
+
+/** Puts one icon where the selection is. See {@link InlineIconEditing}. */
+class InsertIconCommand extends Command {
+
+ override refresh() {
+ const model = this.editor.model;
+ const parent = model.document.selection.focus?.parent;
+ const isAllowed = parent instanceof ModelElement && model.schema.checkChild(parent, ICON);
+
+ this.isEnabled = !this.editor.isReadOnly && isAllowed;
+ }
+
+ override execute({ iconClass }: { iconClass: string }) {
+ if (!iconClass.trim()) {
+ return;
+ }
+
+ const model = this.editor.model;
+
+ model.change((writer: ModelWriter) => {
+ const icon = writer.createElement(ICON, { [ICON_CLASS]: iconClass });
+
+ model.insertObject(icon, null, null, { setSelection: "after" });
+ });
+ }
+
+}
+
+/** The pack's classes, which is everything the element wears apart from {@link MARKER_CLASS}. */
+function readIconClass(viewElement: ViewElement) {
+ return Array.from(viewElement.getClassNames())
+ .filter((className) => className !== MARKER_CLASS)
+ .join(" ");
+}
+
+/** What the element wears in the view: the marker class, then the pack's own. */
+function viewClasses(modelElement: ModelElement) {
+ const iconClass = modelElement.getAttribute(ICON_CLASS);
+
+ return iconClass ? `${MARKER_CLASS} ${iconClass}` : MARKER_CLASS;
+}
diff --git a/packages/ckeditor5/src/plugins/inline_icon/inline_icon_ui.ts b/packages/ckeditor5/src/plugins/inline_icon/inline_icon_ui.ts
new file mode 100644
index 0000000000..4a9f6cd98e
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/inline_icon/inline_icon_ui.ts
@@ -0,0 +1,47 @@
+import { ButtonView, Plugin } from "ckeditor5";
+
+import insertIconIcon from "../../icons/insert-icon.svg?raw";
+import { INSERT_ICON_COMMAND } from "./inline_icon_editing.js";
+
+/**
+ * The button an icon is inserted through. Picking the icon is left to the host, which already has
+ * a picker over every installed icon pack; it answers by running {@link INSERT_ICON_COMMAND}.
+ */
+export default class InlineIconUI extends Plugin {
+
+ public static get pluginName() {
+ return "InlineIconUI" as const;
+ }
+
+ init() {
+ const editor = this.editor;
+ const t = editor.t;
+
+ editor.ui.componentFactory.add(INSERT_ICON_COMMAND, (locale) => {
+ const command = editor.commands.get(INSERT_ICON_COMMAND);
+ const view = new ButtonView(locale);
+
+ view.set({
+ label: t("Insert icon"),
+ icon: insertIconIcon,
+ tooltip: true
+ });
+
+ if (command) {
+ view.bind("isEnabled").to(command, "isEnabled");
+ }
+
+ this.listenTo(view, "execute", () => {
+ const editorEl = editor.editing.view.getDomRoot();
+ if (!editorEl) {
+ return;
+ }
+
+ glob.getComponentByEl(editorEl).triggerCommand("insertIconToText");
+ });
+
+ return view;
+ });
+ }
+
+}