mirror of
https://github.com/zadam/trilium.git
synced 2025-09-13 18:16:40 +08:00
fix(views/table): bulk actions sometimes not working
This commit is contained in:
parent
e2c8443778
commit
5d619131ec
3 changed files with 20 additions and 15 deletions
|
@ -1,6 +1,4 @@
|
||||||
import { t } from "i18next";
|
import { t } from "i18next";
|
||||||
import attributes from "../../../services/attributes";
|
|
||||||
import froca from "../../../services/froca";
|
|
||||||
import server from "../../../services/server";
|
import server from "../../../services/server";
|
||||||
import toast from "../../../services/toast";
|
import toast from "../../../services/toast";
|
||||||
import ws from "../../../services/ws";
|
import ws from "../../../services/ws";
|
||||||
|
@ -36,16 +34,10 @@ export async function deleteColumn(parentNoteId: string, type: "label" | "relati
|
||||||
}
|
}
|
||||||
|
|
||||||
async function executeBulkAction(parentNoteId: string, action: {}) {
|
async function executeBulkAction(parentNoteId: string, action: {}) {
|
||||||
const bulkActionNote = await froca.getNote("_bulkAction");
|
|
||||||
if (!bulkActionNote) {
|
|
||||||
console.warn("Bulk action note not found");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
attributes.setLabel("_bulkAction", "action", JSON.stringify(action));
|
|
||||||
await server.post("bulk-action/execute", {
|
await server.post("bulk-action/execute", {
|
||||||
noteIds: [ parentNoteId ],
|
noteIds: [ parentNoteId ],
|
||||||
includeDescendants: true
|
includeDescendants: true,
|
||||||
|
actions: [ action ]
|
||||||
});
|
});
|
||||||
|
|
||||||
await ws.waitForMaxKnownEntityChangeId();
|
await ws.waitForMaxKnownEntityChangeId();
|
||||||
|
|
|
@ -3,7 +3,7 @@ import becca from "../../becca/becca.js";
|
||||||
import bulkActionService from "../../services/bulk_actions.js";
|
import bulkActionService from "../../services/bulk_actions.js";
|
||||||
|
|
||||||
function execute(req: Request) {
|
function execute(req: Request) {
|
||||||
const { noteIds, includeDescendants } = req.body;
|
const { noteIds, includeDescendants, actions } = req.body;
|
||||||
if (!Array.isArray(noteIds)) {
|
if (!Array.isArray(noteIds)) {
|
||||||
throw new Error("noteIds must be an array");
|
throw new Error("noteIds must be an array");
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,16 @@ function execute(req: Request) {
|
||||||
|
|
||||||
const bulkActionNote = becca.getNoteOrThrow("_bulkAction");
|
const bulkActionNote = becca.getNoteOrThrow("_bulkAction");
|
||||||
|
|
||||||
bulkActionService.executeActionsFromNote(bulkActionNote, affectedNoteIds);
|
if (actions && actions.length > 0) {
|
||||||
|
for (const action of actions) {
|
||||||
|
if (!action.name) {
|
||||||
|
throw new Error("Action must have a name");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bulkActionService.executeActions(actions, affectedNoteIds);
|
||||||
|
} else {
|
||||||
|
bulkActionService.executeActionsFromNote(bulkActionNote, affectedNoteIds);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAffectedNoteCount(req: Request) {
|
function getAffectedNoteCount(req: Request) {
|
||||||
|
|
|
@ -50,13 +50,15 @@ type ActionHandlers = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
type ActionHandlerMap = {
|
type ActionHandlerMap = {
|
||||||
[K in keyof ActionHandlers]: ActionHandler<ActionHandlers[K] & { name: K }>;
|
[K in keyof ActionHandlers]: ActionHandler<BulkActionData<K>>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type BulkAction = { name: keyof ActionHandlers } & ActionHandlers[keyof ActionHandlers];
|
export type BulkAction = BulkActionData<keyof ActionHandlers>;
|
||||||
|
|
||||||
const ACTION_HANDLERS: ActionHandlerMap = {
|
const ACTION_HANDLERS: ActionHandlerMap = {
|
||||||
addLabel: (action, note) => {
|
addLabel: (action, note) => {
|
||||||
|
@ -207,7 +209,9 @@ function executeActions(actions: BulkAction[], noteIds: string[] | Set<string>)
|
||||||
try {
|
try {
|
||||||
log.info(`Applying action handler to note ${resultNote.noteId}: ${JSON.stringify(action)}`);
|
log.info(`Applying action handler to note ${resultNote.noteId}: ${JSON.stringify(action)}`);
|
||||||
|
|
||||||
ACTION_HANDLERS[action.name](action, resultNote);
|
const handler = ACTION_HANDLERS[action.name];
|
||||||
|
//@ts-ignore
|
||||||
|
handler(action as BulkAction, resultNote);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
log.error(`ExecuteScript search action failed with ${e.message}`);
|
log.error(`ExecuteScript search action failed with ${e.message}`);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue