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(bulk_action): add basic type safety for client
Elian Doran409638151c27
diff --git a/apps/client/src/services/bulk_action.ts b/apps/client/src/services/bulk_action.tsindex c4441259fa..57b880c36e 100644--- a/apps/client/src/services/bulk_action.ts+++ b/apps/client/src/services/bulk_action.ts@@ -16,6 +16,7 @@ import RenameNoteBulkAction from "../widgets/bulk_actions/note/rename_note.js";import { t } from "./i18n.js";import type FNote from "../entities/fnote.js";import toast from "./toast.js";+import { BulkAction } from "@triliumnext/commons";const ACTION_GROUPS = [{@@ -90,7 +91,7 @@ function parseActions(note: FNote) {.filter((action) => !!action);}-export async function executeBulkActions(parentNoteId: string, actions: {}[]) {+export async function executeBulkActions(parentNoteId: string, actions: BulkAction[]) {await server.post("bulk-action/execute", {noteIds: [ parentNoteId ],includeDescendants: true,diff --git a/apps/server/src/services/bulk_actions.ts b/apps/server/src/services/bulk_actions.tsindex 71333992c3..5b2574156d 100644--- a/apps/server/src/services/bulk_actions.ts+++ b/apps/server/src/services/bulk_actions.ts@@ -5,52 +5,7 @@ import branchService from "./branches.js";import { randomString } from "./utils.js";import eraseService from "./erase.js";import type BNote from "../becca/entities/bnote.js";--type ActionHandlers = {- addLabel: {- labelName: string;- labelValue?: string;- },- addRelation: {- relationName: string;- targetNoteId: string;- },- deleteNote: {},- deleteRevisions: {},- deleteLabel: {- labelName: string;- },- deleteRelation: {- relationName: string;- },- renameNote: {- newTitle: string;- },- renameLabel: {- oldLabelName: string;- newLabelName: string;- },- renameRelation: {- oldRelationName: string;- newRelationName: string;- },- updateLabelValue: {- labelName: string;- labelValue: string;- },- updateRelationTarget: {- relationName: string;- targetNoteId: string;- },- moveNote: {- targetParentNoteId: string;- },- executeScript: {- script: string;- }-};--type BulkActionData<T extends keyof ActionHandlers> = ActionHandlers[T] & { name: T };+import { ActionHandlers, BulkAction, BulkActionData } from "@triliumnext/commons";type ActionHandler<T> = (action: T, note: BNote) => void;@@ -58,8 +13,6 @@ type ActionHandlerMap = {[K in keyof ActionHandlers]: ActionHandler<BulkActionData<K>>;};-export type BulkAction = BulkActionData<keyof ActionHandlers>;-const ACTION_HANDLERS: ActionHandlerMap = {addLabel: (action, note) => {note.addLabel(action.labelName, action.labelValue);diff --git a/packages/commons/src/index.ts b/packages/commons/src/index.tsindex 96ba3325f6..5340e06d63 100644--- a/packages/commons/src/index.ts+++ b/packages/commons/src/index.ts@@ -5,3 +5,4 @@ export * from "./lib/hidden_subtree.js";export * from "./lib/rows.js";export * from "./lib/test-utils.js";export * from "./lib/mime_type.js";+export * from "./lib/bulk_actions.js";diff --git a/packages/commons/src/lib/bulk_actions.ts b/packages/commons/src/lib/bulk_actions.tsnew file mode 100644index 0000000000..4dbce561e1--- /dev/null+++ b/packages/commons/src/lib/bulk_actions.ts@@ -0,0 +1,47 @@+export type ActionHandlers = {+ addLabel: {+ labelName: string;+ labelValue?: string;+ },+ addRelation: {+ relationName: string;+ targetNoteId: string;+ },+ deleteNote: {},+ deleteRevisions: {},+ deleteLabel: {+ labelName: string;+ },+ deleteRelation: {+ relationName: string;+ },+ renameNote: {+ newTitle: string;+ },+ renameLabel: {+ oldLabelName: string;+ newLabelName: string;+ },+ renameRelation: {+ oldRelationName: string;+ newRelationName: string;+ },+ updateLabelValue: {+ labelName: string;+ labelValue: string;+ },+ updateRelationTarget: {+ relationName: string;+ targetNoteId: string;+ },+ moveNote: {+ targetParentNoteId: string;+ },+ executeScript: {+ script: string;+ }+};++export type BulkActionData<T extends keyof ActionHandlers> = ActionHandlers[T] & { name: T };++export type BulkAction = BulkActionData<keyof ActionHandlers>;