Skip to content

Commit

Permalink
add: script argument triggerType
Browse files Browse the repository at this point in the history
resolve: #253
  • Loading branch information
windingwind committed Feb 7, 2024
1 parent 54eff08 commit 4d7c072
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ Share & find custom scripts here: https://github.com/windingwind/zotero-actions-

You can use the following variables in the script:

- `triggerType`: The trigger type. Can be `menu`, `shortcut`, `createItem`, `openFile`, `closeTab`, `createAnnotation`, `createNote`, `appendAnnotation`, `appendNote`, `programStartup`, `mainWindowLoad`, `mainWindowUnload`.

- `item`: The target item. Might be `null` if the action is triggered by an event that doesn't have a target item, e.g. shortcut in the Zotero client without selecting an item. (Not available in `programStartup`, `mainWindowLoad`, and `mainWindowUnload` event)

<details style="text-indent: 4em">
Expand Down
18 changes: 14 additions & 4 deletions src/modules/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ export { dispatchActionByEvent, dispatchActionByShortcut, dispatchActionByKey };

async function dispatchActionByEvent(
eventType: ActionEventTypes,
data: ActionArgs,
data: Omit<ActionArgs, "triggerType">,
) {
const actions = getActionsByEvent(eventType);
for (const action of actions) {
await applyAction(action, data);
await applyAction(
action,
Object.assign({}, data, {
triggerType: eventType,
}),
);
}
}

Expand All @@ -21,11 +26,16 @@ function getActionsByEvent(event: ActionEventTypes) {

async function dispatchActionByShortcut(
shortcut: KeyModifier,
data: ActionArgs,
data: Omit<ActionArgs, "triggerType">,
) {
const actions = getActionsByShortcuts(shortcut);
for (const action of actions) {
await applyAction(action, data);
await applyAction(
action,
Object.assign({}, data, {
triggerType: "shortcut",
} as ActionArgs),
);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/modules/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,14 @@ async function triggerMenuCommand(
await addon.api.actionManager.dispatchActionByKey(key, {
itemIDs: items.map((item) => item.id),
collectionID: collection?.id,
triggerType: "menu",
});
// Trigger action for each item
for (const item of items) {
await addon.api.actionManager.dispatchActionByKey(key, {
itemID: item.id,
collectionID: collection?.id,
triggerType: "menu",
});
}
}
18 changes: 11 additions & 7 deletions src/utils/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type ActionArgs = {
itemID?: number;
itemIDs?: number[];
collectionID?: number;
triggerType: ActionEventTypes | "shortcut" | "menu" | "unknown";
[key: string]: any;
};

Expand Down Expand Up @@ -256,17 +257,17 @@ async function applyAction(action: ActionData, args: ActionArgs) {
| false;
}

let paramList: any[] = [item, items, collection, _require];
let paramSign = "item, items, collection, require";
let paramList: any[] = [item, items, collection];
let paramSign = ["item", "items", "collection"];
switch (action.event) {
case ActionEventTypes.mainWindowLoad:
case ActionEventTypes.mainWindowUnload:
paramList = [args.window, _require];
paramSign = "window, require";
paramList = [args.window];
paramSign = ["window"];
break;
case ActionEventTypes.programStartup:
paramList = [_require];
paramSign = "require";
paramList = [];
paramSign = [];
break;
// Use default value
case ActionEventTypes.createItem:
Expand All @@ -281,8 +282,11 @@ async function applyAction(action: ActionData, args: ActionArgs) {
break;
}

paramList.push(_require, args.triggerType);
paramSign.push("require", "triggerType");

try {
const func = new AsyncFunction(paramSign, script);
const func = new AsyncFunction(paramSign.join(", "), script);
message = await func(...paramList);
} catch (e) {
ztoolkit.log("Script Error", e);
Expand Down

0 comments on commit 4d7c072

Please sign in to comment.