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
feat(bulk_actions): complete label values
Elian Doranc7b8e7bfe7df
diff --git a/apps/client/src/widgets/bulk_actions/BulkAction.tsx b/apps/client/src/widgets/bulk_actions/BulkAction.tsxindex f4fc64099d..4c2ad6bb3b 100644--- a/apps/client/src/widgets/bulk_actions/BulkAction.tsx+++ b/apps/client/src/widgets/bulk_actions/BulkAction.tsx@@ -6,6 +6,7 @@ import { useCallback, useEffect, useRef } from "preact/hooks";import { t } from "../../services/i18n";import { AttributeNameSuggestion, fetchAttributeNames } from "../attribute_widgets/attribute_detail";+import { useLabelValueSuggestions } from "../attribute_widgets/label_value_input";import FormAutocomplete from "../react/FormAutocomplete";import { useSpacedUpdate } from "../react/hooks";import HelpRemoveButtons from "../react/HelpRemoveButtons";@@ -89,3 +90,23 @@ export function BulkActionAttributeName({ type, currentValue, onChange, placehol/>);}++/** A row's label value field, completing against the values the label `name` already has. */+export function BulkActionLabelValue({ name, currentValue, onChange, placeholder }: {+ name: string;+ currentValue: string;+ onChange(newValue: string): void;+ placeholder: string;+}) {+ const suggestValues = useLabelValueSuggestions(name);++ return (+ <FormAutocomplete+ placeholder={placeholder}+ currentValue={currentValue}+ onChange={onChange}+ source={suggestValues}+ openOnFocus+ />+ );+}diff --git a/apps/client/src/widgets/bulk_actions/label/add_label.tsx b/apps/client/src/widgets/bulk_actions/label/add_label.tsxindex 46f95d2f1a..24fda9426c 100644--- a/apps/client/src/widgets/bulk_actions/label/add_label.tsx+++ b/apps/client/src/widgets/bulk_actions/label/add_label.tsx@@ -1,8 +1,9 @@import { useState } from "preact/hooks";import { t } from "../../../services/i18n";-import FormTextBox from "../../react/FormTextBox";import AbstractBulkAction, { ActionDefinition } from "../abstract_bulk_action";-import BulkAction, { BulkActionArrow, BulkActionAttributeName, useSaveAction } from "../BulkAction";+import BulkAction, {+ BulkActionArrow, BulkActionAttributeName, BulkActionLabelValue, useSaveAction+} from "../BulkAction";function AddLabelBulkActionComponent({ bulkAction, actionDef }: { bulkAction: AbstractBulkAction, actionDef: ActionDefinition }) {const [ labelName, setLabelName ] = useState<string>(actionDef.labelName ?? "");@@ -20,7 +21,8 @@ function AddLabelBulkActionComponent({ bulkAction, actionDef }: { bulkAction: AbcurrentValue={labelName} onChange={setLabelName}/><BulkActionArrow />- <FormTextBox+ <BulkActionLabelValue+ name={labelName}placeholder={t("add_label.new_value_placeholder")}currentValue={labelValue} onChange={setLabelValue}/>diff --git a/apps/client/src/widgets/bulk_actions/label/update_label_value.tsx b/apps/client/src/widgets/bulk_actions/label/update_label_value.tsxindex 22214bb544..a50175cc28 100644--- a/apps/client/src/widgets/bulk_actions/label/update_label_value.tsx+++ b/apps/client/src/widgets/bulk_actions/label/update_label_value.tsx@@ -1,7 +1,8 @@import { t } from "../../../services/i18n.js";import AbstractBulkAction, { ActionDefinition } from "../abstract_bulk_action.js";-import FormTextBox from "../../react/FormTextBox.jsx";-import BulkAction, { BulkActionArrow, BulkActionAttributeName, useSaveAction } from "../BulkAction.jsx";+import BulkAction, {+ BulkActionArrow, BulkActionAttributeName, BulkActionLabelValue, useSaveAction+} from "../BulkAction.jsx";import { useState } from "preact/hooks";function UpdateLabelValueComponent({ bulkAction, actionDef }: { bulkAction: AbstractBulkAction, actionDef: ActionDefinition}) {@@ -20,7 +21,8 @@ function UpdateLabelValueComponent({ bulkAction, actionDef }: { bulkAction: AbstcurrentValue={labelName} onChange={setLabelName}/><BulkActionArrow />- <FormTextBox+ <BulkActionLabelValue+ name={labelName}placeholder={t("update_label_value.new_value_placeholder")}currentValue={labelValue} onChange={setLabelValue}/>diff --git a/apps/client/src/widgets/dialogs/bulk_actions.spec.tsx b/apps/client/src/widgets/dialogs/bulk_actions.spec.tsxindex f9fdc67532..ce1d78afad 100644--- a/apps/client/src/widgets/dialogs/bulk_actions.spec.tsx+++ b/apps/client/src/widgets/dialogs/bulk_actions.spec.tsx@@ -81,6 +81,19 @@ describe("BulkActionsDialog", () => {return [ ...container.querySelectorAll<HTMLInputElement>(".bulk-existing-action-list input.bulk-action-name") ];}+ /** Types `value` into `input`, lets the debounced lookup run and returns the suggestion rows. */+ async function typeAndSuggest(input: HTMLInputElement | undefined, value: string) {+ if (!input) throw new Error("No such field.");+ await act(async () => {+ input.value = value;+ input.dispatchEvent(new Event("input", { bubbles: true }));+ });+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 200));+ });+ return [ ...document.querySelectorAll<HTMLElement>(".form-autocomplete-dropdown li") ];+ }+it("shows the first selected notes as chips and the rest on demand", async () => {const noteIds = [ "n1", "n2", "n3", "n4", "n5", "n6", "n7" ];await open(noteIds);@@ -292,22 +305,28 @@ describe("BulkActionsDialog", () => {buildNote({ id: "_bulkAction", title: "Bulk action", "#action": JSON.stringify({ name: "deleteRelation" }) });await open([ "n1" ]);- const input = labelNameInputs()[0];- expect(input).toBeDefined();- await act(async () => {- input.value = "au";- input.dispatchEvent(new Event("input", { bubbles: true }));- });- await act(async () => {- await new Promise((resolve) => setTimeout(resolve, 200));- });- const suggestions = [ ...document.querySelectorAll<HTMLElement>(".form-autocomplete-dropdown li") ];+ const suggestions = await typeAndSuggest(labelNameInputs()[0], "au");expect(suggestions.map((li) => li.textContent)).toStrictEqual([ "author" ]);await act(async () => suggestions[0].click());expect(labelNameInputs()[0].value).toBe("author");});+ it("completes a label value against the values the label already has", async () => {+ server.get.mockImplementation(async (url) =>+ (url === "attribute-values/status" ? [ "done", "doing", "todo" ] : []));+ buildNote({+ id: "_bulkAction", title: "Bulk action",+ "#action": JSON.stringify({ name: "updateLabelValue", labelName: "status" })+ });+ await open([ "n1" ]);++ const valueInput = container.querySelector<HTMLInputElement>(+ ".bulk-existing-action-list .bulk-action-fields input:not(.bulk-action-name)");+ const suggestions = await typeAndSuggest(valueInput ?? undefined, "doi");+ expect(suggestions.map((li) => li.textContent)).toStrictEqual([ "doing" ]);+ });+it("saves an action only when its fields change", async () => {vi.useFakeTimers();try {diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Bulk Actions.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Bulk Actions.htmlindex 844bd7ea24..fb6ac8b4e9 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Bulk Actions.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Bulk Actions.html@@ -44,6 +44,8 @@<li>The fields for a label or relation name suggest the names already usedin the database as you type, marking the ones Trilium itself attaches ameaning to.</li>+ <li>The value field of <em>Add label</em> and <em>Update label value</em> suggests+ the values the named label already has in the database.</li></ul></li><li>When all the actions are defined, press the <em>Apply to N notes</em> buttondiff --git a/docs/User Guide/User Guide/Advanced Usage/Bulk Actions.md b/docs/User Guide/User Guide/Advanced Usage/Bulk Actions.mdindex 4a97c7ead3..ed5fca011f 100644--- a/docs/User Guide/User Guide/Advanced Usage/Bulk Actions.md+++ b/docs/User Guide/User Guide/Advanced Usage/Bulk Actions.md@@ -17,6 +17,7 @@ The _Bulk Actions_ dialog makes it easy to apply changes to multiple notes at on* To remove an action, simply press the X button to the right of it.* It is possible to apply multiple actions of the same type, such as adding multiple types.* The fields for a label or relation name suggest the names already used in the database as you type, marking the ones Trilium itself attaches a meaning to.+ * The value field of _Add label_ and _Update label value_ suggests the values the named label already has in the database.* When all the actions are defined, press the _Apply to N notes_ button to trigger all of them at once. The button names the number of notes that will be affected, and stays disabled until at least one action is added.* If an action is missing a field it needs, such as the name of the label to rename, the dialog does not run and points out the empty field instead.* For convenience, the last bulk action configuration is saved for further use and will be restored when entering the dialog again.