Skip to content

Build your personal knowledge base with Trilium Notes

refactor(attributes): port the label value autocomplete to Preact

Elian Doran7256a19fe7d6
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
index a53c9e9ff4..34eaacc5f4 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
@@ -13,7 +13,6 @@ 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 FormTextBox from "../react/FormTextBox.jsx";
import { disposeReactWidget, renderReactWidgetAtElement } from "../react/react_utils.jsx";
export interface AttributeDetailOpts {
@@ -249,6 +248,24 @@ function AttributeForm({ opts, attrType, onAttributesChanged, onSaveAndClose, on
const [ value, setValue ] = useState(attribute.value ?? "");
const [ isInheritable, setIsInheritable ] = useState(!!attribute.isInheritable);
const nameRef = useRef<HTMLInputElement>(null);
+ // The values known for a label name never change while the popup is open, so they are fetched
+ // once per name and filtered locally afterwards.
+ const knownValues = useRef<{ name: string; values: string[] }>();
+ const suggestLabelValues = useCallback(async (query: string) => {
+ if (!name.trim()) {
+ return [];
+ }
+
+ if (knownValues.current?.name !== name) {
+ knownValues.current = {
+ name,
+ values: await server.get<string[]>(`attribute-values/${encodeURIComponent(name)}`)
+ };
+ }
+
+ const term = query.toLowerCase();
+ return knownValues.current.values.filter((value) => value.toLowerCase().includes(term));
+ }, [ name ]);
// Committing mid-composition makes the spawning editor re-render and swallow the
// characters being composed: https://github.com/zadam/trilium/pull/3812
const isComposing = useRef(false);
@@ -305,10 +322,12 @@ function AttributeForm({ opts, attrType, onAttributesChanged, onSaveAndClose, on
<tr class="attr-row-value">
<th>{t("attribute_detail.value")}</th>
<td>
- <FormTextBox
+ <FormAutocomplete
className="attr-input-value"
currentValue={value}
readOnly={!isOwned}
+ source={suggestLabelValues}
+ openOnFocus
onChange={(newValue) => isComposing.current ? setValue(newValue) : commitValue(newValue)}
onCompositionStart={() => isComposing.current = true}
onCompositionEnd={(e) => {
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 fc68f2b6a6..2f10e6de0a 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts
@@ -1,5 +1,4 @@
import appContext from "../../components/app_context.js";
-import attributeAutocompleteService from "../../services/attribute_autocomplete.js";
import type { Attribute } from "../../services/attribute_parser.js";
import froca from "../../services/froca.js";
import { t } from "../../services/i18n.js";
@@ -113,7 +112,6 @@ interface SearchRelatedResponse {
export default class AttributeDetailWidget extends NoteContextAwareWidget {
private $inputName!: JQuery<HTMLElement>;
- private $inputValue!: JQuery<HTMLElement>;
private $rowPromoted!: JQuery<HTMLElement>;
private $inputPromoted!: JQuery<HTMLElement>;
private $inputPromotedAlias!: JQuery<HTMLElement>;
@@ -147,16 +145,6 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {
this.$inputName = this.$widget.find(".attr-input-name");
- this.$inputValue = this.$widget.find(".attr-input-value");
- this.$inputValue.on("autocomplete:closed", () => this.userEditedAttribute());
- this.$inputValue.on("focus", () => {
- attributeAutocompleteService.initLabelValueAutocomplete({
- $el: this.$inputValue,
- open: true,
- nameCallback: () => String(this.$inputName.val())
- });
- });
-
this.$rowPromoted = this.$widget.find(".attr-row-promoted");
this.$inputPromoted = this.$widget.find(".attr-input-promoted");
this.$inputPromoted.on("change", () => this.userEditedAttribute());