mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 06:22:22 -04:00
Implement register-package and export some packages
Exported packages are: * anki/packages * anki/shortcuts * anki/bridgecommand
This commit is contained in:
parent
9a27e44ff8
commit
6e0f7c3d53
4 changed files with 65 additions and 0 deletions
|
@ -7,6 +7,7 @@
|
|||
*/
|
||||
|
||||
import "sveltelib/export-runtime";
|
||||
import "lib/register-package";
|
||||
|
||||
import { filterHTML } from "html-filter";
|
||||
import { updateActiveButtons } from "./toolbar";
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
// Copyright: Ankitects Pty Ltd and contributors
|
||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||
|
||||
import { registerPackage } from "./register-package";
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
bridgeCommand<T>(command: string, callback?: (value: T) => void): void;
|
||||
|
@ -15,3 +17,7 @@ export function bridgeLink(command: string, label: string): string {
|
|||
export function bridgeCommand<T>(command: string, callback?: (value: T) => void): void {
|
||||
window.bridgeCommand<T>(command, callback);
|
||||
}
|
||||
|
||||
registerPackage("anki/bridgecommand", {
|
||||
bridgeCommand,
|
||||
});
|
||||
|
|
52
ts/lib/register-package.ts
Normal file
52
ts/lib/register-package.ts
Normal file
|
@ -0,0 +1,52 @@
|
|||
// Copyright: Ankitects Pty Ltd and contributors
|
||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||
|
||||
import { runtimeLibraries } from "./runtime-require";
|
||||
|
||||
export function registerPackage(
|
||||
name: string,
|
||||
entries: Record<string, unknown>,
|
||||
deprecation?: Record<string, string>
|
||||
): void {
|
||||
const pack = deprecation
|
||||
? new Proxy(entries, {
|
||||
get: (target, name: string) => {
|
||||
if (name in deprecation) {
|
||||
console.log(`anki: ${name} is deprecated: ${deprecation[name]}`);
|
||||
}
|
||||
|
||||
return target[name];
|
||||
},
|
||||
})
|
||||
: entries;
|
||||
|
||||
runtimeLibraries[name] = pack;
|
||||
}
|
||||
|
||||
function listPackages(): string[] {
|
||||
return Object.keys(runtimeLibraries);
|
||||
}
|
||||
|
||||
function hasPackages(...names: string[]): boolean {
|
||||
const libraries = listPackages();
|
||||
return names.reduce(
|
||||
(accu: boolean, name: string) => accu && libraries.includes(name),
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
function immediatelyDeprecated() {
|
||||
return false;
|
||||
}
|
||||
|
||||
registerPackage(
|
||||
"anki/packages",
|
||||
{
|
||||
listPackages,
|
||||
hasPackages,
|
||||
immediatelyDeprecated,
|
||||
},
|
||||
{
|
||||
[immediatelyDeprecated.name]: "Do not use this function",
|
||||
}
|
||||
);
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
import * as tr from "./i18n";
|
||||
import { isApplePlatform } from "./platform";
|
||||
import { registerPackage } from "./register-package";
|
||||
|
||||
export type Modifier = "Control" | "Alt" | "Shift" | "Meta";
|
||||
|
||||
|
@ -183,3 +184,8 @@ export function registerShortcut(
|
|||
document.addEventListener("keydown", handler);
|
||||
return (): void => document.removeEventListener("keydown", handler);
|
||||
}
|
||||
|
||||
registerPackage("anki/shortcuts", {
|
||||
registerShortcut,
|
||||
getPlatformString,
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue