Skip to content

Build your personal knowledge base with Trilium Notes

refactor(attributes): introduce Preact shell for attribute detail

Elian Doran9c514afe4184
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
new file mode 100644
index 0000000000..04ccf0f052
--- /dev/null
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
@@ -0,0 +1,76 @@
+import "./attribute_detail.css";
+
+import type { Attribute } from "../../services/attribute_parser.js";
+import NoteContextAwareWidget from "../note_context_aware_widget.js";
+import { disposeReactWidget, renderReactWidgetAtElement } from "../react/react_utils.jsx";
+
+export interface AttributeDetailOpts {
+ allAttributes?: Attribute[];
+ attribute: Attribute;
+ isOwned: boolean;
+ x: number;
+ y: number;
+ focus?: "name";
+ parent?: HTMLElement;
+ hideMultiplicity?: boolean;
+}
+
+/**
+ * Transitional adapter keeping the legacy widget contract (`showAttributeDetail()`/`hide()`)
+ * while the actual UI is progressively ported to the {@link AttributeDetail} Preact component.
+ * Will be removed once the consumers can render the component directly.
+ */
+export default class AttributeDetailWidget extends NoteContextAwareWidget {
+ private opts: AttributeDetailOpts | null = null;
+
+ doRender() {
+ // No initial renderComponent() here: doRender() runs synchronously inside the
+ // parent's render phase (via useLegacyWidget's useMemo), and a nested Preact
+ // render() there corrupts the outer component's hook state.
+ this.$widget = $("<div>");
+ }
+
+ async refresh() {
+ // switching note/tab should close the widget
+ this.hide();
+ }
+
+ async noteSwitched() {
+ this.hide();
+ }
+
+ showAttributeDetail(opts: AttributeDetailOpts) {
+ this.opts = opts;
+ this.renderComponent();
+ // The widget has no note context, so isEnabled() is falsy and render()
+ // stamps the container with hidden-int; visibility is driven manually,
+ // exactly like the legacy widget did.
+ this.toggleInt(true);
+ }
+
+ hide() {
+ this.opts = null;
+ this.renderComponent();
+ this.toggleInt(false);
+ }
+
+ cleanup() {
+ disposeReactWidget(this.$widget[0]);
+ }
+
+ private renderComponent() {
+ renderReactWidgetAtElement(this, <AttributeDetail opts={this.opts} />, this.$widget[0]);
+ }
+}
+
+function AttributeDetail({ opts }: { opts: AttributeDetailOpts | null }) {
+ if (!opts) {
+ return null;
+ }
+
+ return (
+ <div class="attr-detail tn-tool-dialog" style={{ left: opts.x, top: opts.y }}>
+ Hello world
+ </div>
+ );
+}
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.ts b/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts
similarity index 98%
rename from apps/client/src/widgets/attribute_widgets/attribute_detail.ts
rename to apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts
index d63df810fe..b1f43641af 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.ts
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts
@@ -1,5 +1,3 @@
-import "./attribute_detail.css";
-
import appContext from "../../components/app_context.js";
import attributeAutocompleteService from "../../services/attribute_autocomplete.js";
import type { Attribute } from "../../services/attribute_parser.js";
@@ -16,9 +14,10 @@ import SpacedUpdate from "../../services/spaced_update.js";
import utils, { openInAppHelpFromUrl } from "../../services/utils.js";
import NoteContextAwareWidget from "../note_context_aware_widget.js";
import { ATTR_HELP } from "./attr_help.js";
+import type { AttributeDetailOpts } from "./attribute_detail.jsx";
const TPL = /*html*/`
-<div class="attr-detail tn-tool-dialog">
+<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>
@@ -145,17 +144,6 @@ const ATTR_TITLES: Record<string, string> = {
"relation-definition": t("attribute_detail.relation_definition")
};
-interface AttributeDetailOpts {
- allAttributes?: Attribute[];
- attribute: Attribute;
- isOwned: boolean;
- x: number;
- y: number;
- focus?: "name";
- parent?: HTMLElement;
- hideMultiplicity?: boolean;
-}
-
interface SearchRelatedResponse {
// TODO: Deduplicate once we split client from server.
results: {
@@ -204,12 +192,6 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {
private allAttributes?: Attribute[];
private attrType!: ReturnType<AttributeDetailWidget["getAttrType"]>;
- async refresh() {
- // switching note/tab should close the widget
-
- this.hide();
- }
-
doRender() {
this.relatedNotesSpacedUpdate = new SpacedUpdate(async () => this.updateRelatedNotes(), 1000);
@@ -651,8 +633,4 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {
class: "reference-link"
});
}
-
- async noteSwitched() {
- this.hide();
- }
}