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): add a Preact autocomplete and use it for attribute names
Elian Dorand58b914ddab5
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsxindex 0dd57dab32..a53c9e9ff4 100644--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx@@ -1,15 +1,17 @@import "./attribute_detail.css";-import { useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";+import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";import type { Attribute } from "../../services/attribute_parser.js";import { isExperimentalFeatureEnabled } from "../../services/experimental_features.js";import { focusSavedElement, saveFocusedElement } from "../../services/focus.js";import { t } from "../../services/i18n.js";+import server from "../../services/server.js";import { isIMEComposing } from "../../services/shortcuts.js";import utils from "../../services/utils.js";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";@@ -170,7 +172,7 @@ function AttributeDetail({ opts, showId, parentOffset, onDismiss, onCancel, ...f// attribute or close; dismissing here first would hide and immediately// re-show the popup, which reads as a flicker.|| spawner?.contains(e.target)- || e.target.closest(".algolia-autocomplete, #context-menu-container")) {+ || e.target.closest(`${AUTOCOMPLETE_DROPDOWN_SELECTOR}, .algolia-autocomplete, #context-menu-container`)) {return;}onDismiss();@@ -238,6 +240,11 @@ function AttributeForm({ opts, attrType, onAttributesChanged, onSaveAndClose, onattrType: AttrType;}) {const { attribute, allAttributes, isOwned, focus } = opts;+ // Definitions describe the attribute they define, so they complete against its own type.+ const suggestAttributeNames = useCallback((query: string) => {+ const type = attrType === "relation" || attrType === "relation-definition" ? "relation" : "label";+ return server.get<string[]>(`attribute-names/?type=${type}&query=${encodeURIComponent(query)}`);+ }, [ attrType ]);const [ name, setName ] = useState(() => stripDefinitionPrefix(attribute.name, attrType));const [ value, setValue ] = useState(attribute.value ?? "");const [ isInheritable, setIsInheritable ] = useState(!!attribute.isInheritable);@@ -277,11 +284,13 @@ function AttributeForm({ opts, attrType, onAttributesChanged, onSaveAndClose, on<tr title={t("attribute_detail.attr_name_title")}><th>{t("attribute_detail.name")}</th><td>- <FormTextBox+ <FormAutocompleteclassName="attr-input-name"inputRef={nameRef}currentValue={name}readOnly={!isOwned}+ source={suggestAttributeNames}+ openOnFocusonChange={(newName) => isComposing.current ? setName(newName) : commitName(newName)}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.tsindex 0c9b153af8..fc68f2b6a6 100644--- a/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts@@ -146,15 +146,6 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {this.$widget = $(TPL);this.$inputName = this.$widget.find(".attr-input-name");- this.$inputName.on("autocomplete:closed", () => this.userEditedAttribute());-- this.$inputName.on("focus", () => {- attributeAutocompleteService.initAttributeNameAutocomplete({- $el: this.$inputName,- attributeType: () => (["relation", "relation-definition"].includes(this.attrType || "") ? "relation" : "label"),- open: true- });- });this.$inputValue = this.$widget.find(".attr-input-value");this.$inputValue.on("autocomplete:closed", () => this.userEditedAttribute());diff --git a/apps/client/src/widgets/react/FormAutocomplete.css b/apps/client/src/widgets/react/FormAutocomplete.cssnew file mode 100644index 0000000000..1377b2130f--- /dev/null+++ b/apps/client/src/widgets/react/FormAutocomplete.css@@ -0,0 +1,26 @@+.form-autocomplete-dropdown {+ position: fixed;+ z-index: 2000;+ margin: 0;+ padding: 0;+ list-style: none;+ overflow-y: auto;+ background-color: var(--main-background-color);+ border: 1px solid var(--main-border-color);+ border-radius: 4px;+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);+}++.form-autocomplete-dropdown .form-autocomplete-item {+ cursor: pointer;+ padding: 6px 16px;+ margin: 0;+ white-space: nowrap;+ overflow: hidden;+ text-overflow: ellipsis;+}++.form-autocomplete-dropdown .form-autocomplete-item.active {+ color: var(--active-item-text-color);+ background-color: var(--active-item-background-color);+}diff --git a/apps/client/src/widgets/react/FormAutocomplete.tsx b/apps/client/src/widgets/react/FormAutocomplete.tsxnew file mode 100644index 0000000000..14b861908c--- /dev/null+++ b/apps/client/src/widgets/react/FormAutocomplete.tsx@@ -0,0 +1,226 @@+import "./FormAutocomplete.css";++import type { TargetedKeyboardEvent } from "preact";+import { createPortal } from "preact/compat";+import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";++import FormTextBox from "./FormTextBox";++/** Marks the dropdown, which is portalled to the body: popups checking for outside clicks must ignore it. */+export const AUTOCOMPLETE_DROPDOWN_SELECTOR = ".form-autocomplete-dropdown";++const DEBOUNCE_MS = 150;+const MAX_DROPDOWN_HEIGHT = 300;+/** Below this the space under the input is considered unusable, and the dropdown flips above it. */+const MIN_DROPDOWN_HEIGHT = 120;+const VIEWPORT_MARGIN = 8;++type FormTextBoxProps = Parameters<typeof FormTextBox>[0];++interface FormAutocompleteProps extends Omit<FormTextBoxProps, "onChange"> {+ currentValue: string;+ onChange(newValue: string): void;+ /**+ * Provides the suggestions for a query. Called debounced while the dropdown is open; results+ * arriving after a newer query has been issued are discarded, so it may resolve out of order.+ */+ source(query: string): Promise<string[]>;+ /** Opens the dropdown as soon as the field receives focus, not just on typing. */+ openOnFocus?: boolean;+}++/**+ * A text box with a suggestion dropdown, driven by an async {@link FormAutocompleteProps.source}.+ *+ * The dropdown is portalled to the body and positioned over everything else, so it is not clipped+ * by scrolling ancestors. Selecting a suggestion reports it through `onChange`, exactly like typing.+ */+export default function FormAutocomplete({ currentValue, onChange, source, openOnFocus, inputRef, onBlur, onKeyDown, ...restProps }: FormAutocompleteProps) {+ const ownInputRef = useRef<HTMLInputElement>(null);+ const inputEl = inputRef ?? ownInputRef;+ const dropdownRef = useRef<HTMLUListElement>(null);+ const [ isOpen, setIsOpen ] = useState(false);+ const [ items, setItems ] = useState<string[]>([]);+ const [ activeIndex, setActiveIndex ] = useState(-1);+ const [ position, setPosition ] = useState<DropdownPosition>();++ // Discards responses of queries that were superseded while in flight.+ const latestQuery = useRef(0);+ const isDisabled = !!(restProps.readOnly || restProps.disabled);++ const close = useCallback(() => {+ // Invalidates in-flight queries too, so a late response cannot repopulate a closed dropdown.+ latestQuery.current++;+ setIsOpen(false);+ setActiveIndex(-1);+ setItems([]);+ }, []);++ // Fetch suggestions for the current query, debounced. The previous items stay visible while+ // the request is in flight, so refining a query does not make the dropdown flicker.+ useEffect(() => {+ if (!isOpen) {+ return;+ }++ const queryId = ++latestQuery.current;+ const timeout = setTimeout(async () => {+ const suggestions = await source(currentValue);++ // A newer query (or a close) happened while awaiting.+ if (latestQuery.current === queryId) {+ setItems(suggestions);+ setActiveIndex(-1);+ }+ }, DEBOUNCE_MS);++ return () => clearTimeout(timeout);+ }, [ isOpen, currentValue, source ]);++ // Keep the dropdown glued to the input.+ useLayoutEffect(() => {+ if (!isOpen || !items.length) {+ return;+ }++ const reposition = () => {+ if (inputEl.current) {+ setPosition(computeDropdownPosition(inputEl.current));+ }+ };++ reposition();+ window.addEventListener("resize", reposition);+ // Capture: the input may live inside a scrolling container rather than the document.+ window.addEventListener("scroll", reposition, true);+ return () => {+ window.removeEventListener("resize", reposition);+ window.removeEventListener("scroll", reposition, true);+ };+ }, [ isOpen, items.length, inputEl ]);++ function selectItem(item: string) {+ onChange(item);+ close();+ inputEl.current?.focus();+ }++ function handleKeyDown(e: TargetedKeyboardEvent<HTMLInputElement>) {+ const isDropdownShown = isOpen && items.length > 0;++ switch (e.key) {+ case "ArrowDown":+ case "ArrowUp":+ e.preventDefault();+ if (!isDropdownShown) {+ setIsOpen(true);+ } else {+ const delta = e.key === "ArrowDown" ? 1 : -1;+ setActiveIndex((index) => (index + delta + items.length) % items.length);+ }+ break;++ case "Enter":+ if (isDropdownShown && activeIndex >= 0) {+ // Consume the key so it does not also reach the surrounding form or dialog.+ e.preventDefault();+ e.stopPropagation();+ selectItem(items[activeIndex]);+ }+ break;++ case "Escape":+ if (isDropdownShown) {+ // Only dismiss the dropdown; a surrounding popup keeps its own Escape handling.+ e.stopPropagation();+ close();+ }+ break;++ case "Tab":+ close();+ break;+ }++ onKeyDown?.(e);+ }++ return (+ <>+ <FormTextBox+ {...restProps}+ inputRef={inputEl}+ currentValue={currentValue}+ onChange={(newValue) => {+ onChange(newValue);+ if (!isDisabled) {+ setIsOpen(true);+ }+ }}+ onFocus={openOnFocus && !isDisabled ? () => setIsOpen(true) : undefined}+ onBlur={(newValue) => {+ close();+ onBlur?.(newValue);+ }}+ onKeyDown={handleKeyDown}+ role="combobox"+ aria-expanded={isOpen && items.length > 0}+ aria-autocomplete="list"+ />++ {isOpen && items.length > 0 && position && createPortal(+ <ul+ ref={dropdownRef}+ className="form-autocomplete-dropdown"+ role="listbox"+ style={position}+ // Keeps the input focused, so the blur handler does not close the dropdown+ // before the click lands on an item.+ onMouseDown={(e) => e.preventDefault()}+ >+ {items.map((item, index) => (+ <li+ key={item}+ className={`form-autocomplete-item ${index === activeIndex ? "active" : ""}`}+ role="option"+ aria-selected={index === activeIndex}+ onMouseEnter={() => setActiveIndex(index)}+ onClick={() => selectItem(item)}+ >+ {item}+ </li>+ ))}+ </ul>,+ document.body)}+ </>+ );+}++interface DropdownPosition {+ left: number;+ top: number;+ width: number;+ maxHeight: number;+}++/**+ * Places the dropdown under the input, flipping above it only when the space below is too small to+ * be useful. Preferring below matters for inputs sitting in a panel docked to the bottom of the+ * screen: the room under them is limited but ample, while flipping would cover the panel itself.+ */+function computeDropdownPosition(input: HTMLElement): DropdownPosition {+ const rect = input.getBoundingClientRect();+ const viewportHeight = document.documentElement.clientHeight;+ const spaceBelow = viewportHeight - rect.bottom - VIEWPORT_MARGIN;+ const spaceAbove = rect.top - VIEWPORT_MARGIN;+ const flipAbove = spaceBelow < MIN_DROPDOWN_HEIGHT && spaceAbove > spaceBelow;+ const available = flipAbove ? spaceAbove : spaceBelow;+ const maxHeight = Math.min(MAX_DROPDOWN_HEIGHT, Math.max(available, 0));++ return {+ left: rect.left,+ top: flipAbove ? rect.top - maxHeight : rect.bottom,+ width: rect.width,+ maxHeight+ };+}