Files
dbgate/packages/web/src/commands/registerCommand.ts
2021-02-25 21:43:23 +01:00

41 lines
775 B
TypeScript

import { commands } from '../stores';
export interface SubCommand {
text: string;
onClick: Function;
}
export interface GlobalCommand {
id: string;
text: string;
keyText?: string;
getSubCommands?: () => SubCommand[];
onClick?: Function;
enabledStore?: any;
icon?: string;
toolbar?: boolean;
enabled?: boolean;
}
export default function registerCommand(command: GlobalCommand) {
const { enabledStore } = command;
commands.update(x => ({
...x,
[command.id]: {
...command,
enabled: !enabledStore,
},
}));
if (enabledStore) {
enabledStore.subscribe(value => {
commands.update(x => ({
...x,
[command.id]: {
...x[command.id],
enabled: value,
},
}));
});
}
}