mirror of
https://github.com/zadam/trilium.git
synced 2025-11-02 03:16:11 +01:00
refactor(bulk_action): add basic type safety for client
This commit is contained in:
@@ -16,6 +16,7 @@ import RenameNoteBulkAction from "../widgets/bulk_actions/note/rename_note.js";
|
|||||||
import { t } from "./i18n.js";
|
import { t } from "./i18n.js";
|
||||||
import type FNote from "../entities/fnote.js";
|
import type FNote from "../entities/fnote.js";
|
||||||
import toast from "./toast.js";
|
import toast from "./toast.js";
|
||||||
|
import { BulkAction } from "@triliumnext/commons";
|
||||||
|
|
||||||
const ACTION_GROUPS = [
|
const ACTION_GROUPS = [
|
||||||
{
|
{
|
||||||
@@ -90,7 +91,7 @@ function parseActions(note: FNote) {
|
|||||||
.filter((action) => !!action);
|
.filter((action) => !!action);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function executeBulkActions(parentNoteId: string, actions: {}[]) {
|
export async function executeBulkActions(parentNoteId: string, actions: BulkAction[]) {
|
||||||
await server.post("bulk-action/execute", {
|
await server.post("bulk-action/execute", {
|
||||||
noteIds: [ parentNoteId ],
|
noteIds: [ parentNoteId ],
|
||||||
includeDescendants: true,
|
includeDescendants: true,
|
||||||
|
|||||||
@@ -5,52 +5,7 @@ import branchService from "./branches.js";
|
|||||||
import { randomString } from "./utils.js";
|
import { randomString } from "./utils.js";
|
||||||
import eraseService from "./erase.js";
|
import eraseService from "./erase.js";
|
||||||
import type BNote from "../becca/entities/bnote.js";
|
import type BNote from "../becca/entities/bnote.js";
|
||||||
|
import { ActionHandlers, BulkAction, BulkActionData } from "@triliumnext/commons";
|
||||||
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 };
|
|
||||||
|
|
||||||
type ActionHandler<T> = (action: T, note: BNote) => void;
|
type ActionHandler<T> = (action: T, note: BNote) => void;
|
||||||
|
|
||||||
@@ -58,8 +13,6 @@ type ActionHandlerMap = {
|
|||||||
[K in keyof ActionHandlers]: ActionHandler<BulkActionData<K>>;
|
[K in keyof ActionHandlers]: ActionHandler<BulkActionData<K>>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type BulkAction = BulkActionData<keyof ActionHandlers>;
|
|
||||||
|
|
||||||
const ACTION_HANDLERS: ActionHandlerMap = {
|
const ACTION_HANDLERS: ActionHandlerMap = {
|
||||||
addLabel: (action, note) => {
|
addLabel: (action, note) => {
|
||||||
note.addLabel(action.labelName, action.labelValue);
|
note.addLabel(action.labelName, action.labelValue);
|
||||||
|
|||||||
@@ -5,3 +5,4 @@ export * from "./lib/hidden_subtree.js";
|
|||||||
export * from "./lib/rows.js";
|
export * from "./lib/rows.js";
|
||||||
export * from "./lib/test-utils.js";
|
export * from "./lib/test-utils.js";
|
||||||
export * from "./lib/mime_type.js";
|
export * from "./lib/mime_type.js";
|
||||||
|
export * from "./lib/bulk_actions.js";
|
||||||
|
|||||||
47
packages/commons/src/lib/bulk_actions.ts
Normal file
47
packages/commons/src/lib/bulk_actions.ts
Normal file
@@ -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>;
|
||||||
Reference in New Issue
Block a user