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
refactor(attributes): port popup header and click-to-dismiss to Preact
Elian Dorandf9ac985bb60
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.css b/apps/client/src/widgets/attribute_widgets/attribute_detail.cssindex 05ade1acd2..ad78cb3a83 100644--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.css+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.css@@ -36,6 +36,13 @@display: inline-block;}+.attr-detail-header {+ display: flex;+ justify-content: space-between;+ align-items: start;+ margin-bottom: 8px;+}+.close-attr-detail-button {font-size: x-large;cursor: pointer;diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsxindex 04ccf0f052..0c9ae15cd0 100644--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx@@ -1,6 +1,10 @@import "./attribute_detail.css";+import { useEffect, useRef } from "preact/hooks";+import type { Attribute } from "../../services/attribute_parser.js";+import { focusSavedElement, saveFocusedElement } from "../../services/focus.js";+import { t } from "../../services/i18n.js";import NoteContextAwareWidget from "../note_context_aware_widget.js";import { disposeReactWidget, renderReactWidgetAtElement } from "../react/react_utils.jsx";@@ -40,6 +44,14 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {}showAttributeDetail(opts: AttributeDetailOpts) {+ if (!opts.attribute) {+ // the attribute can be null at runtime, e.g. when the editor content fails to parse+ this.hide();+ return;+ }++ saveFocusedElement();+this.opts = opts;this.renderComponent();// The widget has no note context, so isEnabled() is falsy and render()@@ -58,19 +70,98 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {disposeReactWidget(this.$widget[0]);}+ private async cancelAndClose() {+ await this.triggerCommand("reloadAttributes");++ this.hide();++ focusSavedElement();+ }+private renderComponent() {- renderReactWidgetAtElement(this, <AttributeDetail opts={this.opts} />, this.$widget[0]);+ renderReactWidgetAtElement(+ this,+ <AttributeDetail+ opts={this.opts}+ onDismiss={() => this.hide()}+ onCancel={() => this.cancelAndClose()}+ />,+ this.$widget[0]);}}-function AttributeDetail({ opts }: { opts: AttributeDetailOpts | null }) {+interface AttributeDetailProps {+ opts: AttributeDetailOpts | null;+ /** Plain close, e.g. on click outside the popup. */+ onDismiss: () => void;+ /** Close discarding unsaved changes (close button, escape). */+ onCancel: () => void;+}++function AttributeDetail({ opts, onDismiss, onCancel }: AttributeDetailProps) {+ const popupRef = useRef<HTMLDivElement>(null);+ const shown = !!opts;++ // Dismiss on click outside the popup, except in floating UI logically belonging+ // to it (autocomplete dropdowns and context menus are appended to the body).+ // Unlike the legacy widget, the listener only exists while the popup is shown.+ useEffect(() => {+ if (!shown) {+ return;+ }++ const onMouseDown = (e: MouseEvent) => {+ if (!(e.target instanceof Element)+ || popupRef.current?.contains(e.target)+ || e.target.closest(".algolia-autocomplete, #context-menu-container")) {+ return;+ }+ onDismiss();+ };++ window.addEventListener("mousedown", onMouseDown);+ return () => window.removeEventListener("mousedown", onMouseDown);+ }, [ shown, onDismiss ]);+if (!opts) {return null;}+ const attrType = getAttrType(opts.attribute);+return (- <div class="attr-detail tn-tool-dialog" style={{ left: opts.x, top: opts.y }}>+ <div ref={popupRef} class="attr-detail tn-tool-dialog" style={{ left: opts.x, top: opts.y }}>+ <div class="attr-detail-header">+ <h5 class="attr-detail-title">{attrType ? ATTR_TITLES[attrType] : ""}</h5>++ <button+ class="close-attr-detail-button icon-action bx bx-x"+ title={t("attribute_detail.close_button_title")}+ onClick={onCancel}+ />+ </div>+Hello world</div>);}++const ATTR_TITLES: Record<string, string> = {+ label: t("attribute_detail.label"),+ "label-definition": t("attribute_detail.label_definition"),+ relation: t("attribute_detail.relation"),+ "relation-definition": t("attribute_detail.relation_definition")+};++function getAttrType(attribute: Attribute) {+ if (attribute.type === "label") {+ if (attribute.name.startsWith("label:")) {+ return "label-definition";+ } else if (attribute.name.startsWith("relation:")) {+ return "relation-definition";+ }+ return "label";+ } else if (attribute.type === "relation") {+ return "relation";+ }+}diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts b/apps/client/src/widgets/attribute_widgets/attribute_detail_old.tsindex b1f43641af..c685a471fb 100644--- a/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts@@ -2,7 +2,7 @@ import appContext from "../../components/app_context.js";import attributeAutocompleteService from "../../services/attribute_autocomplete.js";import type { Attribute } from "../../services/attribute_parser.js";import { isExperimentalFeatureEnabled } from "../../services/experimental_features.js";-import { focusSavedElement, saveFocusedElement } from "../../services/focus.js";+import { focusSavedElement } from "../../services/focus.js";import froca from "../../services/froca.js";import { t } from "../../services/i18n.js";import linkService from "../../services/link.js";@@ -18,12 +18,6 @@ import type { AttributeDetailOpts } from "./attribute_detail.jsx";const TPL = /*html*/`<div>- <div style="display: flex; justify-content: space-between; align-items: start; margin-bottom: 8px;">- <h5 class="attr-detail-title">${t("attribute_detail.attr_detail_title")}</h5>-- <button class="close-attr-detail-button icon-action bx bx-x" title="${t("attribute_detail.close_button_title")}"></button>- </div>-<div class="attr-is-owned-by">${t("attribute_detail.attr_is_owned_by")}</div><table class="attr-edit-table">@@ -137,13 +131,6 @@ const TPL = /*html*/`const DISPLAYED_NOTES = 10;-const ATTR_TITLES: Record<string, string> = {- label: t("attribute_detail.label"),- "label-definition": t("attribute_detail.label_definition"),- relation: t("attribute_detail.relation"),- "relation-definition": t("attribute_detail.relation_definition")-};-interface SearchRelatedResponse {// TODO: Deduplicate once we split client from server.results: {@@ -156,7 +143,6 @@ interface SearchRelatedResponse {const isNewLayout = isExperimentalFeatureEnabled("new-layout");export default class AttributeDetailWidget extends NoteContextAwareWidget {- private $title!: JQuery<HTMLElement>;private $inputName!: JQuery<HTMLElement>;private $inputValue!: JQuery<HTMLElement>;private $rowPromoted!: JQuery<HTMLElement>;@@ -177,7 +163,6 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {private $rowPromotedAlias!: JQuery<HTMLElement>;private $attrIsOwnedBy!: JQuery<HTMLElement>;private $attrSaveDeleteButtonContainer!: JQuery<HTMLElement>;- private $closeAttrDetailButton!: JQuery<HTMLElement>;private $saveAndCloseButton!: JQuery<HTMLElement>;private $deleteButton!: JQuery<HTMLElement>;private $relatedNotesContainer!: JQuery<HTMLElement>;@@ -200,8 +185,6 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {shortcutService.bindElShortcut(this.$widget, "ctrl+return", () => this.saveAndClose());shortcutService.bindElShortcut(this.$widget, "esc", () => this.cancelAndClose());- this.$title = this.$widget.find(".attr-detail-title");-this.$inputName = this.$widget.find(".attr-input-name");this.$inputName.on("input", (ev) => {if (!(ev.originalEvent as KeyboardEvent)?.isComposing) {@@ -286,9 +269,6 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {this.$inputInheritable = this.$widget.find(".attr-input-inheritable");this.$inputInheritable.on("change", () => this.userEditedAttribute());- this.$closeAttrDetailButton = this.$widget.find(".close-attr-detail-button");- this.$closeAttrDetailButton.on("click", () => this.cancelAndClose());-this.$attrIsOwnedBy = this.$widget.find(".attr-is-owned-by");this.$attrSaveDeleteButtonContainer = this.$widget.find(".attr-save-delete-button-container");@@ -313,33 +293,15 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {this.$relatedNotesTitle = this.$relatedNotesContainer.find(".related-notes-tile");this.$relatedNotesList = this.$relatedNotesContainer.find(".related-notes-list");this.$relatedNotesMoreNotes = this.$relatedNotesContainer.find(".related-notes-more-notes");-- $(window).on("mousedown", (e) => {- if (!$(e.target).closest(this.$widget[0]).length && !$(e.target).closest(".algolia-autocomplete").length && !$(e.target).closest("#context-menu-container").length) {- this.hide();- }- });}async showAttributeDetail({ allAttributes, attribute, isOwned, x, y, focus, hideMultiplicity }: AttributeDetailOpts) {- if (!attribute) {- this.hide();-- return;- }-- saveFocusedElement();-this.attrType = this.getAttrType(attribute);const attrName = this.attrType === "label-definition" ? attribute.name.substr(6) : this.attrType === "relation-definition" ? attribute.name.substr(9) : attribute.name;const definition = this.attrType?.endsWith("-definition") ? promotedAttributeDefinitionParser.parse(attribute.value || "") : {};- if (this.attrType) {- this.$title.text(ATTR_TITLES[this.attrType]);- }-this.allAttributes = allAttributes;this.attribute = attribute;@@ -557,8 +519,6 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {} else if (attribute.type === "relation") {return "relation";}- this.$title.text("");-}updateAttributeInEditor() {