From a95515fc47653a49665a69e1436b583535fcefda Mon Sep 17 00:00:00 2001 From: xiangyu <3170102889@zju.edu.cn> Date: Mon, 9 Jan 2023 00:44:26 +0800 Subject: [PATCH] add: shortcut examples for toolkit 0.1.6 --- README.md | 7 +++ package.json | 2 +- src/hooks.ts | 25 ++++++++++- src/modules/examples.ts | 97 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 540a500..519a04d 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,13 @@ Search `@example` in `src/examples.ts`. The examples are called in `src/hooks.ts - registerNotifier - registerPrefs, unregisterPrefs +### Shortcut Keys Examples + +- registerShortcuts +- exampleShortcutLargerCallback +- exampleShortcutSmallerCallback +- exampleShortcutConflictionCallback + ### UI Examples ![image](https://user-images.githubusercontent.com/33902321/209274492-7aa94912-af38-4154-af46-dc8f59640de3.png) diff --git a/package.json b/package.json index 1ea0907..5e08e86 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ }, "homepage": "https://github.com/windingwind/zotero-addon-template#readme", "dependencies": { - "zotero-plugin-toolkit": "^0.1.3" + "zotero-plugin-toolkit": "^0.1.6" }, "devDependencies": { "@types/node": "^18.11.17", diff --git a/src/hooks.ts b/src/hooks.ts index 3b31537..9a0f82b 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -1,4 +1,8 @@ -import { BasicExampleFactory, UIExampleFactory } from "./modules/examples"; +import { + BasicExampleFactory, + KeyExampleFactory, + UIExampleFactory, +} from "./modules/examples"; import { config } from "../package.json"; import { getString, initLocale } from "./modules/locale"; import { registerPrefsScripts } from "./modules/preferenceScript"; @@ -30,6 +34,8 @@ async function onStartup() { BasicExampleFactory.registerNotifier(); + KeyExampleFactory.registerShortcuts(); + await Zotero.Promise.delay(1000); popupWin.changeLine({ progress: 30, @@ -110,6 +116,22 @@ async function onPrefsEvent(type: string, data: { [key: string]: any }) { } } +function onShortcuts(type: string) { + switch (type) { + case "larger": + KeyExampleFactory.exampleShortcutLargerCallback(); + break; + case "smaller": + KeyExampleFactory.exampleShortcutSmallerCallback(); + break; + case "confliction": + KeyExampleFactory.exampleShortcutConflictionCallback(); + break; + default: + break; + } +} + // Add your hooks here. For element click, etc. // Keep in mind hooks only do dispatch. Don't add code that does real jobs in hooks. // Otherwise the code would be hard to read and maintian. @@ -119,4 +141,5 @@ export default { onShutdown, onNotify, onPrefsEvent, + onShortcuts, }; diff --git a/src/modules/examples.ts b/src/modules/examples.ts index fd90ab6..e99d13f 100644 --- a/src/modules/examples.ts +++ b/src/modules/examples.ts @@ -100,6 +100,103 @@ 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.KeyTool.registerKey("event", { + id: `${config.addonRef}-key-larger`, + key: "L", + modifiers: "alt", + callback: (keyOptions) => { + addon.hooks.onShortcuts("larger"); + }, + }); + // Register an element key using for Alt+S + ztoolkit.KeyTool.registerKey("element", { + id: `${config.addonRef}-key-smaller`, + key: "S", + modifiers: "alt", + xulData: { + document, + command: cmdSmallerId, + _parentId: keysetId, + _commandOptions: { + id: cmdSmallerId, + document, + _parentId: cmdsetId, + oncommand: "Zotero.AddonTemplate.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.KeyTool.registerKey("event", { + id: `${config.addonRef}-key-smaller-conflict`, + key: "S", + modifiers: "alt", + callback: (keyOptions) => { + ztoolkit.Compat.getGlobal("alert")("Smaller! This is a conflict key."); + }, + }); + // Register an event key to check confliction + ztoolkit.KeyTool.registerKey("event", { + id: `${config.addonRef}-key-check-conflict`, + key: "C", + modifiers: "alt", + callback: (keyOptions) => { + addon.hooks.onShortcuts("confliction"); + }, + }); + ztoolkit.Tool.createProgressWindow(config.addonName) + .createLine({ + text: "Example Shortcuts: Alt+L/S/C", + type: "success", + }) + .show(); + } + + @example + static exampleShortcutLargerCallback() { + ztoolkit.Tool.createProgressWindow(config.addonName) + .createLine({ + text: "Larger!", + type: "default", + }) + .show(); + } + + @example + static exampleShortcutSmallerCallback() { + ztoolkit.Tool.createProgressWindow(config.addonName) + .createLine({ + text: "Smaller!", + type: "default", + }) + .show(); + } + + @example + static exampleShortcutConflictionCallback() { + const conflictionGroups = ztoolkit.KeyTool.checkAllKeyConfliction(); + ztoolkit.Tool.createProgressWindow("Check Key Confliction") + .createLine({ + text: `${conflictionGroups.length} groups of confliction keys found. Details are in the debug output/console.`, + }) + .show(-1); + ztoolkit.Tool.log( + "Conflictions:", + conflictionGroups, + "All keys:", + ztoolkit.KeyTool.getAllKeys() + ); + } +} + export class UIExampleFactory { @example static registerStyleSheet() {