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): offer the relation kind as one of a definition's types
Elian Doran61197ce712ff
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex aa9302743f..9bbf38ab95 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -646,7 +646,8 @@"print_page_size": "When exporting to PDF, changes the size of the page. Supported values: <code>A0</code>, <code>A1</code>, <code>A2</code>, <code>A3</code>, <code>A4</code>, <code>A5</code>, <code>A6</code>, <code>Legal</code>, <code>Letter</code>, <code>Tabloid</code>, <code>Ledger</code>.","print_scale": "When exporting to PDF, changes the scale of the rendered content. Values range from 0.1 (10%) to 2 (200%), default is 1 (100%).","print_margins": "When exporting to PDF, sets page margins. Use <code>default</code>, <code>none</code>, <code>minimum</code>, or custom values as <code>top,right,bottom,left</code> in millimeters.",- "color_type": "Color"+ "color_type": "Color",+ "relation_type": "Relation"},"attribute_editor": {"help_text_body1": "To add label, just type e.g. <code>#rock</code> or if you want to add also value then e.g. <code>#year = 2020</code>",diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsxindex 3d86197a9b..eeb802bf91 100644--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx@@ -21,8 +21,8 @@ import NoteContextAwareWidget from "../note_context_aware_widget.js";import { Badge, BadgeWithDropdown } from "../react/Badge.jsx";import Button from "../react/Button.jsx";import FormAutocomplete, { AUTOCOMPLETE_DROPDOWN_SELECTOR } from "../react/FormAutocomplete.jsx";-import { FormDropdownDivider, FormListItem } from "../react/FormList.jsx";import FormDropdownList from "../react/FormDropdownList.jsx";+import { FormDropdownDivider, FormListItem } from "../react/FormList.jsx";import FormTextBox, { FormTextBoxWithUnit } from "../react/FormTextBox.jsx";import HelpTooltipButton from "../react/HelpTooltipButton.jsx";import NoteAutocomplete from "../react/NoteAutocomplete.jsx";@@ -282,12 +282,16 @@ interface AttributeFormCallbacks {* The editable part of the popup. Remounted per show (keyed on `showId`) so the field state is* simply seeded from the attribute instead of being synchronized to it on every change.*/-function AttributeForm({ opts, attrType, currentNoteId, onCancel, onAttributesChanged, onSaveAndClose, onDelete }: AttributeFormCallbacks & {+function AttributeForm({ opts, attrType: initialAttrType, currentNoteId, onCancel, onAttributesChanged, onSaveAndClose, onDelete }: AttributeFormCallbacks & {opts: AttributeDetailOpts;attrType: AttrType;currentNoteId?: string | null;}) {const { attribute, allAttributes, isOwned, focus } = opts;+ // Held rather than read from the attribute, which the popup can change the kind of: a definition+ // switched between holding a value and pointing at a note is renamed in place, and the spawning+ // widget the kind would otherwise be resolved by does not re-render the popup.+ const [ attrType, setAttrType ] = useState(initialAttrType);// Definitions describe the attribute they define, so they complete against its own type.const nameType = attrType === "relation" || attrType === "relation-definition" ? "relation" : "label";const suggestAttributeNames = useCallback((query: string) => fetchAttributeNames(nameType, query), [ nameType ]);@@ -350,15 +354,32 @@ function AttributeForm({ opts, attrType, currentNoteId, onCancel, onAttributesCh}/** Definitions keep their settings serialized in the value, so any change rebuilds the whole thing. */- function commitDefinition(changes: Partial<DefinitionObject>) {+ function commitDefinition(changes: Partial<DefinitionObject>, kind = attrType) {const newDefinition = { ...definition, ...changes };setDefinition(newDefinition);attribute.value = promotedAttributeDefinitionParser.serialize(- newDefinition, attrType === "label-definition" ? "label" : "relation");+ newDefinition, kind === "label-definition" ? "label" : "relation");onAttributesChanged?.(allAttributes ?? []);}+ /**+ * What a definition sets up is a field, and pointing at a note is one more kind of field to the eye+ * that reads the form — so the two kinds of definition are offered as two values of one type rather+ * than as two things. What tells them apart underneath is the name they are stored under, `label:foo`+ * against `relation:foo`, and the settings the value carries: a label type against an inverse.+ */+ function commitDefinitionType(type: string) {+ const isRelation = type === RELATION_DEFINITION_TYPE;+ const newAttrType = isRelation ? "relation-definition" : "label-definition";++ setAttrType(newAttrType);+ attribute.name = addDefinitionPrefix(name, newAttrType);+ // The label type is kept in the definition while the popup is open, so switching to a relation+ // and back leaves the field as it was rather than back at the default.+ commitDefinition(isRelation ? {} : { labelType: type as LabelType }, newAttrType);+ }+// A relation stores the target note's id as its value; clearing the field clears the target.const commitTargetNote = useCallback((targetNoteId?: string) => {attribute.value = targetNoteId ?? "";@@ -447,17 +468,20 @@ function AttributeForm({ opts, attrType, currentNoteId, onCancel, onAttributesCh{/* No description: the values name themselves, and what they do to the field is plainenough once one is picked. */}- {attrType === "label-definition" && (+ {isDefinition(attrType) && (<OptionsRow name="attr-label-type" label={t("attribute_detail.label_type")}><FormDropdownListclassName="attr-input-label-type"- values={LABEL_TYPES}+ values={DEFINITION_TYPES}keyProperty="value"titleProperty="title"iconProperty="icon"- currentValue={definition.labelType ?? "text"}+ dividerBeforeProperty="startsGroup"+ currentValue={attrType === "relation-definition"+ ? RELATION_DEFINITION_TYPE+ : definition.labelType ?? "text"}disabled={!isOwned}- onChange={(labelType) => commitDefinition({ labelType: labelType as LabelType })}+ onChange={commitDefinitionType}/></OptionsRow>)}@@ -620,6 +644,12 @@ function lookupAttributeHelp(attrType: AttrType, name: string): AttrHelpEntry |/** Constant so it does not re-initialise the autocomplete on every render. */const TARGET_NOTE_OPTS = { allowCreatingNotes: true };+/**+ * The value standing for a definition that points at a note rather than holding a value of its own.+ * Not a label type: it is what the definition is named after, `relation:foo` rather than `label:foo`.+ */+export const RELATION_DEFINITION_TYPE = "relation";+/** Exported so that hosts listing definitions can name their label type as the popup does. */export const LABEL_TYPES = [{ value: "text", title: t("attribute_detail.text"), icon: "bx bx-text" },@@ -633,6 +663,21 @@ export const LABEL_TYPES = [{ value: "color", title: t("attribute_detail.color_type"), icon: "bx bx-palette" }];+/**+ * What a definition can set its field up to hold, the note it can point at instead included. The last+ * of them is set apart: the others only decide which field the value is typed into, while this one+ * decides what the attribute is, renaming it and leaving whatever was filled in under the old name.+ */+const DEFINITION_TYPES: { value: string; title: string; icon: string; startsGroup?: boolean }[] = [+ ...LABEL_TYPES,+ {+ value: RELATION_DEFINITION_TYPE,+ title: t("attribute_detail.relation_type"),+ icon: "bx bx-transfer",+ startsGroup: true+ }+];+function fetchAttributeNames(type: "label" | "relation", query: string) {return server.get<string[]>(`attribute-names/?type=${type}&query=${encodeURIComponent(query)}`);}diff --git a/apps/client/src/widgets/react/FormDropdownList.tsx b/apps/client/src/widgets/react/FormDropdownList.tsxindex 6c381ce0b9..1feebc2657 100644--- a/apps/client/src/widgets/react/FormDropdownList.tsx+++ b/apps/client/src/widgets/react/FormDropdownList.tsx@@ -1,5 +1,7 @@+import { Fragment } from "preact";+import Dropdown, { DropdownProps } from "./Dropdown";-import { FormListItem } from "./FormList";+import { FormDropdownDivider, FormListItem } from "./FormList";import Icon from "./Icon";interface FormDropdownList<T> extends Omit<DropdownProps, "children"> {@@ -11,11 +13,16 @@ interface FormDropdownList<T> extends Omit<DropdownProps, "children"> {descriptionProperty?: keyof T;/** Property holding the icon of an item, e.g. `bx bx-calendar`. Shown on the items and on the button. */iconProperty?: keyof T;+ /**+ * Property marking an item as starting a group of its own, drawing a divider above it. For a list+ * whose entries are not all the same sort of choice, e.g. one that changes more than the others do.+ */+ dividerBeforeProperty?: keyof T;currentValue: string;onChange(newValue: string): void;}-export default function FormDropdownList<T>({ values, keyProperty, titleProperty, titleSuffixProperty, descriptionProperty, iconProperty, currentValue, onChange, ...restProps }: FormDropdownList<T>) {+export default function FormDropdownList<T>({ values, keyProperty, titleProperty, titleSuffixProperty, descriptionProperty, iconProperty, dividerBeforeProperty, currentValue, onChange, ...restProps }: FormDropdownList<T>) {const currentValueData = values.find(value => value[keyProperty] === currentValue);const renderTitle = (item: T) => {@@ -38,19 +45,22 @@ export default function FormDropdownList<T>({ values, keyProperty, titleProperty{values.map(item => {const isCurrent = currentValue === item[keyProperty];return (- <FormListItem- key={item[keyProperty] as string}- icon={iconProperty ? item[iconProperty] as string : undefined}- onClick={() => onChange(item[keyProperty] as string)}- // The check mark takes the place of the item's icon, so where the items carry one- // the current value is marked by highlighting the item instead of by a mark.- checked={!iconProperty && isCurrent}- active={!!iconProperty && isCurrent}- description={descriptionProperty && item[descriptionProperty] as string}- selected={isCurrent}- >- {renderTitle(item)}- </FormListItem>+ <Fragment key={item[keyProperty] as string}>+ {dividerBeforeProperty && !!item[dividerBeforeProperty] && <FormDropdownDivider />}++ <FormListItem+ icon={iconProperty ? item[iconProperty] as string : undefined}+ onClick={() => onChange(item[keyProperty] as string)}+ // The check mark takes the place of the item's icon, so where the items carry+ // one the current value is marked by highlighting the item instead of by a mark.+ checked={!iconProperty && isCurrent}+ active={!!iconProperty && isCurrent}+ description={descriptionProperty && item[descriptionProperty] as string}+ selected={isCurrent}+ >+ {renderTitle(item)}+ </FormListItem>+ </Fragment>);})}</Dropdown>