Skip to content

Build your personal knowledge base with Trilium Notes

feat(attributes): show the attribute clipboard in the panel header

Elian Doran70e1fa6a1d5e
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 3762f6cf34..3fddf8a6f0 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -910,7 +910,10 @@
"paste_failed": "The clipboard could not be read as attributes: {{- message}}",
"delete_selection_one": "Delete attribute",
"delete_selection_other": "Delete {{count}} attributes",
- "delete_selection_confirm": "Delete the {{count}} selected attributes?"
+ "delete_selection_confirm": "Delete the {{count}} selected attributes?",
+ "clear_clipboard": "Clear clipboard",
+ "to_copy_one": "{{count}} attribute to copy",
+ "to_copy_other": "{{count}} attributes to copy"
},
"attribute_names": {
"system": "System",
diff --git a/apps/client/src/widgets/sidebar/AttributeList.mobile.spec.tsx b/apps/client/src/widgets/sidebar/AttributeList.mobile.spec.tsx
index 7c8e16a89f..ba1f1254ba 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.mobile.spec.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeList.mobile.spec.tsx
@@ -113,7 +113,12 @@ describe("AttributeList on a phone", () => {
expect(container.querySelector(".attribute-selection-bar")).toBeNull();
expect(iconsIn(header("attributes-owned"))).toEqual([ "bx bx-help-circle", "bx bx-paste", "bx bx-plus" ]);
- const paste = header("attributes-owned")?.querySelector<HTMLElement>(".bx-paste");
+ // The button opens the clipboard menu, whose Paste row pastes.
+ const toggle = header("attributes-owned")?.querySelector<HTMLElement>(".attribute-clipboard > button");
+ expect(toggle).not.toBeNull();
+ act(() => toggle?.click());
+ const paste = document.querySelector(".tn-dropdown-portal .bx-paste")?.closest<HTMLElement>("[role=menuitem]");
+ expect(paste).toBeTruthy();
await act(async () => paste?.click());
const [ url, saved ] = put.mock.calls[0] as [ string, { name: string; value: string }[] ];
diff --git a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
index f7c88d7a1a..61e847c617 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
@@ -59,7 +59,7 @@ vi.mock("../react/NoteAutocomplete", async () => {
import appContext from "../../components/app_context";
import type Component from "../../components/component";
import FAttribute, { FAttributeRow } from "../../entities/fattribute";
-import { writeAttributes } from "../../services/attribute_clipboard";
+import { getHeldAttributes, holdAttributes, writeAttributes } from "../../services/attribute_clipboard";
import type { Attribute } from "../../services/attribute_parser";
import froca from "../../services/froca";
import type LoadResults from "../../services/load_results";
@@ -1167,11 +1167,67 @@ describe("AttributeList", () => {
expect(showContextMenu).not.toHaveBeenCalled();
expect(event.defaultPrevented).toBe(false);
- // The paste key saves nothing.
+ // The paste key saves nothing, and the header has no clipboard dropdown.
await act(async () => pasteInto(firstPanel(), { "text/plain": "#tag=new" }));
expect(put).not.toHaveBeenCalled();
+ expect(clipboardToggle()).toBeNull();
});
+ it("shows the copied attributes in the header, to paste onto another note or clear", async () => {
+ holdAttributes([]);
+ renderPanel(buildNote({ id: "receiver", title: "Receiver" }));
+ expect(clipboardToggle()).toBeNull();
+
+ render(null, container);
+ buildNote({ id: "clip-target", title: "Target note" });
+ holdAttributes([
+ { type: "label", name: "author", value: "Elian", isInheritable: false },
+ { type: "relation", name: "template", value: "clip-target", isInheritable: false }
+ ]);
+ renderPanel(buildNote({ id: "receiver", title: "Receiver" }));
+
+ // The menu lists what is held as disabled rows: the kind icon, the name, and the value
+ // beneath it, a relation's target by its title.
+ act(() => clipboardToggle()?.click());
+ const heldRows = [ ...document.querySelectorAll(".tn-dropdown-portal [role=menuitem][aria-disabled=true]") ];
+ expect(heldRows.map((row) => {
+ const description = row.querySelector(".description")?.textContent ?? "";
+ return [
+ row.querySelector(".tn-icon")?.classList.contains("bx-transfer") ? "relation" : "label",
+ (row.textContent ?? "").replace(description, ""),
+ description
+ ];
+ })).toEqual([ [ "label", "author", "Elian" ], [ "relation", "template", "Target note" ] ]);
+
+ await act(async () => clipboardItem("bx bx-paste")?.click());
+ const [ url, saved ] = put.mock.calls[0] as [ string, Attribute[] ];
+ expect(url).toBe("notes/receiver/attributes");
+ expect(saved).toMatchObject([
+ { type: "label", name: "author", value: "Elian" },
+ { type: "relation", name: "template", value: "clip-target" }
+ ]);
+ // Pasting keeps what is held, so it can go onto another note too.
+ expect(clipboardToggle()).not.toBeNull();
+
+ // Clearing lets it go, and the dropdown with it.
+ act(() => clipboardToggle()?.click());
+ act(() => clipboardItem("bx bx-x")?.click());
+ expect(getHeldAttributes()).toEqual([]);
+ expect(clipboardToggle()).toBeNull();
+ });
+
+ function clipboardToggle() {
+ return container.querySelector<HTMLElement>(".attribute-clipboard > button");
+ }
+
+ /** A row of the open clipboard menu, named by its icon since translations stay unloaded. */
+ function clipboardItem(icon: string) {
+ const item = document.querySelector(`.tn-dropdown-portal .${icon.split(" ").join(".")}`)
+ ?.closest<HTMLElement>("[role=menuitem]");
+ expect(item).toBeTruthy();
+ return item;
+ }
+
function rows() {
return [ ...container.querySelectorAll<HTMLElement>(".attribute-row") ];
}
diff --git a/apps/client/src/widgets/sidebar/AttributeList.tsx b/apps/client/src/widgets/sidebar/AttributeList.tsx
index e619666bc5..edb0a3bcc9 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeList.tsx
@@ -13,6 +13,7 @@ import { copyAttributesToClipboard, getHeldAttributes, holdAttributes, mergePast
import type { Attribute } from "../../services/attribute_parser";
import attributes, { isBuiltinAttribute } from "../../services/attributes";
import dialog from "../../services/dialog";
+import froca from "../../services/froca";
import { t } from "../../services/i18n";
import type { SidebarSectionId } from "../../services/in_app_help";
import server from "../../services/server";
@@ -23,7 +24,8 @@ import { attributeKindIcon, RELATION_DEFINITION_TYPE } from "../attribute_widget
import { ColorChip, renderLabelValue } from "../attribute_widgets/label_value_display";
import ActionButton from "../react/ActionButton";
import SimpleBadge from "../react/Badge";
-import { FormListItem } from "../react/FormList";
+import Dropdown from "../react/Dropdown";
+import { FormDropdownDivider, FormListHeader, FormListItem } from "../react/FormList";
import { useActiveNoteContext, useTriliumEvent } from "../react/hooks";
import Icon from "../react/Icon";
import { DetailPane, MasterPane, useMasterDetail, useMasterDetailPage } from "../react/master_detail";
@@ -770,15 +772,15 @@ export default function AttributeList() {
<>
{run.help && <SidebarHelp section={run.help} />}
- {/* The only paste control that works on a phone and on a note with no
- attributes, which has no rows to right-click. */}
+ {/* The only paste control that is visible on every note, and the only
+ one on a phone or a note with no rows to right-click. */}
{index === 0 && canEdit && heldAttributes.length > 0 && (
- <ActionButton
- icon="bx bx-paste"
- text={t("attribute_list_panel.paste", { count: heldAttributes.length })}
- onClick={(e) => {
- e.stopPropagation();
- void applyPaste(heldAttributes);
+ <AttributeClipboardDropdown
+ held={heldAttributes}
+ onPaste={() => void applyPaste(heldAttributes)}
+ onClear={() => {
+ holdAttributes([]);
+ rerender();
}}
/>
)}
@@ -1539,6 +1541,59 @@ function AddAttributeButton({ text, attrTypes, onSelect }: {
);
}
+/**
+ * The attributes held for pasting, in the first card's header. The menu lists them, each with its kind
+ * icon and value, then pastes them onto the note or clears them.
+ */
+function AttributeClipboardDropdown({ held, onPaste, onClear }: {
+ held: Attribute[];
+ onPaste: () => void;
+ onClear: () => void;
+}) {
+ return (
+ <Dropdown
+ className="attribute-clipboard"
+ buttonClassName="bx bx-paste"
+ dropdownContainerClassName="attribute-clipboard-menu"
+ title={t("attribute_list_panel.to_copy", { count: held.length })}
+ iconAction
+ hideToggleArrow
+ noSelectButtonStyle
+ mobileBottomSheet={IS_MOBILE}
+ >
+ <FormListHeader text={t("attribute_list_panel.to_copy", { count: held.length })} />
+ {/* Disabled, so the menu's arrow keys pass over them to the actions. */}
+ {held.map((attribute, index) => (
+ <FormListItem
+ key={index}
+ disabled
+ icon={attributeKindIcon(attribute.type, attribute.name, attribute.value ?? "")}
+ description={getClipboardValue(attribute) || undefined}
+ >
+ {getDisplayName(attribute, getAttributeKind(attribute))}
+ </FormListItem>
+ ))}
+ <FormDropdownDivider />
+ <FormListItem icon="bx bx-paste" onClick={onPaste}>
+ {t("attribute_list_panel.paste", { count: held.length })}
+ </FormListItem>
+ <FormListItem icon="bx bx-x" onClick={onClear}>
+ {t("attribute_list_panel.clear_clipboard")}
+ </FormListItem>
+ </Dropdown>
+ );
+}
+
+/** A held relation's target by its title where froca has the note, since its id means nothing to a reader. */
+function getClipboardValue(attribute: Attribute) {
+ const value = attribute.value ?? "";
+ if (attribute.type === "relation") {
+ return froca.getNoteFromCache(value)?.title ?? value;
+ }
+
+ return value;
+}
+
/**
* What can be done to the rows picked out, at the foot of the card holding them: how many they are,
* and the three things there are to do with them. It stands where the add row stands and takes its