Skip to content

Commit

Permalink
update: registerShortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Dec 14, 2023
1 parent 032aa74 commit 3d9f8a8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 73 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@
},
"homepage": "https://github.com/windingwind/zotero-addon-template#readme",
"dependencies": {
"zotero-plugin-toolkit": "^2.3.11"
"zotero-plugin-toolkit": "^2.3.15"
},
"devDependencies": {
"@types/node": "^20.10.4",
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.13.2",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "^6.14.0",
"chokidar": "^3.5.3",
"compressing": "^1.10.0",
"esbuild": "^0.19.8",
"esbuild": "^0.19.9",
"eslint": "^8.55.0",
"eslint-config-prettier": "^9.1.0",
"prettier": "^3.1.0",
"prettier": "^3.1.1",
"release-it": "^17.0.1",
"replace-in-file": "^7.0.2",
"typescript": "^5.3.3",
"zotero-types": "^1.3.7"
"zotero-types": "^1.3.10"
},
"eslintConfig": {
"env": {
Expand Down
7 changes: 2 additions & 5 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ async function onStartup() {

BasicExampleFactory.registerNotifier();

KeyExampleFactory.registerShortcuts();

await onMainWindowLoad(window);
}

Expand All @@ -48,8 +50,6 @@ async function onMainWindowLoad(win: Window): Promise<void> {
})
.show();

KeyExampleFactory.registerShortcuts();

await Zotero.Promise.delay(1000);
popupWin.changeLine({
progress: 30,
Expand Down Expand Up @@ -151,9 +151,6 @@ function onShortcuts(type: string) {
case "smaller":
KeyExampleFactory.exampleShortcutSmallerCallback();
break;
case "confliction":
KeyExampleFactory.exampleShortcutConflictingCallback();
break;
default:
break;
}
Expand Down
70 changes: 8 additions & 62 deletions src/modules/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,55 +86,17 @@ export class BasicExampleFactory {
export class KeyExampleFactory {
@example
static registerShortcuts() {
const keysetId = `${config.addonRef}-keyset`;
const cmdsetId = `${config.addonRef}-cmdset`;
const cmdSmallerId = `${config.addonRef}-cmd-smaller`;
// Register an event key for Alt+L
ztoolkit.Shortcut.register("event", {
id: `${config.addonRef}-key-larger`,
key: "L",
modifiers: "alt",
callback: (keyOptions) => {
ztoolkit.Keyboard.register((ev, keyOptions) => {
ztoolkit.log(ev, keyOptions.keyboard);
if (keyOptions.keyboard.equals("shift,l")) {
addon.hooks.onShortcuts("larger");
},
});
// Register an element key using <key> for Alt+S
ztoolkit.Shortcut.register("element", {
id: `${config.addonRef}-key-smaller`,
key: "S",
modifiers: "alt",
xulData: {
document,
command: cmdSmallerId,
_parentId: keysetId,
_commandOptions: {
id: cmdSmallerId,
document,
_parentId: cmdsetId,
oncommand: `Zotero.${config.addonInstance}.hooks.onShortcuts('smaller')`,
},
},
});
// Here we register an conflict key for Alt+S
// just to show how the confliction check works.
// This is something you should avoid in your plugin.
ztoolkit.Shortcut.register("event", {
id: `${config.addonRef}-key-smaller-conflict`,
key: "S",
modifiers: "alt",
callback: (keyOptions) => {
ztoolkit.getGlobal("alert")("Smaller! This is a conflict key.");
},
});
// Register an event key to check confliction
ztoolkit.Shortcut.register("event", {
id: `${config.addonRef}-key-check-conflict`,
key: "C",
modifiers: "alt",
callback: (keyOptions) => {
addon.hooks.onShortcuts("confliction");
},
}
if (ev.shiftKey && ev.key === "S") {
addon.hooks.onShortcuts("smaller");
}
});

new ztoolkit.ProgressWindow(config.addonName)
.createLine({
text: "Example Shortcuts: Alt+L/S/C",
Expand Down Expand Up @@ -162,22 +124,6 @@ export class KeyExampleFactory {
})
.show();
}

@example
static exampleShortcutConflictingCallback() {
const conflictingGroups = ztoolkit.Shortcut.checkAllKeyConflicting();
new ztoolkit.ProgressWindow("Check Key Conflicting")
.createLine({
text: `${conflictingGroups.length} groups of conflicting keys found. Details are in the debug output/console.`,
})
.show(-1);
ztoolkit.log(
"Conflicting:",
conflictingGroups,
"All keys:",
ztoolkit.Shortcut.getAll(),
);
}
}

export class UIExampleFactory {
Expand Down

0 comments on commit 3d9f8a8

Please sign in to comment.