Skip to content

Build your personal knowledge base with Trilium Notes

client/snippets: add a badge for snippet notes

Adorian Doranf03980127167
diff --git a/apps/client/src/stylesheets/theme-next-dark.css b/apps/client/src/stylesheets/theme-next-dark.css
index f92d4db844..332d812074 100644
--- a/apps/client/src/stylesheets/theme-next-dark.css
+++ b/apps/client/src/stylesheets/theme-next-dark.css
@@ -218,6 +218,7 @@
--badge-temporaraily-editable-background-color: #297331;
--badge-read-only-background-color: #af4340;
--badge-share-background-color: #4d4d4d;
+ --badge-snippet-background-color: #4d4d4d;
--badge-clipped-note-background-color: #295773;
--badge-doc-url-background-color: #1e5c42;
--badge-execute-background-color: #604180;
diff --git a/apps/client/src/stylesheets/theme-next-light.css b/apps/client/src/stylesheets/theme-next-light.css
index 8df14e6f5d..7e52a1ea08 100644
--- a/apps/client/src/stylesheets/theme-next-light.css
+++ b/apps/client/src/stylesheets/theme-next-light.css
@@ -210,6 +210,7 @@
--badge-temporaraily-editable-background-color: #35a64c;
--badge-read-only-background-color: #c8302c;
--badge-share-background-color: #6b6b6b;
+ --badge-snippet-background-color: #6b6b6b;
--badge-clipped-note-background-color: #2284c0;
--badge-doc-url-background-color: #2e7d5e;
--badge-execute-background-color: #7b47af;
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index b4d97f17ff..e904737d95 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2546,6 +2546,11 @@
"bookmark_buttons": {
"bookmarks": "Bookmarks"
},
+ "snippet_badge": {
+ "label": "{{type}} snippet",
+ "type_text": "Text",
+ "tooltip": "A reusable piece of text or code, meant to be inserted into the editor (from the snippet menu in the toolbar, or via a slash command)."
+ },
"active_content_badges": {
"type_icon_pack": "Icon pack",
"type_backend_script": "Backend script",
diff --git a/apps/client/src/widgets/layout/NoteBadges.css b/apps/client/src/widgets/layout/NoteBadges.css
index 9485b57c65..cac8acf4df 100644
--- a/apps/client/src/widgets/layout/NoteBadges.css
+++ b/apps/client/src/widgets/layout/NoteBadges.css
@@ -22,6 +22,7 @@
&.clipped-note-badge {--color: var(--badge-clipped-note-background-color);}
&.doc-url-badge {--color: var(--badge-doc-url-background-color);}
&.execute-badge {--color: var(--badge-execute-background-color);}
+ &.snippet-badge {--color: var(--badge-snippet-background-color);}
&.save-status-badge {
--default-opacity: 0.4;
opacity: var(--default-opacity);
diff --git a/apps/client/src/widgets/layout/NoteBadges.tsx b/apps/client/src/widgets/layout/NoteBadges.tsx
index 682c4387d2..2f59f81b64 100644
--- a/apps/client/src/widgets/layout/NoteBadges.tsx
+++ b/apps/client/src/widgets/layout/NoteBadges.tsx
@@ -12,6 +12,7 @@ import { useGetContextDataFrom, useIsNoteReadOnly, useNoteContext, useNoteLabel,
import { useShareState } from "../ribbon/BasicPropertiesTab";
import { useShareInfo } from "../shared_info";
import { ActiveContentBadges } from "./ActiveContentBadges";
+import { SnippetBadge } from "./SnippetBadge";
export default function NoteBadges() {
return (
@@ -21,6 +22,7 @@ export default function NoteBadges() {
<ShareBadge />
<ClippedNoteBadge />
<ExecuteBadge />
+ <SnippetBadge />
<ActiveContentBadges />
</div>
);
diff --git a/apps/client/src/widgets/layout/SnippetBadge.tsx b/apps/client/src/widgets/layout/SnippetBadge.tsx
new file mode 100644
index 0000000000..2133393a19
--- /dev/null
+++ b/apps/client/src/widgets/layout/SnippetBadge.tsx
@@ -0,0 +1,68 @@
+import { useEffect, useState } from "preact/hooks";
+
+import FNote from "../../entities/fnote";
+import attributes from "../../services/attributes";
+import { t } from "../../services/i18n";
+import mimeTypesService from "../../services/mime_types";
+import { Badge } from "../react/Badge";
+import { useNoteContext, useNoteProperty, useTriliumEvent } from "../react/hooks";
+
+interface SnippetInfo {
+ /** Human-readable kind of snippet, e.g. "Text", "Markdown", "CSS". */
+ typeName: string;
+ icon: string;
+}
+
+/**
+ * Informational badge marking a note as a reusable snippet and naming its kind — e.g. "CSS snippet"
+ * rather than just a generic code note, and "Text snippet" so a rich-text snippet is distinguishable
+ * from an ordinary text note. Read-only; enabling/disabling is not offered.
+ */
+export function SnippetBadge() {
+ const { note } = useNoteContext();
+ const info = useSnippetInfo(note);
+
+ return (info &&
+ <Badge
+ className="snippet-badge"
+ icon={info.icon}
+ text={t("snippet_badge.label", { type: info.typeName })}
+ tooltip={t("snippet_badge.tooltip")}
+ />
+ );
+}
+
+function useSnippetInfo(note: FNote | null | undefined) {
+ const [ info, setInfo ] = useState<SnippetInfo | null>(null);
+ const noteType = useNoteProperty(note, "type");
+ const noteMime = useNoteProperty(note, "mime");
+
+ function refresh() {
+ if (!note || !(note.hasLabel("snippet") || note.hasLabel("textSnippet"))) {
+ setInfo(null);
+ return;
+ }
+
+ // Rich-text snippets are plain text notes; name them "Text" rather than via their text/html MIME.
+ if (note.type === "text") {
+ setInfo({ typeName: t("snippet_badge.type_text"), icon: "bx bx-align-left" });
+ return;
+ }
+
+ const mimeType = mimeTypesService.getMimeTypes().find((mt) => mt.mime === note.mime);
+ setInfo({
+ typeName: mimeType?.title ?? note.mime,
+ icon: mimeType?.icon ?? "bx bx-code"
+ });
+ }
+
+ useEffect(refresh, [ note, noteType, noteMime ]);
+
+ useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
+ if (loadResults.getAttributeRows().some((attr) => attributes.isAffecting(attr, note))) {
+ refresh();
+ }
+ });
+
+ return info;
+}