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
chore(core): integrate promoted_attribute_definition_parser
Elian Doran60cb8d950ef0
diff --git a/apps/client/src/services/promoted_attribute_definition_parser.ts b/apps/client/src/services/promoted_attribute_definition_parser.tsindex 0d93aae3c5..292e64438b 100644--- a/apps/client/src/services/promoted_attribute_definition_parser.ts+++ b/apps/client/src/services/promoted_attribute_definition_parser.ts@@ -1,14 +1,4 @@-export type LabelType = "text" | "number" | "boolean" | "date" | "datetime" | "time" | "url" | "color";-type Multiplicity = "single" | "multi";--export interface DefinitionObject {- isPromoted?: boolean;- labelType?: LabelType;- multiplicity?: Multiplicity;- numberPrecision?: number;- promotedAlias?: string;- inverseRelation?: string;-}+import { DefinitionObject, LabelType, Multiplicity } from "@triliumnext/commons";function parse(value: string) {const tokens = value.split(",").map((t) => t.trim());diff --git a/apps/client/src/widgets/attribute_widgets/UserAttributesList.tsx b/apps/client/src/widgets/attribute_widgets/UserAttributesList.tsxindex f01e70b491..93ed5356c9 100644--- a/apps/client/src/widgets/attribute_widgets/UserAttributesList.tsx+++ b/apps/client/src/widgets/attribute_widgets/UserAttributesList.tsx@@ -1,14 +1,16 @@+import "./UserAttributesList.css";++import type { DefinitionObject } from "@triliumnext/commons";+import { ComponentChildren, CSSProperties } from "preact";import { useState } from "preact/hooks";+import FNote from "../../entities/fnote";-import "./UserAttributesList.css";-import { useTriliumEvent } from "../react/hooks";import attributes from "../../services/attributes";-import { DefinitionObject } from "../../services/promoted_attribute_definition_parser";+import { getReadableTextColor } from "../../services/css_class_manager";import { formatDateTime } from "../../utils/formatters";-import { ComponentChildren, CSSProperties } from "preact";+import { useTriliumEvent } from "../react/hooks";import Icon from "../react/Icon";import NoteLink from "../react/NoteLink";-import { getReadableTextColor } from "../../services/css_class_manager";interface UserAttributesListProps {note: FNote;@@ -29,7 +31,7 @@ export default function UserAttributesDisplay({ note, ignoredAttributes }: UserA<div className="user-attributes">{userAttributes?.map(attr => buildUserAttribute(attr))}</div>- )+ );}@@ -46,13 +48,13 @@ function useNoteAttributesWithDefinitions(note: FNote, attributesToIgnore: stri}function UserAttribute({ attr, children, style }: { attr: AttributeWithDefinitions, children: ComponentChildren, style?: CSSProperties }) {- const className = `${attr.type === "label" ? "label" + " " + attr.def.labelType : "relation"}`;+ const className = `${attr.type === "label" ? `label` + ` ${ attr.def.labelType}` : "relation"}`;return (<span key={attr.friendlyName} className={`user-attribute type-${className}`} style={style}>{children}</span>- )+ );}function buildUserAttribute(attr: AttributeWithDefinitions): ComponentChildren {@@ -61,7 +63,7 @@ function buildUserAttribute(attr: AttributeWithDefinitions): ComponentChildren {let style: CSSProperties | undefined;if (attr.type === "label") {- let value = attr.value;+ const value = attr.value;switch (attr.def.labelType) {case "number":let formattedValue = value;@@ -102,7 +104,7 @@ function buildUserAttribute(attr: AttributeWithDefinitions): ComponentChildren {content = <>{defaultLabel}<NoteLink notePath={attr.value} showNoteIcon /></>;}- return <UserAttribute attr={attr} style={style}>{content}</UserAttribute>+ return <UserAttribute attr={attr} style={style}>{content}</UserAttribute>;}function getAttributesWithDefinitions(note: FNote, attributesToIgnore: string[] = []): AttributeWithDefinitions[] {diff --git a/apps/client/src/widgets/collections/table/columns.tsx b/apps/client/src/widgets/collections/table/columns.tsxindex 74db6ddb71..3c2e487634 100644--- a/apps/client/src/widgets/collections/table/columns.tsx+++ b/apps/client/src/widgets/collections/table/columns.tsx@@ -1,11 +1,12 @@-import type { CellComponent, ColumnDefinition, EmptyCallback, FormatterParams, ValueBooleanCallback, ValueVoidCallback } from "tabulator-tables";-import { LabelType } from "../../../services/promoted_attribute_definition_parser.js";+import { LabelType } from "@triliumnext/commons";import { JSX } from "preact";-import { renderReactWidget } from "../../react/react_utils.jsx";-import Icon from "../../react/Icon.jsx";import { useEffect, useRef, useState } from "preact/hooks";+import type { CellComponent, ColumnDefinition, EmptyCallback, FormatterParams, ValueBooleanCallback, ValueVoidCallback } from "tabulator-tables";+import froca from "../../../services/froca.js";+import Icon from "../../react/Icon.jsx";import NoteAutocomplete from "../../react/NoteAutocomplete.jsx";+import { renderReactWidget } from "../../react/react_utils.jsx";type ColumnType = LabelType | "relation";@@ -78,7 +79,7 @@ export function buildColumnDefinitions({ info, movableRows, existingColumnData,rowHandle: movableRows,width: calculateIndexColumnWidth(rowNumberHint, movableRows),formatter: wrapFormatter(({ cell, formatterParams }) => <div>- {(formatterParams as RowNumberFormatterParams).movableRows && <><span class="bx bx-dots-vertical-rounded"></span>{" "}</>}+ {(formatterParams as RowNumberFormatterParams).movableRows && <><span class="bx bx-dots-vertical-rounded" />{" "}</>}{cell.getRow().getPosition(true)}</div>),formatterParams: { movableRows } satisfies RowNumberFormatterParams@@ -200,14 +201,14 @@ function wrapEditor(Component: (opts: EditorOpts) => JSX.Element): ((editorParams: {},) => HTMLElement | false) {return (cell, _, success, cancel, editorParams) => {- const elWithParams = <Component cell={cell} success={success} cancel={cancel} editorParams={editorParams} />+ const elWithParams = <Component cell={cell} success={success} cancel={cancel} editorParams={editorParams} />;return renderReactWidget(null, elWithParams)[0];};}function NoteFormatter({ cell }: FormatterOpts) {const noteId = cell.getValue();- const [ note, setNote ] = useState(noteId ? froca.getNoteFromCache(noteId) : null)+ const [ note, setNote ] = useState(noteId ? froca.getNoteFromCache(noteId) : null);useEffect(() => {if (!noteId || note?.noteId === noteId) return;@@ -231,5 +232,5 @@ function RelationEditor({ cell, success }: EditorOpts) {hideAllButtons: true}}noteIdChanged={success}- />+ />;}diff --git a/apps/server/src/services/handlers.ts b/apps/server/src/services/handlers.tsindex b76417554e..8a7d6d12f3 100644--- a/apps/server/src/services/handlers.ts+++ b/apps/server/src/services/handlers.ts@@ -1,3 +1,4 @@+import { DefinitionObject } from "@triliumnext/commons";import { type AbstractBeccaEntity, events as eventService } from "@triliumnext/core";import becca from "../becca/becca.js";@@ -6,7 +7,6 @@ import type BNote from "../becca/entities/bnote.js";import hiddenSubtreeService from "./hidden_subtree.js";import noteService from "./notes.js";import oneTimeTimer from "./one_time_timer.js";-import type { DefinitionObject } from "./promoted_attribute_definition_interface.js";import scriptService from "./script.js";import treeService from "./tree.js";diff --git a/apps/server/src/services/promoted_attribute_definition_interface.ts b/apps/server/src/services/promoted_attribute_definition_interface.tsdeleted file mode 100644index 2f68aa7ac1..0000000000--- a/apps/server/src/services/promoted_attribute_definition_interface.ts+++ /dev/null@@ -1,8 +0,0 @@-export interface DefinitionObject {- isPromoted?: boolean;- labelType?: string;- multiplicity?: string;- numberPrecision?: number;- promotedAlias?: string;- inverseRelation?: string;-}diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.tsindex 17d8f14716..a26a8f2861 100644--- a/packages/commons/src/lib/server_api.ts+++ b/packages/commons/src/lib/server_api.ts@@ -298,3 +298,15 @@ export interface IconRegistry {}[]}[];}++export type LabelType = "text" | "number" | "boolean" | "date" | "datetime" | "time" | "url" | "color";+export type Multiplicity = "single" | "multi";++export interface DefinitionObject {+ isPromoted?: boolean;+ labelType?: LabelType;+ multiplicity?: Multiplicity;+ numberPrecision?: number;+ promotedAlias?: string;+ inverseRelation?: string;+}diff --git a/apps/server/src/services/promoted_attribute_definition_parser.ts b/packages/trilium-core/src/services/promoted_attribute_definition_parser.tssimilarity index 84%rename from apps/server/src/services/promoted_attribute_definition_parser.tsrename to packages/trilium-core/src/services/promoted_attribute_definition_parser.tsindex 630c57a729..bc9c66bbc0 100644--- a/apps/server/src/services/promoted_attribute_definition_parser.ts+++ b/packages/trilium-core/src/services/promoted_attribute_definition_parser.ts@@ -1,4 +1,4 @@-import type { DefinitionObject } from "./promoted_attribute_definition_interface.js";+import { DefinitionObject, LabelType, Multiplicity } from "@triliumnext/commons";function parse(value: string): DefinitionObject {const tokens = value.split(",").map((t) => t.trim());@@ -8,9 +8,9 @@ function parse(value: string): DefinitionObject {if (token === "promoted") {defObj.isPromoted = true;} else if (["text", "number", "boolean", "date", "datetime", "time", "url"].includes(token)) {- defObj.labelType = token;+ defObj.labelType = token as LabelType;} else if (["single", "multi"].includes(token)) {- defObj.multiplicity = token;+ defObj.multiplicity = token as Multiplicity;} else if (token.startsWith("precision")) {const chunks = token.split("=");