Skip to content

Commit

Permalink
update: ztoolkit 2.3.14
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Dec 14, 2023
1 parent 737e926 commit 452f6f0
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 176 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"homepage": "https://github.com/windingwind/zotero-actions-tags#readme",
"dependencies": {
"js-yaml": "^4.1.0",
"zotero-plugin-toolkit": "^2.3.12"
"zotero-plugin-toolkit": "^2.3.14"
},
"devDependencies": {
"@types/js-yaml": "^4.0.9",
Expand All @@ -51,7 +51,7 @@
"release-it": "^16.3.0",
"replace-in-file": "^7.0.2",
"typescript": "^5.3.3",
"zotero-types": "^1.3.7"
"zotero-types": "^1.3.10"
},
"eslintConfig": {
"env": {
Expand Down
2 changes: 0 additions & 2 deletions src/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
} from "zotero-plugin-toolkit/dist/helpers/virtualizedTable";
import { createZToolkit } from "./utils/ztoolkit";
import { ActionMap } from "./utils/actions";
import { KeyModifier } from "./utils/shorcut";
import hooks from "./hooks";
import api from "./api";

Expand Down Expand Up @@ -33,7 +32,6 @@ class Addon {
cachedKeys: string[];
selectedKey?: string;
};
shortcut?: KeyModifier;
tabStatus: Map<string, number>;
hint: {
total: number;
Expand Down
15 changes: 4 additions & 11 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { getString, initLocale } from "./utils/locale";
import { initPrefPane } from "./modules/preferenceWindow";
import { ActionEventTypes, initActions } from "./utils/actions";
import { initNotifierObserver } from "./modules/notify";
import {
initReaderShortcuts,
initWindowShortcuts,
unInitWindowShortcuts,
} from "./modules/shortcuts";
import { initShortcuts } from "./modules/shortcuts";
import {
buildItemMenu,
initItemMenu,
Expand All @@ -29,20 +25,20 @@ async function onStartup() {

initNotifierObserver();

ztoolkit.PreferencePane.register({
Zotero.PreferencePanes.register({
pluginID: config.addonID,
src: rootURI + "chrome/content/preferences.xhtml",
label: getString("prefs-title"),
defaultXUL: true,
helpURL: homepage,
image: rootURI + "chrome/content/icons/favicon.png",
});

await addon.api.actionManager.dispatchActionByEvent(
ActionEventTypes.programStartup,
{},
);

initReaderShortcuts();
initShortcuts();

initReaderMenu();

Expand All @@ -52,7 +48,6 @@ async function onStartup() {
}

async function onMainWindowLoad(win: Window): Promise<void> {
initWindowShortcuts(win);
initItemMenu(win);
await addon.api.actionManager.dispatchActionByEvent(
ActionEventTypes.mainWindowLoad,
Expand All @@ -63,7 +58,6 @@ async function onMainWindowLoad(win: Window): Promise<void> {
}

async function onMainWindowUnload(win: Window): Promise<void> {
unInitWindowShortcuts(win);
await addon.api.actionManager.dispatchActionByEvent(
ActionEventTypes.mainWindowUnload,
{
Expand All @@ -74,7 +68,6 @@ async function onMainWindowUnload(win: Window): Promise<void> {

function onShutdown(): void {
ztoolkit.unregisterAll();
Zotero.getMainWindows().forEach(unInitWindowShortcuts);
// Remove addon object
addon.data.alive = false;
delete Zotero[config.addonInstance];
Expand Down
2 changes: 1 addition & 1 deletion src/modules/dispatch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KeyModifier } from "zotero-plugin-toolkit/dist/managers/keyboard";
import { ActionEventTypes, ActionArgs, applyAction } from "../utils/actions";
import { KeyModifier } from "../utils/shorcut";

export { dispatchActionByEvent, dispatchActionByShortcut, dispatchActionByKey };

Expand Down
2 changes: 1 addition & 1 deletion src/modules/edit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { KeyModifier } from "zotero-plugin-toolkit/dist/managers/keyboard";
import { ActionEventTypes, ActionOperationTypes } from "../utils/actions";
import { getString } from "../utils/locale";
import { getPref } from "../utils/prefs";
import { KeyModifier } from "../utils/shorcut";
import { waitUtilAsync } from "../utils/wait";
import { closeWindow, isWindowAlive } from "../utils/window";

Expand Down
86 changes: 15 additions & 71 deletions src/modules/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,21 @@
import { getCurrentItems } from "../utils/items";
import { KeyModifier } from "../utils/shorcut";
import { waitUntil } from "../utils/wait";

export { initWindowShortcuts, unInitWindowShortcuts, initReaderShortcuts };
export { initShortcuts };

function initWindowShortcuts(win: Window) {
_initShortcuts(win);
}

function unInitWindowShortcuts(win: Window) {
_unInitShortcuts(win);
}

function initReaderShortcuts() {
Zotero.Reader.registerEventListener("renderToolbar", (event) => {
const reader = event.reader;
_initShortcuts(reader._iframeWindow);
waitUntil(
() => (reader._internalReader?._primaryView as any)?._iframeWindow,
() =>
_initShortcuts(
(reader._internalReader._primaryView as any)?._iframeWindow,
),
);
});
}

function _initShortcuts(win?: Window) {
if (!win) {
return;
}
win.addEventListener("keydown", savePressedKeys);
win.addEventListener("keyup", triggerShortcut);
}
function initShortcuts() {
ztoolkit.Keyboard.register(async (ev, options) => {
ztoolkit.log(options.keyboard.getLocalized());

function _unInitShortcuts(win?: Window) {
if (!win) {
return;
}
win.removeEventListener("keydown", savePressedKeys);
win.removeEventListener("keyup", triggerShortcut);
}

function savePressedKeys(e: KeyboardEvent) {
if (!addon.data.shortcut) {
addon.data.shortcut = new KeyModifier("");
}
const shortcut = addon.data.shortcut;
shortcut.control = e.ctrlKey;
shortcut.meta = e.metaKey;
shortcut.shift = e.shiftKey;
shortcut.alt = e.altKey;
if (!["Shift", "Meta", "Ctrl", "Alt", "Control"].includes(e.key)) {
shortcut.key = e.key;
}
}

async function triggerShortcut(e: KeyboardEvent) {
if (!addon.data.shortcut) {
return;
}
const shortcut = new KeyModifier(addon.data.shortcut.getRaw());
addon.data.shortcut = undefined;

const items = await getCurrentItems();
// Trigger action for multiple items
await addon.api.actionManager.dispatchActionByShortcut(shortcut, {
itemIDs: items.map((item) => item?.id),
});
// Trigger action for each item
for (const item of items) {
await addon.api.actionManager.dispatchActionByShortcut(shortcut, {
itemID: item.id,
const items = await getCurrentItems();
// Trigger action for multiple items
await addon.api.actionManager.dispatchActionByShortcut(options.keyboard, {
itemIDs: items.map((item) => item?.id),
});
}
// Trigger action for each item
for (const item of items) {
await addon.api.actionManager.dispatchActionByShortcut(options.keyboard, {
itemID: item.id,
});
}
});
}
86 changes: 0 additions & 86 deletions src/utils/shorcut.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/utils/ztoolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ import { VirtualizedTableHelper } from "zotero-plugin-toolkit/dist/helpers/virtu
import { DialogHelper } from "zotero-plugin-toolkit/dist/helpers/dialog";
import { MenuManager } from "zotero-plugin-toolkit/dist/managers/menu";
import { FilePickerHelper } from "zotero-plugin-toolkit/dist/helpers/filePicker";
import { KeyboardManager } from "zotero-plugin-toolkit/dist/managers/keyboard";

class MyToolkit extends BasicTool {
UI: UITool;
Menu: MenuManager;
PreferencePane: PreferencePaneManager;
Keyboard: KeyboardManager;
ProgressWindow: typeof ProgressWindowHelper;
LargePref: typeof LargePrefHelper;
VirtualizedTable: typeof VirtualizedTableHelper;
Expand All @@ -52,7 +53,7 @@ class MyToolkit extends BasicTool {
super();
this.UI = new UITool(this);
this.Menu = new MenuManager(this);
this.PreferencePane = new PreferencePaneManager(this);
this.Keyboard = new KeyboardManager(this);
this.ProgressWindow = ProgressWindowHelper;
this.LargePref = LargePrefHelper;
this.VirtualizedTable = VirtualizedTableHelper;
Expand Down

0 comments on commit 452f6f0

Please sign in to comment.