Skip to content

Commit

Permalink
fix: async bug
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Nov 29, 2023
1 parent 3b11a12 commit ae99f96
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
20 changes: 10 additions & 10 deletions src/modules/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function initItemMenu(win: Window) {
},
],
},
win.document.querySelector("popupset")!
win.document.querySelector("popupset")!,
);
}

Expand Down Expand Up @@ -124,15 +124,15 @@ function initReaderMenu() {
classList: ["dropmarker"],
},
],
})
}),
);
append(
ztoolkit.UI.createElement(doc, "style", {
id: `${config.addonRef}-reader-button`,
properties: {
textContent: readerButtonCSS,
},
})
}),
);
});
}
Expand All @@ -148,19 +148,19 @@ function initReaderAnnotationMenu() {
label: action.menu!,
onCommand: () => {
triggerMenuCommand(action.key, () =>
getItemsByKey(reader._item.libraryID, ...params.ids)
getItemsByKey(reader._item.libraryID, ...params.ids),
);
},
});
}
}
},
);
}

function buildItemMenu(win: Window, target: "item" | "collection" | "reader") {
const doc = win.document;
const popup = doc.querySelector(
`#${config.addonRef}-${target}-popup`
`#${config.addonRef}-${target}-popup`,
) as XUL.MenuPopup;
// Remove all children in popup
while (popup?.firstChild) {
Expand Down Expand Up @@ -196,7 +196,7 @@ function buildItemMenu(win: Window, target: "item" | "collection" | "reader") {
triggerMenuCommand(
action.key,
() => getCurrentItems(target),
target === "collection"
target === "collection",
);
},
},
Expand All @@ -217,7 +217,7 @@ function getActionsByMenu(target: ActionShowInMenu) {
action &&
action.menu &&
action.enabled &&
(!action.showInMenu || action.showInMenu[target] !== false)
(!action.showInMenu || action.showInMenu[target] !== false),
)
.sort((x, y) => {
if (!x && !y) {
Expand All @@ -231,7 +231,7 @@ function getActionsByMenu(target: ActionShowInMenu) {
}
return ((x[sortBy] as string) || "").localeCompare(
(y[sortBy] || "") as string,
Zotero.locale
Zotero.locale,
);
});
}
Expand All @@ -241,7 +241,7 @@ async function triggerMenuCommand(
getItems: () =>
| Zotero.DataObject[]
| Promise<Zotero.DataObject[]> = getCurrentItems,
withCollection: boolean = false
withCollection: boolean = false,
) {
const items = await getItems();
let collection: Zotero.Collection | undefined = undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async function triggerShortcut(e: KeyboardEvent) {
const shortcut = new KeyModifier(addon.data.shortcut.getRaw());
addon.data.shortcut = undefined;

const items = getCurrentItems();
const items = await getCurrentItems();
// Trigger action for multiple items
await addon.api.actionManager.dispatchActionByShortcut(shortcut, {
itemIDs: items.map((item) => item?.id),
Expand Down
18 changes: 10 additions & 8 deletions src/utils/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function initActions() {
addon.data.actions.map = new ztoolkit.LargePref(
`${config.prefsPrefix}.rules`,
`${config.prefsPrefix}.rules.`,
"parser"
"parser",
).asMapLike() as ActionMap;
if (!getPref("rulesInit")) {
for (const key of defaultActions.keys()) {
Expand Down Expand Up @@ -161,7 +161,7 @@ function initActions() {

function updateCachedActionKeys() {
addon.data.actions.cachedKeys = Array.from(
addon.data.actions.map.keys()
addon.data.actions.map.keys(),
).sort((a, b) => {
const actionA = addon.data.actions.map.get(a);
const actionB = addon.data.actions.map.get(b);
Expand All @@ -172,13 +172,13 @@ function updateCachedActionKeys() {
actionA[
addon.data.prefs.columns[addon.data.prefs.columnIndex]
.dataKey as keyof ActionData
] || ""
] || "",
);
const valueB = String(
actionB[
addon.data.prefs.columns[addon.data.prefs.columnIndex]
.dataKey as keyof ActionData
] || ""
] || "",
);

return addon.data.prefs.columnAscending
Expand Down Expand Up @@ -234,7 +234,7 @@ async function applyAction(action: ActionData, args: ActionArgs) {
}
}
message = `Toggle tag ${tags.join(",")} to item ${item?.getField(
"title"
"title",
)}`;
break;
}
Expand All @@ -245,7 +245,9 @@ async function applyAction(action: ActionData, args: ActionArgs) {

let collection: Zotero.Collection | false = false;
if (args.collectionID) {
collection = Zotero.Collections.get(args.collectionID) as Zotero.Collection | false;
collection = Zotero.Collections.get(args.collectionID) as
| Zotero.Collection
| false;
}

let paramList: any[] = [item, items, collection, _require];
Expand Down Expand Up @@ -286,7 +288,7 @@ async function applyAction(action: ActionData, args: ActionArgs) {
const actions = getActions();
// Find the action by name
const nextAction = Object.values(actions).find(
(_action) => _action.name === action.data
(_action) => _action.name === action.data,
);
if (nextAction) {
await applyAction(nextAction, args);
Expand All @@ -307,7 +309,7 @@ async function applyAction(action: ActionData, args: ActionArgs) {
function getActions(): Record<string, ActionData>;
function getActions(key: string): ActionData | undefined;
function getActions(
key?: string
key?: string,
): Record<string, ActionData> | ActionData | undefined {
if (!key) {
const map = addon.data.actions.map;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ActionShowInMenu } from "./actions";
export { getCurrentItems, getItemsByKey };

async function getCurrentItems(type?: ActionShowInMenu) {
let items = [] as Zotero.DataObject[];
let items = [] as Zotero.Item[];
if (!type) {
type = getCurrentTargetType();
}
Expand Down

0 comments on commit ae99f96

Please sign in to comment.