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
fix(attributes): no deleting or pasting on a note that cannot be edited
Elian Doranc62351fb57a1
diff --git a/apps/client/src/widgets/sidebar/AttributeList.mobile.spec.tsx b/apps/client/src/widgets/sidebar/AttributeList.mobile.spec.tsxindex 55b7d959ec..ea66c8c3f7 100644--- a/apps/client/src/widgets/sidebar/AttributeList.mobile.spec.tsx+++ b/apps/client/src/widgets/sidebar/AttributeList.mobile.spec.tsx@@ -124,6 +124,13 @@ describe("AttributeList on a phone", () => {const [ url, saved ] = put.mock.calls[0] as [ string, { name: string; value: string }[] ];expect(url).toBe("notes/phone-bare/attributes");expect(saved).toEqual([ { type: "label", name: "author", value: "Elian", isInheritable: false } ]);++ // A note that cannot be edited has no paste button, even with attributes held.+ render(null, container);+ const locked = buildNote({ id: "phone-locked", title: "Locked" });+ locked.isContentWritable = false;+ renderPanel(locked);+ expect(iconsIn(header("attributes-owned"))).toEqual([ "bx bx-help-circle" ]);});it("still picks rows out, by the press held down, and says what can be done with them", () => {diff --git a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx b/apps/client/src/widgets/sidebar/AttributeList.spec.tsxindex d531c5abe4..b05234150f 100644--- a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx+++ b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx@@ -1141,6 +1141,37 @@ describe("AttributeList", () => {expect(firstPanel().className).not.toContain("selecting");});+ it("lets a note that cannot be edited be copied from, but not deleted from or pasted onto", async () => {+ const note = noteWithAttributes();+ note.isContentWritable = false;+ writeAttributes(null, [ plain("tag") ]);+ renderPanel(note);++ // The selection bar offers copying and the way out only.+ checkbox(0).click();+ checkbox(1).click();+ expect(barIcons()).toEqual([ "bx bx-copy", "bx bx-x" ]);++ // So does the row menu, over rows the note owns.+ rightPress(0);+ expect(menuItem("bx bx-copy")).toBeDefined();+ expect(menuItem("bx bx-trash")).toBeUndefined();+ expect(menuItem("bx bx-paste")).toBeUndefined();++ // A right press beside the rows has nothing to offer, so the browser's menu shows.+ showContextMenu.mockClear();+ const event = new MouseEvent("contextmenu", { bubbles: true, cancelable: true });+ act(() => {+ firstPanel().dispatchEvent(event);+ });+ expect(showContextMenu).not.toHaveBeenCalled();+ expect(event.defaultPrevented).toBe(false);++ // The paste key saves nothing.+ await act(async () => pasteInto(firstPanel(), { "text/plain": "#tag=new" }));+ expect(put).not.toHaveBeenCalled();+ });+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.tsxindex 3cd4f2aedf..07a875443d 100644--- a/apps/client/src/widgets/sidebar/AttributeList.tsx+++ b/apps/client/src/widgets/sidebar/AttributeList.tsx@@ -51,6 +51,7 @@ import SidebarHelp from "./SidebarHelp";*/export default function AttributeList() {const { note } = useActiveNoteContext();+ const canEdit = !!note && note.isContentWritable;const { isMasterDetail } = useMasterDetail();const parentComponent = useContext(ParentComponent);const containerRef = useRef<HTMLDivElement>(null);@@ -314,7 +315,6 @@ export default function AttributeList() {}const sections = splitIntoSections(owned.current, inherited.current);- const canEdit = !!note && note.isContentWritable;const internalRows = internal.current.map((attribute) => toEntry(attribute, true));//#region Selection and the clipboard@@ -433,7 +433,7 @@ export default function AttributeList() {* from either lands here whole, as does one written out by hand.*/async function pasteAttributes(e: ClipboardEvent) {- if (!note) {+ if (!canEdit) {return;}e.preventDefault();@@ -518,7 +518,7 @@ export default function AttributeList() {...pasteMenuItems()];- if (picked.every((candidate) => owned.current.includes(candidate))) {+ if (canEdit && picked.every((candidate) => owned.current.includes(candidate))) {items.push({ kind: "separator" });items.push({title: t("attribute_list_panel.delete_selection", { count: picked.length }),@@ -553,7 +553,7 @@ export default function AttributeList() {*/function pasteMenuItems(): MenuItem<never>[] {const heldAttributes = getHeldAttributes();- if (heldAttributes.length === 0 || !note) {+ if (heldAttributes.length === 0 || !canEdit) {return [];}@@ -744,8 +744,8 @@ export default function AttributeList() {const selectionBar = note && isSelecting && (<AttributeSelectionBarcount={selection.size}- canDelete={selectedAttributes().every((candidate) => owned.current.includes(candidate))}- canPaste={heldAttributes.length > 0}+ canDelete={canEdit && selectedAttributes().every((candidate) => owned.current.includes(candidate))}+ canPaste={canEdit && heldAttributes.length > 0}onCopy={() => void copyPickedAttributes(selectedAttributes())}onPaste={() => void applyPaste(heldAttributes)}onDelete={() => void deleteSelection(selectedAttributes())}@@ -793,7 +793,7 @@ export default function AttributeList() {rows would land on is nothing at all on the note that most wantspasting into, having no rows to give it a height. The header is theone part of a run that stands whatever the run holds. */}- {heldAttributes.length > 0 && (+ {canEdit && heldAttributes.length > 0 && (<ActionButtonicon="bx bx-paste"text={t("attribute_list_panel.paste", { count: heldAttributes.length })}