group commands

This commit is contained in:
Jan Prochazka 2021-03-15 21:06:15 +01:00
parent d4a35fb414
commit 321d5d71de
9 changed files with 49 additions and 28 deletions

View File

@ -1,7 +1,8 @@
<script lang="ts" context="module">
import { commands } from '../stores';
import { get } from 'svelte/store';
import { runGroupCommand } from './runCommand';
export function handleCommandKeyDown(e) {
let keyText = '';
if (e.ctrlKey) keyText += 'Ctrl+';
@ -12,10 +13,8 @@
// console.log('keyText', keyText);
const commandsValue = get(commands);
const command: any = Object.values(commandsValue).find(
const commandsFiltered: any = Object.values(commandsValue).filter(
(x: any) =>
x.enabled &&
!x.isGroupCommand &&
x.keyText &&
x.keyText
.toLowerCase()
@ -30,10 +29,23 @@
.includes(keyText.toLowerCase()))
);
if (command) {
if (commandsFiltered.length > 0) {
e.preventDefault();
e.stopPropagation();
command.onClick();
}
const notGroup = commandsFiltered.filter(x => x.enabled && !x.isGroupCommand);
if (notGroup.length == 1) {
const command = notGroup[0];
if (command.onClick) command.onClick();
return;
}
const group = commandsFiltered.filter(x => x.enabled && x.isGroupCommand);
if (group.length == 1) {
const command = group[0];
runGroupCommand(command.group);
}
}
</script>

View File

@ -30,6 +30,9 @@ export default async function invalidateCommands() {
if (!command.isGroupCommand) continue;
const groupSources = values.filter(x => x.group == command.group && !x.isGroupCommand && x.enabled);
command.enabled = groupSources.length > 0;
for(const source of groupSources) {
source.keyTextFromGroup = command.keyText;
}
}
}
return res || dct;

View File

@ -12,6 +12,7 @@ export interface GlobalCommand {
name: string;
text?: string /* category: name */;
keyText?: string;
keyTextFromGroup?: string; // automatically filled from group
group?: string;
getSubCommands?: () => SubCommand[];
onClick?: Function;

View File

@ -1,9 +1,26 @@
import { get } from 'svelte/store';
import { commands } from '../stores';
import { getCommands } from '../stores';
import { GlobalCommand } from './registerCommand';
export default function runCommand(commandId: string) {
const commandsValue = get(commands);
const command: GlobalCommand = commandsValue[commandId];
if (command.enabled) command.onClick();
export default function runCommand(id) {
const commandsValue = getCommands();
const command = commandsValue[id];
if (command) {
if (!command.enabled) return;
if (command.isGroupCommand) {
runGroupCommand(command.group);
} else {
if (command.onClick) {
command.onClick();
}
}
}
}
window['dbgate_runCommand'] = runCommand;
export function runGroupCommand(group) {
const commandsValue = getCommands();
const values = Object.values(commandsValue) as GlobalCommand[];
const real = values.find(x => x.group == group && !x.isGroupCommand && x.enabled);
if (real && real.onClick) real.onClick();
}

View File

@ -134,7 +134,7 @@ export function registerFileCommands({
group: 'save',
category,
name: 'Save',
keyText: 'Ctrl+S',
// keyText: 'Ctrl+S',
icon: 'icon save',
toolbar: true,
testEnabled: () => getCurrentEditor() != null,

View File

@ -19,7 +19,7 @@
group: 'save',
category: 'Data grid',
name: 'Save',
keyText: 'Ctrl+S',
// keyText: 'Ctrl+S',
toolbar: true,
icon: 'icon save',
testEnabled: () => getCurrentDataGrid()?.getGrider()?.allowSave,

View File

@ -31,7 +31,7 @@
if (command) {
return {
text: command.name,
keyText: command.keyText,
keyText: command.keyText || command.keyTextFromGroup,
onClick: command.onClick,
disabled: !command.enabled,
};

View File

@ -90,15 +90,3 @@ commands.subscribe(value => {
}
});
export const getCommands = () => commandsValue;
export function runCommand(id) {
const command = commandsValue[id];
if (command) {
if (command.isGroupCommand) {
const values = Object.values(commandsValue) as GlobalCommand[];
const real = values.find(x => x.group == command.group && !x.isGroupCommand && x.enabled);
if (real && real.onClick) real.onClick();
}
command.onClick();
}
}
window['dbgate_runCommand'] = runCommand;

View File

@ -1,7 +1,7 @@
<script context="module">
function getCommandTitle(command) {
let res = command.text;
if (command.keyText) res += ` (${command.keyText})`;
if (command.keyText || command.keyTextFromGroup) res += ` (${command.keyText || command.keyTextFromGroup})`;
return res;
}
</script>