update: registerShortcuts

This commit is contained in:
windingwind 2023-12-14 22:33:10 +08:00
parent 032aa74f6e
commit 3d9f8a8a0d
3 changed files with 16 additions and 73 deletions

View File

@ -32,22 +32,22 @@
}, },
"homepage": "https://github.com/windingwind/zotero-addon-template#readme", "homepage": "https://github.com/windingwind/zotero-addon-template#readme",
"dependencies": { "dependencies": {
"zotero-plugin-toolkit": "^2.3.11" "zotero-plugin-toolkit": "^2.3.15"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^20.10.4", "@types/node": "^20.10.4",
"@typescript-eslint/eslint-plugin": "^6.13.2", "@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "^6.13.2", "@typescript-eslint/parser": "^6.14.0",
"chokidar": "^3.5.3", "chokidar": "^3.5.3",
"compressing": "^1.10.0", "compressing": "^1.10.0",
"esbuild": "^0.19.8", "esbuild": "^0.19.9",
"eslint": "^8.55.0", "eslint": "^8.55.0",
"eslint-config-prettier": "^9.1.0", "eslint-config-prettier": "^9.1.0",
"prettier": "^3.1.0", "prettier": "^3.1.1",
"release-it": "^17.0.1", "release-it": "^17.0.1",
"replace-in-file": "^7.0.2", "replace-in-file": "^7.0.2",
"typescript": "^5.3.3", "typescript": "^5.3.3",
"zotero-types": "^1.3.7" "zotero-types": "^1.3.10"
}, },
"eslintConfig": { "eslintConfig": {
"env": { "env": {

View File

@ -30,6 +30,8 @@ async function onStartup() {
BasicExampleFactory.registerNotifier(); BasicExampleFactory.registerNotifier();
KeyExampleFactory.registerShortcuts();
await onMainWindowLoad(window); await onMainWindowLoad(window);
} }
@ -48,8 +50,6 @@ async function onMainWindowLoad(win: Window): Promise<void> {
}) })
.show(); .show();
KeyExampleFactory.registerShortcuts();
await Zotero.Promise.delay(1000); await Zotero.Promise.delay(1000);
popupWin.changeLine({ popupWin.changeLine({
progress: 30, progress: 30,
@ -151,9 +151,6 @@ function onShortcuts(type: string) {
case "smaller": case "smaller":
KeyExampleFactory.exampleShortcutSmallerCallback(); KeyExampleFactory.exampleShortcutSmallerCallback();
break; break;
case "confliction":
KeyExampleFactory.exampleShortcutConflictingCallback();
break;
default: default:
break; break;
} }

View File

@ -86,55 +86,17 @@ export class BasicExampleFactory {
export class KeyExampleFactory { export class KeyExampleFactory {
@example @example
static registerShortcuts() { 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 // Register an event key for Alt+L
ztoolkit.Shortcut.register("event", { ztoolkit.Keyboard.register((ev, keyOptions) => {
id: `${config.addonRef}-key-larger`, ztoolkit.log(ev, keyOptions.keyboard);
key: "L", if (keyOptions.keyboard.equals("shift,l")) {
modifiers: "alt",
callback: (keyOptions) => {
addon.hooks.onShortcuts("larger"); addon.hooks.onShortcuts("larger");
}, }
}); if (ev.shiftKey && ev.key === "S") {
// Register an element key using <key> for Alt+S addon.hooks.onShortcuts("smaller");
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");
},
}); });
new ztoolkit.ProgressWindow(config.addonName) new ztoolkit.ProgressWindow(config.addonName)
.createLine({ .createLine({
text: "Example Shortcuts: Alt+L/S/C", text: "Example Shortcuts: Alt+L/S/C",
@ -162,22 +124,6 @@ export class KeyExampleFactory {
}) })
.show(); .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 { export class UIExampleFactory {