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
refactor(attributes): clear the selection from the select-all checkbox
Elian Dorana4c8f565af90
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 103ae19d9d..059712825c 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -901,7 +901,6 @@"paste_other": "Paste {{count}} attributes","selected_one": "{{count}} selected","selected_other": "{{count}} selected",- "clear_selection": "Clear the selection","copied_one": "{{count}} attribute copied","copied_other": "{{count}} attributes copied","pasted_one": "{{count}} attribute pasted",diff --git a/apps/client/src/widgets/sidebar/AttributeList.mobile.spec.tsx b/apps/client/src/widgets/sidebar/AttributeList.mobile.spec.tsxindex 770b745c8c..b7fee2fb41 100644--- a/apps/client/src/widgets/sidebar/AttributeList.mobile.spec.tsx+++ b/apps/client/src/widgets/sidebar/AttributeList.mobile.spec.tsx@@ -150,7 +150,7 @@ describe("AttributeList on a phone", () => {expect(container.querySelector(".attribute-list-panel")?.className).toContain("selecting");// The toolbar above the cards turns over to the selection.- expect(iconsIn(toolbar())).toEqual([ "bx bx-x", "bx bx-copy", "bx bx-trash", "bx bx-plus" ]);+ expect(iconsIn(toolbar())).toEqual([ "bx bx-copy", "bx bx-trash", "bx bx-plus" ]);expect(toolbar()?.compareDocumentPosition(section("attributes-owned"))).toBe(Node.DOCUMENT_POSITION_FOLLOWING);});diff --git a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx b/apps/client/src/widgets/sidebar/AttributeList.spec.tsxindex fcf4e5c9ee..9370fa1e8e 100644--- a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx+++ b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx@@ -919,7 +919,7 @@ describe("AttributeList", () => {expect(document.querySelector(".attr-detail")).toBeNull();// With none picked out it is the one attribute it landed on again.- act(() => toolbarButton("bx bx-x")?.click());+ act(() => selectAllBox()?.click());act(() => rows()[1].click());expect(selectedNames()).toEqual([]);expect(document.querySelector(".attr-detail")).not.toBeNull();@@ -1135,46 +1135,47 @@ describe("AttributeList", () => {// Only that the count has a slot: the wording is translated, and translations stay unloaded.expect(toolbar?.querySelector(".attribute-selection-count")).not.toBeNull();expect(container.querySelector(".attribute-add-row")).not.toBeNull();- // Clearing, copying and deleting go ahead of adding, which stays at the end.- expect(toolbarIcons()).toEqual([ "bx bx-x", "bx bx-copy", "bx bx-trash", "bx bx-plus" ]);+ // Copying and deleting go ahead of adding, which stays at the end.+ expect(toolbarIcons()).toEqual([ "bx bx-copy", "bx bx-trash", "bx bx-plus" ]);// Copying holds the attributes, which puts pasting on offer, also at the end.await act(async () => toolbarButton("bx bx-copy")?.click());- expect(toolbarIcons()).toEqual([ "bx bx-x", "bx bx-copy", "bx bx-trash", "bx bx-paste", "bx bx-plus" ]);+ expect(toolbarIcons()).toEqual([ "bx bx-copy", "bx bx-trash", "bx bx-paste", "bx bx-plus" ]);// An inherited row is the source note's to delete, so the selection cannot be deleted.checkbox(3).click();expect(toolbarIcons()).not.toContain("bx bx-trash");- // Clearing the selection leaves pasting and adding where they were.- act(() => toolbarButton("bx bx-x")?.click());+ // Clearing the selection from the checkbox leaves pasting and adding where they were.+ act(() => selectAllBox()?.click());expect(selectedNames()).toEqual([]);expect(container.querySelector(".attribute-selection-count")).toBeNull();expect(toolbarIcons()).toEqual([ "bx bx-paste", "bx bx-plus" ]);expect(firstPanel().className).not.toContain("selecting");});- it("selects every row from the toolbar's checkbox, and clears the selection from it once all are", () => {+ it("selects every row from the toolbar's checkbox with none selected, and clears any selection from it", () => {renderPanel(noteWithAttributes());- const box = container.querySelector<HTMLInputElement>(".attribute-toolbar .form-checkbox input");- expect(box).not.toBeNull();+ const box = selectAllBox();expect(box?.checked).toBe(false);expect(box?.indeterminate).toBe(false);- // Some rows selected: the box shows the mixed state, and a click selects the rest.- checkbox(0).click();- expect(box?.indeterminate).toBe(true);- expect(box?.checked).toBe(false);-act(() => box?.click());expect(selectedNames()).toEqual([ "author", "cssClass", "template", "inheritedLabel", "archived", "priority", "status" ]);expect(box?.checked).toBe(true);- expect(box?.indeterminate).toBe(false);- // All rows selected: a click clears the selection.act(() => box?.click());expect(selectedNames()).toEqual([]);expect(box?.checked).toBe(false);++ // Some rows selected: the box shows the mixed state, and a click clears rather than selecting+ // the rest, though the browser checks the box on that click.+ checkbox(0).click();+ expect(box?.indeterminate).toBe(true);+ act(() => box?.click());+ expect(selectedNames()).toEqual([]);+ expect(box?.checked).toBe(false);+ expect(box?.indeterminate).toBe(false);});it("lets a note that cannot be edited be copied from, but not deleted from or pasted onto", async () => {@@ -1187,10 +1188,10 @@ describe("AttributeList", () => {expect(container.querySelector(".attribute-toolbar .attribute-toolbar-read-only")).not.toBeNull();expect(toolbarIcons()).toEqual([]);- // The toolbar offers clearing and copying only: no paste, delete or add.+ // The toolbar offers copying only: no paste, delete or add.checkbox(0).click();checkbox(1).click();- expect(toolbarIcons()).toEqual([ "bx bx-x", "bx bx-copy" ]);+ expect(toolbarIcons()).toEqual([ "bx bx-copy" ]);// The count takes the notice's place while rows are selected.expect(container.querySelector(".attribute-toolbar-read-only")).toBeNull();@@ -1291,6 +1292,12 @@ describe("AttributeList", () => {`.attribute-toolbar .icon-action.${icon.split(" ").join(".")}`);}+ function selectAllBox() {+ const box = container.querySelector<HTMLInputElement>(".attribute-toolbar .form-checkbox input");+ expect(box).not.toBeNull();+ return box;+ }+function selectedNames() {return [ ...container.querySelectorAll(".attribute-row.selected .attribute-name") ].map((name) => name.textContent);diff --git a/apps/client/src/widgets/sidebar/AttributeList.tsx b/apps/client/src/widgets/sidebar/AttributeList.tsxindex 642b5f649c..7b5d22325f 100644--- a/apps/client/src/widgets/sidebar/AttributeList.tsx+++ b/apps/client/src/widgets/sidebar/AttributeList.tsx@@ -1597,8 +1597,8 @@ function getClipboardValue(attribute: Attribute) {* rows are selected, it also holds the selection's count and actions. Its height stays the same* either way, so starting or ending a selection moves nothing.*- * The checkbox's label is "Select all" with nothing selected and the count otherwise. It selects every- * row unless all are selected already, in which case it clears the selection.+ * The checkbox's label is "Select all" with nothing selected and the count otherwise. A click selects+ * every row when none is selected, and clears the selection otherwise.** Paste and add stay at the trailing end in both states, and the selection's actions go before them.* Deleting is offered only when every selected row is the note's own. On a note that cannot be edited, a@@ -1635,16 +1635,8 @@ function AttributeToolbar({ selectionCount, selectableCount, readOnly, held, can: t("attribute_list_panel.select_all")}currentValue={allSelected}indeterminate={isSelecting && !allSelected}- // A click on a partly checked box checks it, so `checked` alone tells the two actions apart.- onChange={(checked) => checked ? onSelectAll() : onClearSelection()}- />- )}-- {isSelecting && (- <ActionButton- icon="bx bx-x"- text={t("attribute_list_panel.clear_selection")}- onClick={onClearSelection}+ // Decided by the selection, not by `checked`: a click on a partly checked box checks it.+ onChange={() => isSelecting ? onClearSelection() : onSelectAll()}/>)}