Skip to content

Build your personal knowledge base with Trilium Notes

refactor(attributes): port the attribute help row to Preact

Elian Dorane889466d1cf3
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.css b/apps/client/src/widgets/attribute_widgets/attribute_detail.css
index 4098560bfb..7774f86ae4 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.css
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.css
@@ -18,6 +18,11 @@
padding: 5px;
}
+.attr-help .attr-help-page-button {
+ margin-inline-start: 5px;
+ vertical-align: middle;
+}
+
.related-notes-container {
margin-top: 20px;
}
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
index 6337601051..d22361e5a4 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
@@ -19,8 +19,11 @@ import FormAutocomplete, { AUTOCOMPLETE_DROPDOWN_SELECTOR } from "../react/FormA
import FormCheckbox from "../react/FormCheckbox.jsx";
import FormSelect from "../react/FormSelect.jsx";
import FormTextBox, { FormTextBoxWithUnit } from "../react/FormTextBox.jsx";
+import HelpButton from "../react/HelpButton.jsx";
import NoteAutocomplete from "../react/NoteAutocomplete.jsx";
import NoteLink from "../react/NoteLink.jsx";
+import RawHtml from "../react/RawHtml.jsx";
+import { ATTR_HELP } from "./attr_help.js";
import { disposeReactWidget, renderReactWidgetAtElement } from "../react/react_utils.jsx";
export interface AttributeDetailOpts {
@@ -350,6 +353,8 @@ function AttributeForm({ opts, attrType, currentNoteId, onAttributesChanged, onS
</td>
</tr>
+ <AttributeNameHelp attrType={attrType} name={name} />
+
{attrType === "relation" && (
<tr class="attr-row-target-note">
<th title={t("attribute_detail.target_note_title")}>{t("attribute_detail.target_note")}</th>
@@ -528,6 +533,32 @@ function AttributeForm({ opts, attrType, currentNoteId, onAttributesChanged, onS
);
}
+/**
+ * Describes what a well-known attribute does, for the attribute currently being edited. Renders
+ * nothing for names Trilium attaches no meaning to, which is most of them.
+ */
+function AttributeNameHelp({ attrType, name }: { attrType: AttrType; name: string }) {
+ const entry = attrType ? ATTR_HELP[attrType]?.[name] : undefined;
+
+ if (!entry) {
+ return null;
+ }
+
+ const { description, helpPage } = typeof entry === "string" ? { description: entry, helpPage: undefined } : entry;
+
+ return (
+ <tr class="attr-help">
+ {/* Descriptions carry markup of their own, e.g. <code> or a list of possible values. */}
+ <td colSpan={2}>
+ <strong>{name}</strong>
+ {" - "}
+ <RawHtml html={description} />
+ {helpPage && <HelpButton className="attr-help-page-button" helpPage={helpPage} />}
+ </td>
+ </tr>
+ );
+}
+
/** Constant so it does not re-initialise the autocomplete on every render. */
const TARGET_NOTE_OPTS = { allowCreatingNotes: true };
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 0134e4879a..aecb5a7c84 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail_old.ts
@@ -1,40 +1,23 @@
-import type { Attribute } from "../../services/attribute_parser.js";
import { t } from "../../services/i18n.js";
import linkService from "../../services/link.js";
-import { 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>
<div class="attr-is-owned-by">${t("attribute_detail.attr_is_owned_by")}</div>
-
- <table class="attr-edit-table">
- <tr class="attr-help"></tr>
- </table>
</div>`;
export default class AttributeDetailWidget extends NoteContextAwareWidget {
- private $inputName!: JQuery<HTMLElement>;
private $attrIsOwnedBy!: JQuery<HTMLElement>;
- private $attrHelp!: JQuery<HTMLElement>;
-
- private attrType!: ReturnType<AttributeDetailWidget["getAttrType"]>;
doRender() {
this.$widget = $(TPL);
- this.$inputName = this.$widget.find(".attr-input-name");
-
this.$attrIsOwnedBy = this.$widget.find(".attr-is-owned-by");
-
- this.$attrHelp = this.$widget.find(".attr-help");
}
- async showAttributeDetail({ allAttributes, attribute, isOwned, x, y, focus, hideMultiplicity }: AttributeDetailOpts) {
- this.attrType = this.getAttrType(attribute);
-
+ async showAttributeDetail({ attribute, isOwned }: AttributeDetailOpts) {
if (isOwned) {
this.$attrIsOwnedBy.hide();
} else if (attribute.noteId) {
@@ -46,58 +29,10 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget {
.append(await linkService.createLink(attribute.noteId));
}
- this.updateHelp();
-
this.toggleInt(true);
}
- updateHelp() {
- const attrName = String(this.$inputName.val());
-
- if (this.attrType && this.attrType in ATTR_HELP && attrName && attrName in ATTR_HELP[this.attrType]) {
- const entry = ATTR_HELP[this.attrType][attrName];
- const description = typeof entry === "string" ? entry : entry.description;
- const helpPage = typeof entry === "string" ? undefined : entry.helpPage;
-
- const $td = $("<td colspan=2>").append($("<strong>").text(attrName)).append(" - ").append(description);
-
- if (helpPage) {
- const $helpButton = $(`<button class="icon-action bx bx-help-circle" type="button" style="margin-left: 5px; vertical-align: middle;" />`);
- $helpButton.on("click", () => openInAppHelpFromUrl(helpPage));
- $td.append($helpButton);
- }
-
- this.$attrHelp
- .empty()
- .append($td)
- .show();
- } else {
- this.$attrHelp.empty().hide();
- }
- }
-
- 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";
- }
- }
-
hide() {
this.toggleInt(false);
}
-
- createLink(noteId: string) {
- return $("<a>", {
- href: `#root/${noteId}`,
- class: "reference-link"
- });
- }
}