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(attributes): set the system attributes apart from the note's own
Elian Dorane5234ddb0356
diff --git a/apps/client/src/widgets/sidebar/AttributeList.css b/apps/client/src/widgets/sidebar/AttributeList.cssindex 1981420cdd..4190b40d5d 100644--- a/apps/client/src/widgets/sidebar/AttributeList.css+++ b/apps/client/src/widgets/sidebar/AttributeList.css@@ -38,10 +38,13 @@color: var(--muted-text-color);}-/* A definition is the same icon with a cog on it: it configures the label or relation it names, so it- reads as one, rather than as a third kind of thing with an icon of its own. */-.attribute-list-panel .attribute-kind.definition::after {- content: "\ea6e"; /* lookup code for "cog" in boxicons.css */+/*+ * Both markers are the icon of what the attribute is with a second glyph on its corner, so a definition+ * of a label still reads as a label and `cssClass` still reads as the label it is — rather than either+ * becoming a kind of its own with an icon to learn.+ */+.attribute-list-panel .attribute-kind.marker-definition::after,+.attribute-list-panel .attribute-kind.marker-system::after {position: absolute;inset-inline-end: -3px;bottom: -2px;@@ -55,6 +58,25 @@0 0 2px var(--card-background-color, var(--main-background-color));}+/* A definition flags the attribute it names rather than being one. */+.attribute-list-panel .attribute-kind.marker-definition::after {+ content: "\eade"; /* lookup code for "flag" in boxicons.css */+}++/* A cog for the names Trilium reads for itself: settings of the note rather than a word for it. */+.attribute-list-panel .attribute-kind.marker-system::after {+ content: "\ea6e"; /* lookup code for "cog" in boxicons.css */+}++/* What separates the note's own vocabulary from the names Trilium reads for itself. Quiet: the sort is+ what carries the split, the rule only marks where it falls. */+.attribute-list-panel .attribute-rows-divider {+ margin: 4px 4px 5px;+ border: none;+ border-top: 1px solid var(--main-border-color);+ opacity: 0.6;+}+.attribute-list-panel .attribute-name {flex-shrink: 0;max-width: 65%;diff --git a/apps/client/src/widgets/sidebar/AttributeList.spec.ts b/apps/client/src/widgets/sidebar/AttributeList.spec.tsindex 1e6ef5f07c..7e92c271c8 100644--- a/apps/client/src/widgets/sidebar/AttributeList.spec.ts+++ b/apps/client/src/widgets/sidebar/AttributeList.spec.ts@@ -48,6 +48,16 @@ describe("splitIntoSections", () => {// Which of the definitions the note may edit is the row's to know, the card holding both.expect(sections.definitions.map((entry) => entry.isOwned)).toEqual([ true, true, false ]);});++ it("sorts the names Trilium reads for itself last, each group keeping its order", () => {+ const { owned } = splitIntoSections(+ [ plain("cssClass"), plain("priority"), plain("archived"), plain("author") ],+ []+ );++ expect(owned.map((entry) => entry.attribute.name)).toEqual([ "priority", "author", "cssClass", "archived" ]);+ expect(owned.map((entry) => entry.isSystem)).toEqual([ false, false, true, true ]);+ });});describe("getAttributeKind / getDisplayName", () => {diff --git a/apps/client/src/widgets/sidebar/AttributeList.tsx b/apps/client/src/widgets/sidebar/AttributeList.tsxindex 052b9b9b17..2daa8ef01e 100644--- a/apps/client/src/widgets/sidebar/AttributeList.tsx+++ b/apps/client/src/widgets/sidebar/AttributeList.tsx@@ -9,7 +9,7 @@ import FAttribute from "../../entities/fattribute";import FNote from "../../entities/fnote";import contextMenu from "../../menus/context_menu";import type { Attribute } from "../../services/attribute_parser";-import attributes from "../../services/attributes";+import attributes, { isBuiltinAttribute } from "../../services/attributes";import dialog from "../../services/dialog";import { t } from "../../services/i18n";import server from "../../services/server";@@ -246,24 +246,41 @@ interface AttributeRowListProps {}/**- * One card's worth of rows. What a row offers follows from whether the note owns its attribute rather+ * One card's worth of rows, in two lists: what the note was given a name for, and below a rule, what+ * Trilium reads for itself. What a row offers follows from whether the note owns its attribute rather* than from the card it is in: the definitions card holds the note's own alongside a template's.*/function AttributeRowList({ rows, activeAttribute, onOpen, onDelete }: AttributeRowListProps) {+ function renderRows(group: AttributeEntry[]) {+ return (+ <ul class="attribute-rows">+ {group.map(({ attribute, isOwned, isSystem }, index) => (+ <AttributeRow+ key={attribute.attributeId ?? `new-${index}`}+ attribute={attribute}+ active={activeAttribute === attribute}+ isSystem={isSystem}+ // An attribute of another note names it; the current note's own would name itself.+ showOwner={!isOwned}+ onOpen={(anchor, e) => onOpen(attribute, isOwned, anchor, e)}+ onDelete={isOwned ? () => onDelete(attribute) : undefined}+ />+ ))}+ </ul>+ );+ }++ // The system attributes are sorted last (see splitIntoSections), so one index is the whole boundary.+ const boundary = rows.findIndex((entry) => entry.isSystem);+ const userDefined = boundary < 0 ? rows : rows.slice(0, boundary);+ const system = boundary < 0 ? [] : rows.slice(boundary);+return (- <ul class="attribute-rows">- {rows.map(({ attribute, isOwned }, index) => (- <AttributeRow- key={attribute.attributeId ?? `new-${index}`}- attribute={attribute}- active={activeAttribute === attribute}- // An attribute of another note names it; the current note's own would name itself.- showOwner={!isOwned}- onOpen={(anchor, e) => onOpen(attribute, isOwned, anchor, e)}- onDelete={isOwned ? () => onDelete(attribute) : undefined}- />- ))}- </ul>+ <>+ {userDefined.length > 0 && renderRows(userDefined)}+ {userDefined.length > 0 && system.length > 0 && <hr class="attribute-rows-divider" />}+ {system.length > 0 && renderRows(system)}+ </>);}@@ -271,13 +288,15 @@ interface AttributeRowProps {attribute: Attribute;/** Whether the detail popup is currently showing this attribute. */active: boolean;+ /** Whether the name is one Trilium reads for itself rather than one the note was given. */+ isSystem?: boolean;/** Names the note the attribute is inherited from, for attributes not owned by the current note. */showOwner?: boolean;onOpen: (anchor: HTMLElement | null, e: MouseEvent) => void;onDelete?: () => void;}-function AttributeRow({ attribute, active, showOwner, onOpen, onDelete }: AttributeRowProps) {+function AttributeRow({ attribute, active, isSystem, showOwner, onOpen, onDelete }: AttributeRowProps) {const rowRef = useRef<HTMLLIElement>(null);const attrType = getAttributeKind(attribute);@@ -292,9 +311,15 @@ function AttributeRow({ attribute, active, showOwner, onOpen, onDelete }: AttribonOpen(rowRef.current, e);}}>- {/* The kind is the icon: a definition carries the icon of what it defines, marked as a- definition, so `label:foo` reads as a label rather than as a label named "label:foo". */}- <span class={clsx("attribute-kind", isDefinition(attrType) && "definition")}>+ {/* The kind is the icon: a definition carries the icon of what it defines, flagged as a+ definition, so `label:foo` reads as a label rather than as a label named "label:foo".+ A system attribute carries a cog on the same corner — the two never coincide, a+ definition being named by whoever writes it. Its own tooltip names it, the marker being+ the only thing that says so. */}+ <span+ class={clsx("attribute-kind", isDefinition(attrType) ? "marker-definition" : isSystem && "marker-system")}+ title={isSystem ? t("attribute_names.system") : undefined}+ ><Icon icon={attrType === "relation" || attrType === "relation-definition" ? "bx bx-transfer" : "bx bx-hash"} /></span>@@ -432,6 +457,8 @@ function createAttribute(attrType: AttributeKind): Attribute {export interface AttributeEntry {attribute: Attribute;isOwned: boolean;+ /** Whether Trilium reads this name for itself, as opposed to the note having been given it. */+ isSystem: boolean;}export interface AttributeSections {@@ -450,16 +477,30 @@ export interface AttributeSections {export function splitIntoSections(owned: Attribute[], inherited: Attribute[]): AttributeSections {const isDefinitionEntry = ({ attribute }: AttributeEntry) => isDefinition(getAttributeKind(attribute));- const ownedEntries = owned.map((attribute) => ({ attribute, isOwned: true }));- const inheritedEntries = inherited.map((attribute) => ({ attribute, isOwned: false }));+ const toEntry = (isOwned: boolean) => (attribute: Attribute): AttributeEntry => ({+ attribute,+ isOwned,+ isSystem: isBuiltinAttribute(attribute.type, attribute.name)+ });+ const ownedEntries = owned.map(toEntry(true));+ const inheritedEntries = inherited.map(toEntry(false));return {- owned: ownedEntries.filter((entry) => !isDefinitionEntry(entry)),- inherited: inheritedEntries.filter((entry) => !isDefinitionEntry(entry)),- definitions: [ ...ownedEntries, ...inheritedEntries ].filter(isDefinitionEntry)+ owned: sortSystemLast(ownedEntries.filter((entry) => !isDefinitionEntry(entry))),+ inherited: sortSystemLast(inheritedEntries.filter((entry) => !isDefinitionEntry(entry))),+ definitions: sortSystemLast([ ...ownedEntries, ...inheritedEntries ].filter(isDefinitionEntry))};}+/**+ * The names the note was given first, the ones Trilium reads for itself after them: the note's own+ * vocabulary is what its reader is looking for, and `cssClass` or `template` is plumbing they set once.+ * A stable sort, so each group keeps the order it was collected in.+ */+function sortSystemLast(entries: AttributeEntry[]) {+ return entries.toSorted((a, b) => Number(a.isSystem) - Number(b.isSystem));+}+function collectOwned(note: FNote | null | undefined): Attribute[] {return listOwned(note?.getOwnedAttributes() ?? []);}