Skip to content

Build your personal knowledge base with Trilium Notes

refactor(attributes): port the relation target note field to Preact

Elian Doran9eeeb16bd177
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
index e2fcb31522..d8b5ac4537 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
@@ -15,6 +15,7 @@ import NoteContextAwareWidget from "../note_context_aware_widget.js";
import Button from "../react/Button.jsx";
import FormAutocomplete, { AUTOCOMPLETE_DROPDOWN_SELECTOR } from "../react/FormAutocomplete.jsx";
import FormCheckbox from "../react/FormCheckbox.jsx";
+import NoteAutocomplete from "../react/NoteAutocomplete.jsx";
import NoteLink from "../react/NoteLink.jsx";
import { disposeReactWidget, renderReactWidgetAtElement } from "../react/react_utils.jsx";
@@ -301,6 +302,12 @@ function AttributeForm({ opts, attrType, currentNoteId, onAttributesChanged, onS
onAttributesChanged(allAttributes ?? []);
}
+ // 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 ?? "";
+ onAttributesChanged(allAttributes ?? []);
+ }, [ attribute, allAttributes, onAttributesChanged ]);
+
return (
<>
<table class="attr-edit-table">
@@ -325,6 +332,20 @@ function AttributeForm({ opts, attrType, currentNoteId, onAttributesChanged, onS
</td>
</tr>
+ {attrType === "relation" && (
+ <tr class="attr-row-target-note">
+ <th title={t("attribute_detail.target_note_title")}>{t("attribute_detail.target_note")}</th>
+ <td>
+ <NoteAutocomplete
+ noteId={attribute.value || undefined}
+ readOnly={!isOwned}
+ opts={TARGET_NOTE_OPTS}
+ noteIdChanged={commitTargetNote}
+ />
+ </td>
+ </tr>
+ )}
+
{attrType === "label" && (
<tr class="attr-row-value">
<th>{t("attribute_detail.value")}</th>
@@ -389,6 +410,9 @@ function AttributeForm({ opts, attrType, currentNoteId, onAttributesChanged, onS
);
}
+/** Constant so it does not re-initialise the autocomplete on every render. */
+const TARGET_NOTE_OPTS = { allowCreatingNotes: true };
+
const DISPLAYED_NOTES = 10;
/** Edits keep arriving while typing, so the lookup waits for a pause instead of running per keystroke. */
const RELATED_NOTES_DEBOUNCE_MS = 1000;
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts b/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts
index 48dc7aef6b..96fbf6a922 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts
@@ -1,8 +1,6 @@
import type { Attribute } from "../../services/attribute_parser.js";
-import froca from "../../services/froca.js";
import { t } from "../../services/i18n.js";
import linkService from "../../services/link.js";
-import noteAutocompleteService from "../../services/note_autocomplete.js";
import promotedAttributeDefinitionParser from "../../services/promoted_attribute_definition_parser.js";
import utils, { openInAppHelpFromUrl } from "../../services/utils.js";
import NoteContextAwareWidget from "../note_context_aware_widget.js";
@@ -15,14 +13,6 @@ const TPL = /*html*/`
<table class="attr-edit-table">
<tr class="attr-help"></tr>
- <tr class="attr-row-target-note">
- <th title="${t("attribute_detail.target_note_title")}">${t("attribute_detail.target_note")}</th>
- <td>
- <div class="input-group">
- <input type="text" class="attr-input-target-note form-control" />
- </div>
- </td>
- </tr>
<tr class="attr-row-promoted"
title="${t("attribute_detail.promoted_title")}">
<th></th>
@@ -94,13 +84,11 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {
private $inputMultiplicity!: JQuery<HTMLElement>;
private $inputInverseRelation!: JQuery<HTMLElement>;
private $inputLabelType!: JQuery<HTMLElement>;
- private $inputTargetNote!: JQuery<HTMLElement>;
private $inputNumberPrecision!: JQuery<HTMLElement>;
private $rowMultiplicity!: JQuery<HTMLElement>;
private $rowLabelType!: JQuery<HTMLElement>;
private $rowNumberPrecision!: JQuery<HTMLElement>;
private $rowInverseRelation!: JQuery<HTMLElement>;
- private $rowTargetNote!: JQuery<HTMLElement>;
private $rowPromotedAlias!: JQuery<HTMLElement>;
private $attrIsOwnedBy!: JQuery<HTMLElement>;
private $attrHelp!: JQuery<HTMLElement>;
@@ -143,21 +131,6 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {
}
});
- this.$rowTargetNote = this.$widget.find(".attr-row-target-note");
- this.$inputTargetNote = this.$widget.find(".attr-input-target-note");
-
- noteAutocompleteService.initNoteAutocomplete(this.$inputTargetNote, { allowCreatingNotes: true }).on("autocomplete:noteselected", (event, suggestion, dataset) => {
- if (!suggestion.notePath) {
- return false;
- }
-
- const pathChunks = suggestion.notePath.split("/");
-
- this.attribute.value = pathChunks[pathChunks.length - 1]; // noteId
-
- this.triggerCommand("updateAttributeList", { attributes: this.allAttributes ?? [] });
- });
-
this.$attrIsOwnedBy = this.$widget.find(".attr-is-owned-by");
this.$attrHelp = this.$widget.find(".attr-help");
@@ -184,8 +157,6 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {
const disabledFn = () => (!isOwned ? "true" : undefined);
- this.$rowTargetNote.toggle(this.attrType === "relation");
-
this.$rowPromoted.toggle(["label-definition", "relation-definition"].includes(this.attrType || ""));
this.$inputPromoted.prop("checked", !!definition.isPromoted).attr("disabled", disabledFn);
@@ -204,18 +175,6 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {
this.$rowInverseRelation.toggle(this.attrType === "relation-definition");
this.$inputInverseRelation.val(definition.inverseRelation || "").attr("disabled", disabledFn);
- if (attribute.type === "relation") {
- this.$inputTargetNote.attr("readonly", disabledFn).val("").setSelectedNotePath("");
-
- if (attribute.value) {
- const targetNote = await froca.getNote(attribute.value);
-
- if (targetNote) {
- this.$inputTargetNote.val(targetNote ? targetNote.title : "").setSelectedNotePath(attribute.value);
- }
- }
- }
-
this.updateHelp();
this.toggleInt(true);
@@ -268,8 +227,6 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {
updateAttributeInEditor() {
if (this.attrType?.endsWith("-definition")) {
this.attribute.value = this.buildDefinitionValue();
- } else if (this.attrType === "relation") {
- this.attribute.value = this.$inputTargetNote.getSelectedNoteId() || "";
}
this.triggerCommand("updateAttributeList", { attributes: this.allAttributes ?? [] });
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsx
index fb96969401..6fc83bd059 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx
@@ -19,9 +19,11 @@ interface NoteAutocompleteProps {
onBlur?: (newValue: string) => void;
noteIdChanged?: (noteId: string) => void;
noteId?: string;
+ /** Shows the selected note without allowing a different one to be picked. */
+ readOnly?: boolean;
}
-export default function NoteAutocomplete({ id, inputRef: externalInputRef, text, placeholder, onChange, onTextChange, container, containerStyle, opts, noteId, noteIdChanged, onKeyDown, onBlur }: NoteAutocompleteProps) {
+export default function NoteAutocomplete({ id, inputRef: externalInputRef, text, placeholder, onChange, onTextChange, container, containerStyle, opts, noteId, noteIdChanged, onKeyDown, onBlur, readOnly }: NoteAutocompleteProps) {
const ref = useSyncedRef<HTMLInputElement>(externalInputRef);
useEffect(() => {
@@ -103,6 +105,7 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,
id={id}
ref={ref}
className="note-autocomplete form-control"
+ readOnly={readOnly}
placeholder={placeholder ?? t("add_link.search_note")} />
</div>
);