Skip to content

Commit

Permalink
fix: do not trigger action when select-all or typing
Browse files Browse the repository at this point in the history
fix: get item as id for faster data preparation
fix: #315
  • Loading branch information
windingwind committed Apr 27, 2024
1 parent 8aeb168 commit 0d54f75
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 29 deletions.
24 changes: 13 additions & 11 deletions src/modules/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { config } from "../../package.json";
import { getString } from "../utils/locale";
import { getPref } from "../utils/prefs";
import { ActionData, ActionShowInMenu } from "../utils/actions";
import { getCurrentItems, getItemsByKey } from "../utils/items";
import { getCurrentItems, getItemIDsByKey } from "../utils/items";
import { getIcon } from "../utils/icon";

export {
Expand Down Expand Up @@ -91,7 +91,7 @@ function getReaderMenuPopup(reader: _ZoteroTypes.ReaderInstance) {
type: "popupshowing",
listener: (ev) => {
addon.hooks.onMenuEvent("showing", {
window: reader._window,
window: doc.defaultView,
target: "reader",
extraData: {
readerID:
Expand Down Expand Up @@ -123,7 +123,7 @@ function initReaderAnnotationMenu() {
label: action.menu!,
onCommand: () => {
triggerMenuCommand(action.key, () =>
getItemsByKey(reader._item.libraryID, ...params.ids),
getItemIDsByKey(reader._item.libraryID, ...params.ids),
);
},
});
Expand Down Expand Up @@ -240,7 +240,11 @@ function buildItemMenu(
listener: (event) => {
triggerMenuCommand(
action.key,
() => getCurrentItems(target, extraData),
() =>
getCurrentItems(target, {
asIDs: true,
readerID: extraData?.readerID,
}),
target === "collection",
);
},
Expand Down Expand Up @@ -283,27 +287,25 @@ function getActionsByMenu(target: ActionShowInMenu) {

async function triggerMenuCommand(
key: string,
getItems: () =>
| Zotero.DataObject[]
| Promise<Zotero.DataObject[]> = getCurrentItems,
getItemIDs: () => number[] | Promise<number[]>,
withCollection: boolean = false,
) {
const items = await getItems();
const itemIDs = await getItemIDs();
let collection: Zotero.Collection | undefined = undefined;
if (withCollection) {
collection = Zotero.getActiveZoteroPane().getSelectedCollection();
}

// Trigger action for all items
await addon.api.actionManager.dispatchActionByKey(key, {
itemIDs: items.map((item) => item.id),
itemIDs,
collectionID: collection?.id,
triggerType: "menu",
});
// Trigger action for each item
for (const item of items) {
for (const itemID of itemIDs) {
await addon.api.actionManager.dispatchActionByKey(key, {
itemID: item.id,
itemID,
collectionID: collection?.id,
triggerType: "menu",
});
Expand Down
21 changes: 16 additions & 5 deletions src/modules/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,28 @@ export { initShortcuts };
function initShortcuts() {
ztoolkit.Keyboard.register(async (ev, options) => {
ztoolkit.log(options.keyboard?.getLocalized());
if (!options.keyboard) return;
// Do nothing if the keyboard shortcut is not set or is "accel,A"
// "accel,A" is the default keyboard shortcut for selecting all items
// https://github.com/windingwind/zotero-actions-tags/issues/315
if (
!options.keyboard ||
options.keyboard.equals("accel,A") ||
["input", "textarea", "select", "search-textbox", "textbox"].includes(
(ev.target as HTMLElement)?.localName.toLowerCase(),
)
)
return;
ztoolkit.log("triggered", options.keyboard.getLocalized(), ev);

const items = await getCurrentItems();
const itemIDs = await getCurrentItems(undefined, { asIDs: true });
// Trigger action for multiple items
await addon.api.actionManager.dispatchActionByShortcut(options.keyboard, {
itemIDs: items.map((item) => item?.id),
itemIDs,
});
// Trigger action for each item
for (const item of items) {
for (const itemID of itemIDs) {
await addon.api.actionManager.dispatchActionByShortcut(options.keyboard, {
itemID: item.id,
itemID,
});
}
});
Expand Down
51 changes: 38 additions & 13 deletions src/utils/items.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,54 @@
import { ActionShowInMenu } from "./actions";

export { getCurrentItems, getItemsByKey };
export { getCurrentItems, getItemIDsByKey };

async function getCurrentItems(
type?: ActionShowInMenu,
extraData?: {
readerID?: string;
},
) {
let items = [] as Zotero.Item[];
): Promise<Zotero.Item[]>;

async function getCurrentItems(
type?: ActionShowInMenu,
extraData?: {
readerID?: string;
asIDs: true;
},
): Promise<number[]>;

async function getCurrentItems(
type?: ActionShowInMenu,
extraData?: {
readerID?: string;
asIDs?: boolean;
},
): Promise<Zotero.Item[] | number[]> {
const asIDs = !!extraData?.asIDs;
let items = [] as Zotero.Item[] | number[];
if (!type || type === "tools") {
type = getCurrentTargetType();
}
switch (type) {
case "item": {
items = Zotero.getActiveZoteroPane().getSelectedItems();
// Stupid but for type inference
items = asIDs
? Zotero.getActiveZoteroPane().getSelectedItems(true)
: Zotero.getActiveZoteroPane().getSelectedItems(false);
break;
}
case "collection": {
const collection = Zotero.getActiveZoteroPane().getSelectedCollection();
if (collection) {
items = collection?.getChildItems() as Zotero.Item[];
items = asIDs
? collection?.getChildItems(true)
: collection?.getChildItems(false);
} else {
const libraryID = Zotero.getActiveZoteroPane().getSelectedLibraryID();
if (libraryID) {
items = await Zotero.Items.getAll(libraryID);
items = await (asIDs
? Zotero.Items.getAll(libraryID, false, false, true)
: Zotero.Items.getAll(libraryID, false, false, false));
}
}
break;
Expand Down Expand Up @@ -52,28 +76,29 @@ async function getCurrentItems(
const item = Zotero.Items.getByLibraryAndKey(
reader._item.libraryID,
key,
);
) as Zotero.Item;
if (!item) continue;
items.push(item as Zotero.Item);
items.push((asIDs ? item.id : item) as Zotero.Item & number);
}
} else {
items = [reader._item];
items = [asIDs ? reader._item.id : reader._item] as Zotero.Item[] &
number[];
}
break;
}
}
return items;
}

function getItemsByKey(libraryID: number, ...keys: string[]) {
const items = [] as Zotero.DataObject[];
function getItemIDsByKey(libraryID: number, ...keys: string[]) {
const itemIDs = [] as number[];
for (const key of keys) {
const item = Zotero.Items.getByLibraryAndKey(libraryID, key);
if (item) {
items.push(item);
itemIDs.push(item.id);
}
}
return items;
return itemIDs;
}

function getCurrentTargetType() {
Expand Down

0 comments on commit 0d54f75

Please sign in to comment.