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 and relation names
Elian Dorand0f76a17bcf2
diff --git a/apps/client/src/widgets/bulk_actions/BulkAction.tsx b/apps/client/src/widgets/bulk_actions/BulkAction.tsxindex 7e30ad8dfd..f4fc64099d 100644--- a/apps/client/src/widgets/bulk_actions/BulkAction.tsx+++ b/apps/client/src/widgets/bulk_actions/BulkAction.tsx@@ -2,12 +2,15 @@ import "./BulkAction.css";import { ComponentChildren } from "preact";import { memo } from "preact/compat";-import { useEffect, useRef } from "preact/hooks";+import { useCallback, useEffect, useRef } from "preact/hooks";+import { t } from "../../services/i18n";-import AbstractBulkAction, { ActionDefinition } from "./abstract_bulk_action";+import { AttributeNameSuggestion, fetchAttributeNames } from "../attribute_widgets/attribute_detail";+import FormAutocomplete from "../react/FormAutocomplete";import { useSpacedUpdate } from "../react/hooks";import HelpRemoveButtons from "../react/HelpRemoveButtons";import Icon from "../react/Icon";+import AbstractBulkAction, { ActionDefinition } from "./abstract_bulk_action";interface BulkActionProps {children?: ComponentChildren;@@ -59,3 +62,30 @@ export function useSaveAction(bulkAction: AbstractBulkAction, data: Partial<Actiexport function BulkActionArrow() {return <Icon icon="bx bx-right-arrow-alt" className="bulk-action-arrow" />;}++/** A row's label or relation name field, completing against the names already in use. */+export function BulkActionAttributeName({ type, currentValue, onChange, placeholder, title }: {+ type: "label" | "relation";+ currentValue: string | undefined;+ onChange(newValue: string): void;+ placeholder: string;+ title: string;+}) {+ const suggestNames = useCallback((query: string) => fetchAttributeNames(type, query), [ type ]);+ const renderSuggestion = useCallback(+ (name: string, query: string) => <AttributeNameSuggestion type={type} name={name} query={query} />, [ type ]);++ return (+ <FormAutocomplete+ className="bulk-action-name" required+ pattern="[\p{L}\p{N}_:]+"+ placeholder={placeholder}+ title={title}+ currentValue={currentValue ?? ""}+ onChange={onChange}+ source={suggestNames}+ renderItem={renderSuggestion}+ 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 0bf6318ebf..46f95d2f1a 100644--- a/apps/client/src/widgets/bulk_actions/label/add_label.tsx+++ b/apps/client/src/widgets/bulk_actions/label/add_label.tsx@@ -2,7 +2,7 @@ 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, useSaveAction } from "../BulkAction";+import BulkAction, { BulkActionArrow, BulkActionAttributeName, useSaveAction } from "../BulkAction";function AddLabelBulkActionComponent({ bulkAction, actionDef }: { bulkAction: AbstractBulkAction, actionDef: ActionDefinition }) {const [ labelName, setLabelName ] = useState<string>(actionDef.labelName ?? "");@@ -13,11 +13,10 @@ function AddLabelBulkActionComponent({ bulkAction, actionDef }: { bulkAction: Ab<BulkActionbulkAction={bulkAction}>- <FormTextBox+ <BulkActionAttributeName+ type="label"placeholder={t("add_label.label_name_placeholder")}- pattern="[\\p{L}\\p{N}_:]+"title={t("add_label.label_name_title")}- className="bulk-action-name" requiredcurrentValue={labelName} onChange={setLabelName}/><BulkActionArrow />diff --git a/apps/client/src/widgets/bulk_actions/label/delete_label.tsx b/apps/client/src/widgets/bulk_actions/label/delete_label.tsxindex 92531ec32a..83304b26d2 100644--- a/apps/client/src/widgets/bulk_actions/label/delete_label.tsx+++ b/apps/client/src/widgets/bulk_actions/label/delete_label.tsx@@ -1,8 +1,7 @@import { useState } from "preact/hooks";import { t } from "../../../services/i18n.js";-import FormTextBox from "../../react/FormTextBox.jsx";import AbstractBulkAction, { ActionDefinition } from "../abstract_bulk_action.js";-import BulkAction, { useSaveAction } from "../BulkAction.jsx";+import BulkAction, { BulkActionAttributeName, useSaveAction } from "../BulkAction.jsx";function DeleteLabelBulkActionComponent({ bulkAction, actionDef }: { bulkAction: AbstractBulkAction, actionDef: ActionDefinition}) {const [ labelName, setLabelName ] = useState<string>(actionDef.labelName ?? "");@@ -12,11 +11,10 @@ function DeleteLabelBulkActionComponent({ bulkAction, actionDef }: { bulkAction:<BulkActionbulkAction={bulkAction}>- <FormTextBox- pattern="[\\p{L}\\p{N}_:]+"- title={t("delete_label.label_name_title")}+ <BulkActionAttributeName+ type="label"placeholder={t("delete_label.label_name_placeholder")}- className="bulk-action-name" required+ title={t("delete_label.label_name_title")}currentValue={labelName} onChange={setLabelName}/></BulkAction>diff --git a/apps/client/src/widgets/bulk_actions/label/rename_label.tsx b/apps/client/src/widgets/bulk_actions/label/rename_label.tsxindex 44c22076e8..8521a023fb 100644--- a/apps/client/src/widgets/bulk_actions/label/rename_label.tsx+++ b/apps/client/src/widgets/bulk_actions/label/rename_label.tsx@@ -1,8 +1,7 @@import { useState } from "preact/hooks";import { t } from "../../../services/i18n.js";-import FormTextBox from "../../react/FormTextBox.jsx";import AbstractBulkAction, { ActionDefinition } from "../abstract_bulk_action.js";-import BulkAction, { BulkActionArrow, useSaveAction } from "../BulkAction.jsx";+import BulkAction, { BulkActionArrow, BulkActionAttributeName, useSaveAction } from "../BulkAction.jsx";function RenameLabelBulkActionComponent({ bulkAction, actionDef }: { bulkAction: AbstractBulkAction, actionDef: ActionDefinition}) {const [ oldLabelName, setOldLabelName ] = useState(actionDef.oldLabelName);@@ -13,21 +12,19 @@ function RenameLabelBulkActionComponent({ bulkAction, actionDef }: { bulkAction:<BulkActionbulkAction={bulkAction}>- <FormTextBox+ <BulkActionAttributeName+ type="label"placeholder={t("rename_label.old_name_placeholder")}- pattern="[\\p{L}\\p{N}_:]+"title={t("rename_label.name_title")}- className="bulk-action-name" requiredcurrentValue={oldLabelName} onChange={setOldLabelName}/><BulkActionArrow />- <FormTextBox+ <BulkActionAttributeName+ type="label"placeholder={t("rename_label.new_name_placeholder")}- pattern="[\\p{L}\\p{N}_:]+"title={t("rename_label.name_title")}- className="bulk-action-name" requiredcurrentValue={newLabelName} onChange={setNewLabelName}/></BulkAction>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 32d8ff2bbf..22214bb544 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,7 @@import { t } from "../../../services/i18n.js";import AbstractBulkAction, { ActionDefinition } from "../abstract_bulk_action.js";import FormTextBox from "../../react/FormTextBox.jsx";-import BulkAction, { BulkActionArrow, useSaveAction } from "../BulkAction.jsx";+import BulkAction, { BulkActionArrow, BulkActionAttributeName, useSaveAction } from "../BulkAction.jsx";import { useState } from "preact/hooks";function UpdateLabelValueComponent({ bulkAction, actionDef }: { bulkAction: AbstractBulkAction, actionDef: ActionDefinition}) {@@ -13,11 +13,10 @@ function UpdateLabelValueComponent({ bulkAction, actionDef }: { bulkAction: Abst<BulkActionbulkAction={bulkAction}>- <FormTextBox+ <BulkActionAttributeName+ type="label"placeholder={t("update_label_value.label_name_placeholder")}- pattern="[\\p{L}\\p{N}_:]+"title={t("update_label_value.label_name_title")}- className="bulk-action-name" requiredcurrentValue={labelName} onChange={setLabelName}/><BulkActionArrow />diff --git a/apps/client/src/widgets/bulk_actions/relation/add_relation.tsx b/apps/client/src/widgets/bulk_actions/relation/add_relation.tsxindex 7353959089..a0ddc628b5 100644--- a/apps/client/src/widgets/bulk_actions/relation/add_relation.tsx+++ b/apps/client/src/widgets/bulk_actions/relation/add_relation.tsx@@ -1,8 +1,7 @@import AbstractBulkAction, { ActionDefinition } from "../abstract_bulk_action.js";import { t } from "../../../services/i18n.js";-import BulkAction, { BulkActionArrow, useSaveAction } from "../BulkAction.jsx";+import BulkAction, { BulkActionArrow, BulkActionAttributeName, useSaveAction } from "../BulkAction.jsx";import NoteAutocomplete from "../../react/NoteAutocomplete.jsx";-import FormTextBox from "../../react/FormTextBox.jsx";import { useState } from "preact/hooks";function AddRelationBulkActionComponent({ bulkAction, actionDef }: { bulkAction: AbstractBulkAction, actionDef: ActionDefinition }) {@@ -14,11 +13,10 @@ function AddRelationBulkActionComponent({ bulkAction, actionDef }: { bulkAction:<BulkActionbulkAction={bulkAction}>- <FormTextBox+ <BulkActionAttributeName+ type="relation"placeholder={t("add_relation.relation_name")}- pattern="[\\p{L}\\p{N}_:]+"title={t("add_relation.allowed_characters")}- className="bulk-action-name" requiredcurrentValue={relationName} onChange={setRelationName}/>diff --git a/apps/client/src/widgets/bulk_actions/relation/delete_relation.tsx b/apps/client/src/widgets/bulk_actions/relation/delete_relation.tsxindex 39750d869d..11b5bf3076 100644--- a/apps/client/src/widgets/bulk_actions/relation/delete_relation.tsx+++ b/apps/client/src/widgets/bulk_actions/relation/delete_relation.tsx@@ -1,7 +1,6 @@import AbstractBulkAction, { ActionDefinition } from "../abstract_bulk_action.js";import { t } from "../../../services/i18n.js";-import BulkAction, { useSaveAction } from "../BulkAction.jsx";-import FormTextBox from "../../react/FormTextBox.jsx";+import BulkAction, { BulkActionAttributeName, useSaveAction } from "../BulkAction.jsx";import { useState } from "preact/hooks";function DeleteRelationBulkActionComponent({ bulkAction, actionDef }: { bulkAction: AbstractBulkAction, actionDef: ActionDefinition }) {@@ -12,11 +11,10 @@ function DeleteRelationBulkActionComponent({ bulkAction, actionDef }: { bulkActi<BulkActionbulkAction={bulkAction}>- <FormTextBox- pattern="[\\p{L}\\p{N}_:]+"+ <BulkActionAttributeName+ type="relation"placeholder={t("delete_relation.relation_name")}title={t("delete_relation.allowed_characters")}- className="bulk-action-name" requiredcurrentValue={relationName} onChange={setRelationName}/></BulkAction>diff --git a/apps/client/src/widgets/bulk_actions/relation/rename_relation.tsx b/apps/client/src/widgets/bulk_actions/relation/rename_relation.tsxindex 45a77ff25e..d55ddc3f5c 100644--- a/apps/client/src/widgets/bulk_actions/relation/rename_relation.tsx+++ b/apps/client/src/widgets/bulk_actions/relation/rename_relation.tsx@@ -1,7 +1,6 @@import AbstractBulkAction, { ActionDefinition } from "../abstract_bulk_action.js";import { t } from "../../../services/i18n.js";-import BulkAction, { BulkActionArrow, useSaveAction } from "../BulkAction.jsx";-import FormTextBox from "../../react/FormTextBox.jsx";+import BulkAction, { BulkActionArrow, BulkActionAttributeName, useSaveAction } from "../BulkAction.jsx";import { useState } from "preact/hooks";function RenameRelationBulkActionComponent({ bulkAction, actionDef }: { bulkAction: AbstractBulkAction, actionDef: ActionDefinition }) {@@ -13,21 +12,19 @@ function RenameRelationBulkActionComponent({ bulkAction, actionDef }: { bulkActi<BulkActionbulkAction={bulkAction}>- <FormTextBox+ <BulkActionAttributeName+ type="relation"placeholder={t("rename_relation.old_name")}- pattern="[\\p{L}\\p{N}_:]+"title={t("rename_relation.allowed_characters")}- className="bulk-action-name" requiredcurrentValue={oldRelationName} onChange={setOldRelationName}/><BulkActionArrow />- <FormTextBox+ <BulkActionAttributeName+ type="relation"placeholder={t("rename_relation.new_name")}- pattern="[\\p{L}\\p{N}_:]+"title={t("rename_relation.allowed_characters")}- className="bulk-action-name" requiredcurrentValue={newRelationName} onChange={setNewRelationName}/></BulkAction>diff --git a/apps/client/src/widgets/bulk_actions/relation/update_relation_target.tsx b/apps/client/src/widgets/bulk_actions/relation/update_relation_target.tsxindex 4cf314286f..6f7a876302 100644--- a/apps/client/src/widgets/bulk_actions/relation/update_relation_target.tsx+++ b/apps/client/src/widgets/bulk_actions/relation/update_relation_target.tsx@@ -1,7 +1,6 @@import AbstractBulkAction, { ActionDefinition } from "../abstract_bulk_action.js";import { t } from "../../../services/i18n.js";-import BulkAction, { BulkActionArrow, useSaveAction } from "../BulkAction.jsx";-import FormTextBox from "../../react/FormTextBox.jsx";+import BulkAction, { BulkActionArrow, BulkActionAttributeName, useSaveAction } from "../BulkAction.jsx";import NoteAutocomplete from "../../react/NoteAutocomplete.jsx";import { useState } from "preact/hooks";@@ -14,11 +13,10 @@ function UpdateRelationTargetComponent({ bulkAction, actionDef }: { bulkAction:<BulkActionbulkAction={bulkAction}>- <FormTextBox+ <BulkActionAttributeName+ type="relation"placeholder={t("update_relation_target.relation_name")}- pattern="[\\p{L}\\p{N}_:]+"title={t("update_relation_target.allowed_characters")}- className="bulk-action-name" requiredcurrentValue={relationName} onChange={setRelationName}/>diff --git a/apps/client/src/widgets/dialogs/bulk_actions.spec.tsx b/apps/client/src/widgets/dialogs/bulk_actions.spec.tsxindex 1f52b9d1c1..f9fdc67532 100644--- a/apps/client/src/widgets/dialogs/bulk_actions.spec.tsx+++ b/apps/client/src/widgets/dialogs/bulk_actions.spec.tsx@@ -11,7 +11,7 @@ import { ParentComponent } from "../react/react_utils";import BulkActionsDialog from "./bulk_actions";const server = vi.hoisted(() => ({- get: vi.fn(async () => []),+ get: vi.fn(async (_url: string): Promise<string[]> => []),post: vi.fn(),put: vi.fn(async () => {}),remove: vi.fn(async () => {}),@@ -286,6 +286,28 @@ describe("BulkActionsDialog", () => {expect(container.querySelector<HTMLButtonElement>(".tn-chip-remove")?.disabled).toBe(true);});+ it("completes a relation name against the names in use", async () => {+ server.get.mockImplementation(async (url) =>+ (url.startsWith("attribute-names/?type=relation&query=au") ? [ "author" ] : []));+ 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") ];+ expect(suggestions.map((li) => li.textContent)).toStrictEqual([ "author" ]);++ await act(async () => suggestions[0].click());+ expect(labelNameInputs()[0].value).toBe("author");+ });+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 25bdce5f39..844bd7ea24 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@@ -41,6 +41,9 @@<li>To remove an action, simply press the X button to the right of it.</li><li>It is possible to apply multiple actions of the same type, such as addingmultiple types.</li>+ <li>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.</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 d813051b79..4a97c7ead3 100644--- a/docs/User Guide/User Guide/Advanced Usage/Bulk Actions.md+++ b/docs/User Guide/User Guide/Advanced Usage/Bulk Actions.md@@ -16,6 +16,7 @@ The _Bulk Actions_ dialog makes it easy to apply changes to multiple notes at on* Each chosen action appears as an entry in the _Chosen actions_ section, with its own configuration.* 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.* 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.