add: shortcut examples for toolkit 0.1.6

This commit is contained in:
xiangyu 2023-01-09 00:44:26 +08:00
parent d42abcbfcc
commit a95515fc47
4 changed files with 129 additions and 2 deletions

View File

@ -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)

View File

@ -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",

View File

@ -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,
};

View File

@ -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 <key> 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() {