Skip to content

Build your personal knowledge base with Trilium Notes

client/ui/promoted attributes card: refine attribute editing, implement attribute deletion

Adorian Dorana69aef91a71f
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index d6f920d27a..522f197e18 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1387,6 +1387,8 @@
"promoted_attributes": "Promoted Attributes",
"create_attribute": "Create a new attribute",
"edit_attribute": "Edit this attribute",
+ "delete_attribute": "Delete this attribute",
+ "delete_confirmation": "Delete <strong>{{name}}</strong> and its values from every note in this collection? This cannot be undone.",
"shown_on_items": "Shown on the items",
"hidden_from_items": "Hidden from the items",
"unset-field-placeholder": "not set",
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.ts b/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.ts
index dee669572e..5c76f8f102 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.ts
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.ts
@@ -169,6 +169,8 @@ describe("attribute detail popup naming", () => {
expect(isSameShow(shown, opts({ attribute: { type: "relation", name: "author", value: "Tolkien" } }))).toBe(false);
expect(isSameShow(shown, opts({ ...shown, isOwned: false }))).toBe(false);
expect(isSameShow(shown, opts({ ...shown, focus: "name" }))).toBe(false);
+ // Which rows the form carries is part of what it would show.
+ expect(isSameShow(shown, opts({ ...shown, hideInheritance: true }))).toBe(false);
// ...but the very same request handed back is the host re-rendering around an untouched
// popup, which every keystroke does — rebuilding the form there costs the focus and, with a
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
index 6abcc36126..d3a2bcfe62 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
@@ -48,6 +48,11 @@ export interface AttributeDetailOpts {
*/
parent?: HTMLElement;
hideMultiplicity?: boolean;
+ /**
+ * Leaves out the inheritable and promoted toggles, for a host that decides both itself: a
+ * collection arranging what its items show, where every attribute is inheritable and promoted.
+ */
+ hideInheritance?: boolean;
/**
* Places the popup beside this element instead of at `x`/`y`. For hosts whose attributes are shown
* far from the note attributes pane the coordinates are otherwise resolved against, e.g. the
@@ -326,6 +331,7 @@ export function isSameShow(previous: AttributeDetailOpts | null, next: Attribute
return !next.focus
&& previous.isOwned === next.isOwned
&& previous.hideMultiplicity === next.hideMultiplicity
+ && previous.hideInheritance === next.hideInheritance
&& previous.attribute.noteId === next.attribute.noteId
&& previous.attribute.type === next.attribute.type
&& previous.attribute.name === next.attribute.name
@@ -699,20 +705,22 @@ export function AttributeForm({ opts, attrType: initialAttrType, currentNoteId,
/>
)}
- <OptionsRowWithToggle
- name="attr-inheritable"
- label={t("attribute_detail.inheritable")}
- description={t("attribute_detail.inheritable_title")}
- currentValue={isInheritable}
- disabled={!isOwned}
- onChange={(checked) => {
- setIsInheritable(checked);
- attribute.isInheritable = checked;
- onAttributesChanged?.(allAttributes ?? []);
- }}
- />
+ {!opts.hideInheritance && (
+ <OptionsRowWithToggle
+ name="attr-inheritable"
+ label={t("attribute_detail.inheritable")}
+ description={t("attribute_detail.inheritable_title")}
+ currentValue={isInheritable}
+ disabled={!isOwned}
+ onChange={(checked) => {
+ setIsInheritable(checked);
+ attribute.isInheritable = checked;
+ onAttributesChanged?.(allAttributes ?? []);
+ }}
+ />
+ )}
- {isDefinition(attrType) && (
+ {isDefinition(attrType) && !opts.hideInheritance && (
<OptionsRowWithToggle
name="attr-promoted"
label={t("attribute_detail.promoted")}
diff --git a/apps/client/src/widgets/collections/promoted_attributes.ts b/apps/client/src/widgets/collections/promoted_attributes.ts
index 453cb5c359..79790b1a96 100644
--- a/apps/client/src/widgets/collections/promoted_attributes.ts
+++ b/apps/client/src/widgets/collections/promoted_attributes.ts
@@ -116,7 +116,12 @@ export function renameAttributeInSubtree(
return executeBulkActions([ parentNoteId ], [ action ], { includeDescendants: true });
}
-/** Deletes an attribute from the collection and everything under it. */
+/**
+ * Deletes an attribute from the collection and everything under it.
+ *
+ * Quietly: the attribute leaving the list and the items is what reports it, and a toast over a
+ * dialog the reader is still working in reads as something else having happened.
+ */
export function deleteAttributeInSubtree(
parentNoteId: string, type: "label" | "relation", name: string
) {
@@ -124,5 +129,6 @@ export function deleteAttributeInSubtree(
? { name: "deleteLabel", labelName: name }
: { name: "deleteRelation", relationName: name };
- return executeBulkActions([ parentNoteId ], [ action ], { includeDescendants: true });
+ return executeBulkActions(
+ [ parentNoteId ], [ action ], { includeDescendants: true, silent: true });
}
diff --git a/apps/client/src/widgets/react/PromotedAttributesCard.css b/apps/client/src/widgets/react/PromotedAttributesCard.css
index ff13843f64..9720429585 100644
--- a/apps/client/src/widgets/react/PromotedAttributesCard.css
+++ b/apps/client/src/widgets/react/PromotedAttributesCard.css
@@ -9,7 +9,8 @@
white-space: nowrap;
}
- .promoted-attribute-edit {
+ .promoted-attribute-edit,
+ .promoted-attribute-delete {
flex-shrink: 0;
}
diff --git a/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx b/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx
index daaacdb4d1..1364bb7c27 100644
--- a/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx
+++ b/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx
@@ -16,6 +16,7 @@ vi.mock("../../services/i18n", () => ({ t: (key: string) => key }));
const mocks = vi.hoisted(() => ({
setLabel: vi.fn(),
removeOwned: vi.fn(async () => {}),
+ confirm: vi.fn(async () => true),
bulk: vi.fn(async () => {}),
/** The editor the card opens, which the test drives through the callbacks it was given. */
detail: { opts: null as AttributeDetailOpts | null, callbacks: {} as Record<string, Function> }
@@ -32,6 +33,8 @@ vi.mock("../../services/attributes", () => ({
vi.mock("../../services/bulk_action", () => ({ executeBulkActions: mocks.bulk }));
+vi.mock("../../services/dialog", () => ({ default: { confirm: mocks.confirm } }));
+
// The editor is tested where it lives; what the card answers for is what it hands over and what it
// writes once the editor reports a definition.
vi.mock("../attribute_widgets/attribute_detail", () => ({
@@ -74,6 +77,7 @@ describe("PromotedAttributesCard", () => {
settings = undefined;
defined = [ definition("label:dueDate", { alias: "Due" }), definition("label:owner") ];
mocks.detail.opts = null;
+ mocks.confirm.mockResolvedValue(true);
host = new Component();
container = document.body.appendChild(document.createElement("div"));
});
@@ -133,6 +137,9 @@ describe("PromotedAttributesCard", () => {
value: "promoted,single,text",
isInheritable: true
});
+ // Everything arranged here is inheritable and promoted, so neither is asked about.
+ expect(mocks.detail.opts?.hideInheritance).toBe(true);
+ expect(mocks.detail.opts?.hideMultiplicity).toBe(true);
// Portalled to the page, so it stands over the dialog the card is in.
expect(document.body.querySelector(".attribute-detail-stub")).toBeTruthy();
});
@@ -186,7 +193,7 @@ describe("PromotedAttributesCard", () => {
expect(mocks.bulk).toHaveBeenCalledWith(
[ "board1" ],
[ { name: "deleteLabel", labelName: "owner" } ],
- { includeDescendants: true });
+ { includeDescendants: true, silent: true });
expect(mocks.removeOwned).toHaveBeenCalledWith(NOTE, "label", "label:owner");
});
@@ -229,6 +236,39 @@ describe("PromotedAttributesCard", () => {
});
});
+ describe("deleting one", () => {
+ /** The values go with the definition, which the attributes panel does not do. */
+ it("asks, then takes the definition and its values away", async () => {
+ draw();
+
+ await act(async () => {
+ segments()[0].querySelector<HTMLElement>(".promoted-attribute-delete")?.click();
+ await flush();
+ });
+
+ expect(mocks.confirm).toHaveBeenCalledWith(
+ expect.stringContaining("promoted_attributes.delete_confirmation"));
+ expect(mocks.bulk).toHaveBeenCalledWith(
+ [ "board1" ],
+ [ { name: "deleteLabel", labelName: "dueDate" } ],
+ { includeDescendants: true, silent: true });
+ expect(mocks.removeOwned).toHaveBeenCalledWith(NOTE, "label", "label:dueDate");
+ });
+
+ it("leaves everything alone where the reader said no", async () => {
+ mocks.confirm.mockResolvedValue(false);
+ draw();
+
+ await act(async () => {
+ segments()[0].querySelector<HTMLElement>(".promoted-attribute-delete")?.click();
+ await flush();
+ });
+
+ expect(mocks.bulk).not.toHaveBeenCalled();
+ expect(mocks.removeOwned).not.toHaveBeenCalled();
+ });
+ });
+
describe("creating one", () => {
it("opens the editor on a new definition, named for the reader to change", () => {
draw();
@@ -238,6 +278,7 @@ describe("PromotedAttributesCard", () => {
expect(mocks.detail.opts?.attribute)
.toEqual({ type: "label", name: "label:myLabel", value: "promoted,single,text", isInheritable: true });
expect(mocks.detail.opts?.focus).toBe("name");
+ expect(mocks.detail.opts?.hideInheritance).toBe(true);
// Nothing is listed until the note reports what was made.
expect(names()).toEqual([ "Due", "owner" ]);
expect(stored).toEqual([]);
diff --git a/apps/client/src/widgets/react/PromotedAttributesCard.tsx b/apps/client/src/widgets/react/PromotedAttributesCard.tsx
index 2a95216de6..51b3fa392e 100644
--- a/apps/client/src/widgets/react/PromotedAttributesCard.tsx
+++ b/apps/client/src/widgets/react/PromotedAttributesCard.tsx
@@ -6,6 +6,7 @@ import { useCallback, useMemo, useRef, useState } from "preact/hooks";
import type FNote from "../../entities/fnote";
import type { Attribute } from "../../services/attribute_parser";
import attributes, { removeOwnedAttributesByNameOrType } from "../../services/attributes";
+import dialog from "../../services/dialog";
import { t } from "../../services/i18n";
import {
AttributeDetail, type AttributeDetailOpts
@@ -115,7 +116,10 @@ export default function PromotedAttributesCard({
x: event?.pageX ?? 0,
y: event?.pageY ?? 150,
focus: attribute ? undefined : "name",
- hideMultiplicity: true
+ hideMultiplicity: true,
+ // Everything arranged here is inheritable and promoted, which is what puts it on the
+ // items: the reader picks what an attribute holds, not how it reaches them.
+ hideInheritance: true
});
}, []);
@@ -150,18 +154,35 @@ export default function PromotedAttributesCard({
note.noteId, definition.name, definition.value, definition.isInheritable);
}, [ note ]);
- /** Takes the definition away, and with it the values written against it. */
- const remove = useCallback(async () => {
+ /**
+ * Takes the definition away, and with it the values written against it.
+ *
+ * Unlike deleting the definition from the attributes panel, which leaves every item still
+ * carrying its value under a name nothing describes any more.
+ */
+ const erase = useCallback(async (definitionName: string) => {
+ const [ type, name ] = definitionName.split(":", 2) as [ "label" | "relation", string ];
+ await deleteAttributeInSubtree(note.noteId, type, name);
+ await removeOwnedAttributesByNameOrType(note, "label", definitionName);
+ }, [ note ]);
+
+ /** Deletes the definition the editor was opened on. */
+ const removeEdited = useCallback(async () => {
const definition = original.current;
setDetail(null);
- if (!definition?.name.includes(":")) {
- return;
+ if (definition?.name.includes(":")) {
+ await erase(definition.name);
}
+ }, [ erase ]);
- const [ type, name ] = definition.name.split(":", 2) as [ "label" | "relation", string ];
- await deleteAttributeInSubtree(note.noteId, type, name);
- await removeOwnedAttributesByNameOrType(note, "label", definition.name);
- }, [ note ]);
+ /** Asks before deleting, since the values on the items go with the definition. */
+ const confirmErase = useCallback(async (attribute: PromotedAttribute) => {
+ const confirmed = await dialog.confirm(
+ t("promoted_attributes.delete_confirmation", { name: attribute.title }));
+ if (confirmed) {
+ await erase(attribute.definitionName);
+ }
+ }, [ erase ]);
return (
<>
@@ -190,6 +211,16 @@ export default function PromotedAttributesCard({
}}
/>
+ <ActionButton
+ className="promoted-attribute-delete"
+ icon="bx bx-trash"
+ text={t("promoted_attributes.delete_attribute")}
+ onClick={(event) => {
+ event.stopPropagation();
+ confirmErase(attribute);
+ }}
+ />
+
<FormToggle
currentValue={!attribute.hidden}
switchOnTooltip={t("promoted_attributes.shown_on_items")}
@@ -222,7 +253,7 @@ export default function PromotedAttributesCard({
onCancel={() => setDetail(null)}
onAttributesChanged={([ definition ]) => { edited.current = definition; }}
onSaveAndClose={save}
- onDelete={remove}
+ onDelete={removeEdited}
/>,
document.body)}
</>