Skip to content

Build your personal knowledge base with Trilium Notes

refactor(ckeditor): render snippet note icons as font spans, drop the foreignObject SVG

Elian Doran9e9cf5e9db74
diff --git a/apps/client/src/stylesheets/theme-next/notes/text.css b/apps/client/src/stylesheets/theme-next/notes/text.css
index a05b7fe1be..625bc1d090 100644
--- a/apps/client/src/stylesheets/theme-next/notes/text.css
+++ b/apps/client/src/stylesheets/theme-next/notes/text.css
@@ -428,15 +428,14 @@ div.ck-template-form .ck-labeled-field-view {
border-color: transparent;
}
-/* Template icon */
-:root .ck-template-form .ck-button__icon {
- --ck-spacing-medium: 2px;
-}
-
-:root div.ck-template-form .ck-icon foreignObject {
+/* Note icon — a plain font-icon <span>, no SVG <foreignObject> wrapper. The glyph is drawn by the
+ Boxicons font on the element's own ::before, so the circular backdrop goes on ::after to avoid
+ clobbering the glyph's `content`. */
+:root div.ck-template-form .ck-template-icon {
position: relative;
+ color: var(--note-icon-color);
- &::before {
+ &::after {
display: block;
content: "";
position: absolute;
@@ -445,14 +444,6 @@ div.ck-template-form .ck-labeled-field-view {
background: var(--note-icon-hover-background-color);
z-index: -1;
}
-}
-
-:root div.ck-template-form .ck-icon .note-icon {
- display: flex;
- height: 100%;
- justify-content: center;
- align-items: center;
- color: var(--note-icon-color);
}
/* Template name */
diff --git a/apps/client/src/widgets/type_widgets/text/snippets.ts b/apps/client/src/widgets/type_widgets/text/snippets.ts
index 2952487729..c7f3526239 100644
--- a/apps/client/src/widgets/type_widgets/text/snippets.ts
+++ b/apps/client/src/widgets/type_widgets/text/snippets.ts
@@ -4,7 +4,6 @@ import type LoadResults from "../../../services/load_results.js";
import search from "../../../services/search.js";
import type { SnippetDefinition } from "@triliumnext/ckeditor5";
import type FNote from "../../../entities/fnote.js";
-import { escapeHtml } from "../../../services/utils.js";
interface TemplateData {
title: string;
@@ -36,7 +35,8 @@ export default async function getTemplates() {
definitions.push({
title: snippet.title,
data: () => templateCache.get(snippet.noteId)?.content ?? "",
- icon: buildIcon(snippet),
+ iconClass: snippet.getIcon(),
+ iconColorClass: snippet.getColorClass() || undefined,
description
});
}
@@ -60,15 +60,6 @@ async function invalidateCacheFor(snippet: FNote) {
return data;
}
-function buildIcon(snippet: FNote) {
- return /*xml*/`\
-<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
- <foreignObject x="0" y="0" width="20" height="20">
- <span class="note-icon ${escapeHtml(snippet.getIcon())} ${escapeHtml(snippet.getColorClass())}" xmlns="http://www.w3.org/1999/xhtml"></span>
- </foreignObject>
-</svg>`
-}
-
async function handleContentUpdate(affectedNoteIds: string[], setTemplates: (value: SnippetDefinition[]) => void) {
const updatedNoteIds = new Set(affectedNoteIds);
const templateNoteIds = new Set(templateCache.keys());
diff --git a/packages/ckeditor5/src/plugins/snippets/snippetlistitembuttonview.ts b/packages/ckeditor5/src/plugins/snippets/snippetlistitembuttonview.ts
index a2df6f2b88..be069fbb40 100644
--- a/packages/ckeditor5/src/plugins/snippets/snippetlistitembuttonview.ts
+++ b/packages/ckeditor5/src/plugins/snippets/snippetlistitembuttonview.ts
@@ -7,13 +7,15 @@ import type { SnippetDefinition } from "./snippetsconfig.js";
* (optionally) its description. Both the title and description can be highlighted to reflect the
* current search query.
*
- * Mirrors the row rendered by the premium `Template` plugin this replaces, so the visual layout and
- * search behaviour stay familiar.
+ * The note icon is rendered as a plain font-icon `<span>` (see {@link NoteIconView}) rather than fed
+ * through CKEditor's SVG-only `IconView` — Trilium owns this plugin, so the old SVG `<foreignObject>`
+ * wrapper around a Boxicon is no longer needed.
*/
export default class SnippetListItemButtonView extends ButtonView {
public readonly definition: SnippetDefinition;
+ private noteIconView: NoteIconView | null = null;
private textPartView: SnippetTextPartView | null = null;
constructor(locale: Locale, definition: SnippetDefinition) {
@@ -23,23 +25,20 @@ export default class SnippetListItemButtonView extends ButtonView {
this.set({
withText: true,
- class: "ck-snippet-button",
- role: "option",
- icon: definition.icon
+ class: "ck-template-button",
+ role: "option"
});
-
- // Snippet icons carry the note's own colours (see `buildIcon` in the client), so don't let
- // the button's theme recolour them.
- this.iconView.isColorInherited = false;
}
public override render(): void {
super.render();
+ this.noteIconView = new NoteIconView(this.locale, this.definition);
this.textPartView = new SnippetTextPartView(this.locale, this.definition, this.labelView.element?.id);
- // Swap the plain label for the richer title + description block.
+ // Replace the plain label with a note-icon + (title + description) block.
this.children.remove(this.labelView);
+ this.children.add(this.noteIconView);
this.children.add(this.textPartView);
}
@@ -67,6 +66,24 @@ export default class SnippetListItemButtonView extends ButtonView {
}
}
+/**
+ * A note's font icon (`tn-icon bx bx-…`), rendered as a `<span>`. The colour class from the note's
+ * `#color` label, when present, is applied alongside so the icon keeps its note colour.
+ */
+class NoteIconView extends View {
+
+ constructor(locale: Locale | undefined, definition: SnippetDefinition) {
+ super(locale);
+
+ this.setTemplate({
+ tag: "span",
+ attributes: {
+ class: ["ck-template-icon", definition.iconClass, definition.iconColorClass].filter(Boolean)
+ }
+ });
+ }
+}
+
/**
* The title + optional description block placed inside a {@link SnippetListItemButtonView}.
*/
@@ -96,7 +113,7 @@ class SnippetTextPartView extends View {
this.descriptionView.extendTemplate({
tag: "p",
attributes: {
- class: ["ck-snippet__description"]
+ class: ["ck-template-form__description"]
}
});
children.push(this.descriptionView);
@@ -107,7 +124,7 @@ class SnippetTextPartView extends View {
this.setTemplate({
tag: "div",
attributes: {
- class: ["ck", "ck-snippet__text-part"]
+ class: ["ck", "ck-template-form__text-part"]
},
children
});
diff --git a/packages/ckeditor5/src/plugins/snippets/snippetlistview.ts b/packages/ckeditor5/src/plugins/snippets/snippetlistview.ts
index 0db9fbef8c..ced9c3313a 100644
--- a/packages/ckeditor5/src/plugins/snippets/snippetlistview.ts
+++ b/packages/ckeditor5/src/plugins/snippets/snippetlistview.ts
@@ -19,7 +19,7 @@ export default class SnippetListView extends ListView implements FilteredView {
this.extendTemplate({
attributes: {
role: "listbox",
- class: ["ck-snippet-list"]
+ class: ["ck-template-list"]
}
});
diff --git a/packages/ckeditor5/src/plugins/snippets/snippetsconfig.ts b/packages/ckeditor5/src/plugins/snippets/snippetsconfig.ts
index cdf422b80d..08d0aa62b2 100644
--- a/packages/ckeditor5/src/plugins/snippets/snippetsconfig.ts
+++ b/packages/ckeditor5/src/plugins/snippets/snippetsconfig.ts
@@ -1,10 +1,8 @@
/**
* The shape of a single text-snippet ("template") entry shown in the `insertTemplate` dropdown.
*
- * Intentionally structurally compatible with the premium `TemplateDefinition` this plugin replaces,
- * so the client-side builder in `snippets.ts` does not have to change: `data` may be a plain HTML
- * string or a getter, which lets the content stay live (the getter reads a mutable cache) without
- * recreating the editor.
+ * `data` may be a plain HTML string or a getter, which lets the content stay live (the getter reads
+ * a mutable cache) without recreating the editor.
*/
export interface SnippetDefinition {
/** The title of the snippet, shown as the primary label and used for search matching. */
@@ -16,8 +14,15 @@ export interface SnippetDefinition {
*/
data: (() => string) | string;
- /** Optional SVG string used as the entry icon. A generic icon is used when omitted. */
- icon?: string;
+ /**
+ * The note's icon as a Boxicon/`tn-icon` class list (e.g. `"tn-icon bx bx-note"`). Rendered
+ * directly as a font-icon `<span>` — since Trilium owns this plugin, there is no need to wrap the
+ * icon in an SVG `<foreignObject>` to satisfy CKEditor's generic `IconView`.
+ */
+ iconClass?: string;
+
+ /** Optional colour class applied alongside {@link iconClass} (from the note's `#color` label). */
+ iconColorClass?: string;
/** Optional longer description shown under the title and also matched during search. */
description?: string;
diff --git a/packages/ckeditor5/src/plugins/snippets/snippetsui.ts b/packages/ckeditor5/src/plugins/snippets/snippetsui.ts
index fe56e72866..04611dd733 100644
--- a/packages/ckeditor5/src/plugins/snippets/snippetsui.ts
+++ b/packages/ckeditor5/src/plugins/snippets/snippetsui.ts
@@ -45,7 +45,7 @@ export default class SnippetsUI extends Plugin {
queryView: {
label: t("Search template")
},
- class: "ck-snippet-form",
+ class: "ck-template-form",
infoView: {
text: {
notFound: {
@@ -129,10 +129,11 @@ export default class SnippetsUI extends Plugin {
for (const definition of definitions) {
const item = new MenuBarMenuListItemView(locale, menuView);
const button = new MenuBarMenuListItemButtonView(locale);
+ // No per-item note icon here: `MenuBarMenuListItemButtonView` only accepts an SVG icon,
+ // and Trilium does not enable CKEditor's menu bar, so this surface is never shown anyway.
button.set({
- class: "ck-snippet-button",
- label: definition.title,
- icon: definition.icon
+ class: "ck-template-button",
+ label: definition.title
});
button.delegate("execute").to(menuView);
button.on("execute", () => insert(definition.data));
diff --git a/packages/ckeditor5/src/plugins/snippets/theme/snippets.css b/packages/ckeditor5/src/plugins/snippets/theme/snippets.css
index d1ce69a488..b3d43835b4 100644
--- a/packages/ckeditor5/src/plugins/snippets/theme/snippets.css
+++ b/packages/ckeditor5/src/plugins/snippets/theme/snippets.css
@@ -1,21 +1,11 @@
:root {
- --ck-snippet-dropdown-width: 345px;
- --ck-snippet-dropdown-height: 300px;
- --ck-snippet-icon-size: 45px;
+ --ck-template-dropdown-width: 345px;
+ --ck-template-dropdown-height: 300px;
+ --ck-template-icon-size: 45px;
}
-/* Keep the note's own icon colours instead of the button's theme colour. */
-.ck-button.ck-snippet-button .ck.ck-icon,
-.ck-button.ck-snippet-button .ck.ck-icon * {
- color: unset;
-}
-
-.ck-button.ck-snippet-button .ck.ck-icon *:not([fill]) {
- fill: unset;
-}
-
-.ck.ck-snippet-form {
- width: var(--ck-snippet-dropdown-width);
+.ck.ck-template-form {
+ width: var(--ck-template-dropdown-width);
& > .ck-labeled-field-view {
padding: var(--ck-spacing-large);
@@ -25,8 +15,8 @@
border-top: 1px solid var(--ck-color-base-border);
}
- & > .ck-search__results > .ck.ck-list.ck-snippet-list {
- max-height: var(--ck-snippet-dropdown-height);
+ & > .ck-search__results > .ck.ck-list.ck-template-list {
+ max-height: var(--ck-template-dropdown-height);
padding: var(--ck-spacing-large);
overflow: hidden auto;
overscroll-behavior: contain;
@@ -38,9 +28,9 @@
& > .ck.ck-list__item > .ck.ck-button {
display: grid;
- grid-template-columns: var(--ck-snippet-icon-size) 1fr;
- grid-template-rows: var(--ck-snippet-icon-size);
- grid-template-areas: "ck-snippet-icon ck-snippet-content";
+ grid-template-columns: var(--ck-template-icon-size) 1fr;
+ grid-template-rows: var(--ck-template-icon-size);
+ grid-template-areas: "ck-template-icon ck-template-content";
gap: var(--ck-spacing-large);
align-items: center;
width: 100%;
@@ -53,15 +43,21 @@
border-color: var(--ck-color-dropdown-panel-background);
}
- & > .ck.ck-icon {
- grid-area: ck-snippet-icon;
- width: var(--ck-snippet-icon-size);
- height: auto;
+ /* The note's font icon, rendered as a plain <span> (no SVG wrapper). */
+ & > .ck-template-icon {
+ grid-area: ck-template-icon;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ width: var(--ck-template-icon-size);
+ height: var(--ck-template-icon-size);
margin: 0;
+ font-size: calc(var(--ck-template-icon-size) * 0.55);
+ line-height: 1;
}
- & > .ck-snippet__text-part {
- grid-area: ck-snippet-content;
+ & > .ck-template-form__text-part {
+ grid-area: ck-template-content;
display: flex;
flex-direction: column;
justify-content: space-evenly;
@@ -80,7 +76,7 @@
text-overflow: ellipsis;
}
- & .ck-snippet__description {
+ & .ck-template-form__description {
display: -webkit-box;
overflow: hidden;
margin-top: var(--ck-spacing-tiny);
@@ -105,7 +101,7 @@
}
@media screen and (width <= 600px) {
- .ck.ck-snippet-form {
- --ck-snippet-dropdown-width: 260px;
+ .ck.ck-template-form {
+ --ck-template-dropdown-width: 260px;
}
}