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
client/content manager: add extra information for items
Adorian Doraneaa7392121d9
diff --git a/apps/client/src/services/content_manager.spec.ts b/apps/client/src/services/content_manager.spec.tsindex d82196a59b..bec46c64f5 100644--- a/apps/client/src/services/content_manager.spec.ts+++ b/apps/client/src/services/content_manager.spec.ts@@ -4,9 +4,11 @@ import { buildNote } from "../test/easy-froca";import {buildCategoryQuery,CONTENT_CATEGORIES,+ type ContentCategory,isCategoryEnabled,isUserContent,- parseFilterTriggers+ parseFilterTriggers,+ resolveProperties} from "./content_manager";function categoryById(id: string) {@@ -132,6 +134,68 @@ describe("isCategoryEnabled", () => {});});+describe("resolveProperties", () => {+ const triggers: ContentCategory = {+ id: "test",+ titleKey: "unused",+ filter: "#run OR #disabled:run",+ properties: [ {+ titleKey: "prop.trigger",+ values: [+ { titleKey: "value.hourly", condition: { label: "run", is: "hourly" } },+ { titleKey: "value.daily", condition: { label: "run", is: "daily" } },+ { titleKey: "value.other", condition: { label: "run", isNot: [ "hourly", "daily" ] } }+ ]+ } ]+ };++ it("returns each matching value, so several triggers read as a list", () => {+ const note = buildNote({ title: "Both", "#run": "hourly" });++ expect(resolveProperties(note, triggers)).toEqual([+ { titleKey: "prop.trigger", values: [ { titleKey: "value.hourly" } ] }+ ]);+ });++ it("still describes a disabled item, matching through the disabled: prefix", () => {+ // The detail explaining what an item does is most useful precisely when it is switched off.+ const note = buildNote({ title: "Paused", "#disabled:run": "daily" });++ expect(resolveProperties(note, triggers)).toEqual([+ { titleKey: "prop.trigger", values: [ { titleKey: "value.daily" } ] }+ ]);+ });++ it("honours isNot, and requires the label to be present at all", () => {+ const other = buildNote({ title: "Custom", "#run": "weekly" });+ const absent = buildNote({ title: "None" });++ expect(resolveProperties(other, triggers)).toEqual([+ { titleKey: "prop.trigger", values: [ { titleKey: "value.other" } ] }+ ]);+ expect(resolveProperties(absent, triggers)).toEqual([]);+ });++ it("reads a label's own value, skipping the property when it is missing or blank", () => {+ const category: ContentCategory = {+ id: "themes",+ titleKey: "unused",+ filter: "#appTheme",+ properties: [ { titleKey: "prop.base", values: [ { valueOfLabel: "appThemeBase" } ] } ]+ };++ expect(resolveProperties(buildNote({ title: "T", "#appThemeBase": "next-dark" }), category)).toEqual([+ { titleKey: "prop.base", values: [ { text: "next-dark" } ] }+ ]);+ expect(resolveProperties(buildNote({ title: "T", "#appThemeBase": " " }), category)).toEqual([]);+ expect(resolveProperties(buildNote({ title: "T" }), category)).toEqual([]);+ });++ it("returns nothing for a category that declares no properties", () => {+ expect(resolveProperties(buildNote({ title: "X", "#appCss": "" }), categoryById("customCss"))).toEqual([]);+ });+});+describe("isUserContent", () => {it("accepts notes the user created", () => {expect(isUserContent(buildNote({ id: "abc123", title: "My script" }))).toBe(true);diff --git a/apps/client/src/services/content_manager.ts b/apps/client/src/services/content_manager.tsindex 647f147bc7..a51302eda9 100644--- a/apps/client/src/services/content_manager.ts+++ b/apps/client/src/services/content_manager.ts@@ -11,10 +11,36 @@ export interface ContentTrigger {/** How the notes within each category are ordered. */export type ContentSortOrder = "title" | "dateCreated";+/** Matched against the note's own labels, reading through the `disabled:` prefix. */+export interface ContentPropertyCondition {+ label: string;+ /** Matches when the label carries any of these values. */+ is?: string | string[];+ /** Matches when the label exists and carries none of these values. */+ isNot?: string | string[];+}++export interface ContentPropertyValue {+ /** Translation key for the text shown when {@link condition} matches. */+ titleKey?: string;+ /** Shows this label's own value instead of a fixed title. */+ valueOfLabel?: string;+ condition?: ContentPropertyCondition;+}++/** Extra detail shown next to an item, e.g. `Trigger: backend startup, hourly`. */+export interface ContentProperty {+ /** Translation key for the property name. */+ titleKey: string;+ values: ContentPropertyValue[];+}+export interface ContentCategory {id: string;/** Translation key for the category heading. */titleKey: string;+ /** Extra detail to show per item. Only properties that resolve to a value are displayed. */+ properties?: ContentProperty[];/*** Search query selecting the notes of this category.*@@ -30,13 +56,28 @@ export const CONTENT_CATEGORIES: ContentCategory[] = [id: "backendScripts",titleKey: "content_manager.category_backend_scripts",filter: "#run = backendStartup OR #run = hourly OR #run = daily"- + " OR #disabled:run = backendStartup OR #disabled:run = hourly OR #disabled:run = daily"+ + " OR #disabled:run = backendStartup OR #disabled:run = hourly OR #disabled:run = daily",+ properties: [ {+ titleKey: "content_manager.property_trigger",+ values: [+ { titleKey: "content_manager.trigger_backend_startup", condition: { label: "run", is: "backendStartup" } },+ { titleKey: "content_manager.trigger_hourly", condition: { label: "run", is: "hourly" } },+ { titleKey: "content_manager.trigger_daily", condition: { label: "run", is: "daily" } }+ ]+ } ]},{id: "frontendScripts",titleKey: "content_manager.category_frontend_scripts",filter: "#run = frontendStartup OR #run = mobileStartup"- + " OR #disabled:run = frontendStartup OR #disabled:run = mobileStartup"+ + " OR #disabled:run = frontendStartup OR #disabled:run = mobileStartup",+ properties: [ {+ titleKey: "content_manager.property_trigger",+ values: [+ { titleKey: "content_manager.trigger_frontend_startup", condition: { label: "run", is: "frontendStartup" } },+ { titleKey: "content_manager.trigger_mobile_startup", condition: { label: "run", is: "mobileStartup" } }+ ]+ } ]},{id: "widgets",@@ -51,7 +92,11 @@ export const CONTENT_CATEGORIES: ContentCategory[] = [{id: "themes",titleKey: "content_manager.category_themes",- filter: "#appTheme OR #disabled:appTheme"+ filter: "#appTheme OR #disabled:appTheme",+ properties: [ {+ titleKey: "content_manager.property_base_theme",+ values: [ { valueOfLabel: "appThemeBase" } ]+ } ]},{id: "customCss",@@ -61,7 +106,11 @@ export const CONTENT_CATEGORIES: ContentCategory[] = [{id: "iconPacks",titleKey: "content_manager.category_icon_packs",- filter: "#iconPack OR #disabled:iconPack"+ filter: "#iconPack OR #disabled:iconPack",+ properties: [ {+ titleKey: "content_manager.property_prefix",+ values: [ { valueOfLabel: "iconPack" } ]+ } ]},{id: "templates",@@ -108,6 +157,81 @@ export function isUserContent(note: FNote) {return !note.noteId.startsWith("_");}+/** One resolved value: either a fixed title to translate, or text taken from the note itself. */+export interface ResolvedPropertyValue {+ titleKey?: string;+ text?: string;+}++export interface ResolvedProperty {+ titleKey: string;+ values: ResolvedPropertyValue[];+}++/**+ * Works out the extra detail to show for a note, dropping properties that resolve to nothing.+ *+ * Translation is left to the caller: a value is either a key to translate or text read off the note,+ * and mixing the two here would drag i18n into what is otherwise pure data.+ */+export function resolveProperties(note: FNote, category: ContentCategory): ResolvedProperty[] {+ const resolved: ResolvedProperty[] = [];++ for (const property of category.properties ?? []) {+ const values: ResolvedPropertyValue[] = [];++ for (const value of property.values) {+ if (value.condition && !matchesCondition(note, value.condition)) {+ continue;+ }++ if (value.titleKey) {+ values.push({ titleKey: value.titleKey });+ } else if (value.valueOfLabel) {+ // Several labels of the same name are possible, e.g. two `#run` triggers.+ values.push(...getOwnedLabelValues(note, value.valueOfLabel)+ .filter((text) => text.trim())+ .map((text) => ({ text })));+ }+ }++ if (values.length) {+ resolved.push({ titleKey: property.titleKey, values });+ }+ }++ return resolved;+}++function matchesCondition(note: FNote, { label, is, isNot }: ContentPropertyCondition) {+ const values = getOwnedLabelValues(note, label);++ if (!values.length) {+ return false;+ }++ if (is !== undefined && !values.some((value) => toArray(is).includes(value))) {+ return false;+ }++ return isNot === undefined || !values.some((value) => toArray(isNot).includes(value));+}++/**+ * All values the note holds for a label, matched through the `disabled:` prefix so that switching an+ * item off doesn't blank out the detail explaining what it does.+ */+function getOwnedLabelValues(note: FNote, name: string) {+ return note.getOwnedAttributes()+ .filter((attribute) => attribute.type === "label"+ && attributes.getNameWithoutDangerousPrefix(attribute.name).toLowerCase() === name.toLowerCase())+ .map((attribute) => attribute.value);+}++function toArray(value: string | string[]) {+ return Array.isArray(value) ? value : [ value ];+}+/** Whether the note is currently active in this category, i.e. at least one trigger is not disabled. */export function isCategoryEnabled(note: FNote, category: ContentCategory) {return getCategoryTriggers(note, category).some(({ enabled }) => enabled);diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 577a4899e5..4b94408f2e 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2612,6 +2612,14 @@"loading": "Retrieving content...","toggle_enable": "Enable","toggle_disable": "Disable",+ "property_trigger": "Trigger",+ "property_base_theme": "Base theme",+ "property_prefix": "Prefix",+ "trigger_backend_startup": "Backend startup",+ "trigger_frontend_startup": "Frontend startup",+ "trigger_mobile_startup": "Mobile startup",+ "trigger_hourly": "Hourly",+ "trigger_daily": "Daily","sort_order": "Sort by:","sort_by_title": "Title","sort_by_date_created": "Creation date",diff --git a/apps/client/src/widgets/type_widgets/options/content_manager.css b/apps/client/src/widgets/type_widgets/options/content_manager.cssindex eef3109737..20dcced723 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager.css+++ b/apps/client/src/widgets/type_widgets/options/content_manager.css@@ -15,6 +15,16 @@white-space: nowrap;}+.content-manager-properties {+ font-size: .75em;+ opacity: .75;+}++/* Separate consecutive properties: "Trigger: hourly; Mode: authentic". */+.content-manager-property:not(:last-child)::after {+ content: "; ";+}+.content-manager-loading {opacity: 0.7;}diff --git a/apps/client/src/widgets/type_widgets/options/content_manager.tsx b/apps/client/src/widgets/type_widgets/options/content_manager.tsxindex 771e0562de..c142b887d1 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager.tsx@@ -10,6 +10,7 @@ import {type ContentSortOrder,findCategoryNotes,isCategoryEnabled,+ resolveProperties,setCategoryEnabled} from "../../../services/content_manager";import { t } from "../../../services/i18n";@@ -23,6 +24,25 @@ import OptionsPageHeader from "./components/OptionsPageHeader";const NOOP = () => {};+/** Renders a note's extra detail, e.g. `Trigger: backend startup, hourly; Mode: authentic`. */+function ContentProperties({ note, category }: { note: FNote, category: ContentCategory }) {+ const properties = resolveProperties(note, category);++ if (!properties.length) return null;++ return (+ <span className="content-manager-properties">+ {properties.map(({ titleKey, values }) => (+ <span key={titleKey} className="content-manager-property">+ <strong>{t(titleKey)}:</strong>+ {" "}+ {values.map((value) => value.titleKey ? t(value.titleKey) : value.text).join(", ")}+ </span>+ ))}+ </span>+ );+}+function ContentToggle({ note, category }: { note: FNote, category: ContentCategory }) {const [ enabled, setEnabled ] = useState(() => isCategoryEnabled(note, category));@@ -106,7 +126,12 @@ function CategoryList({ pageNote, categories }: { pageNote: FNote, categories: ClistOptions={{...LIST_OPTIONS,title: t(category.titleKey),- renderItemActions: (note) => <ContentToggle note={note} category={category} />+ renderItemActions: (note) => (+ <>+ <ContentProperties note={note} category={category} />+ <ContentToggle note={note} category={category} />+ </>+ )}}/>))}