mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-02 03:13:58 +00:00
group commands
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
<script lang="ts" context="module">
|
<script lang="ts" context="module">
|
||||||
import { commands } from '../stores';
|
import { commands } from '../stores';
|
||||||
import { get } from 'svelte/store';
|
import { get } from 'svelte/store';
|
||||||
|
import { runGroupCommand } from './runCommand';
|
||||||
|
|
||||||
export function handleCommandKeyDown(e) {
|
export function handleCommandKeyDown(e) {
|
||||||
let keyText = '';
|
let keyText = '';
|
||||||
@@ -12,10 +13,8 @@
|
|||||||
// console.log('keyText', keyText);
|
// console.log('keyText', keyText);
|
||||||
|
|
||||||
const commandsValue = get(commands);
|
const commandsValue = get(commands);
|
||||||
const command: any = Object.values(commandsValue).find(
|
const commandsFiltered: any = Object.values(commandsValue).filter(
|
||||||
(x: any) =>
|
(x: any) =>
|
||||||
x.enabled &&
|
|
||||||
!x.isGroupCommand &&
|
|
||||||
x.keyText &&
|
x.keyText &&
|
||||||
x.keyText
|
x.keyText
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
@@ -30,10 +29,23 @@
|
|||||||
.includes(keyText.toLowerCase()))
|
.includes(keyText.toLowerCase()))
|
||||||
);
|
);
|
||||||
|
|
||||||
if (command) {
|
if (commandsFiltered.length > 0) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
e.stopPropagation();
|
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>
|
</script>
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ export default async function invalidateCommands() {
|
|||||||
if (!command.isGroupCommand) continue;
|
if (!command.isGroupCommand) continue;
|
||||||
const groupSources = values.filter(x => x.group == command.group && !x.isGroupCommand && x.enabled);
|
const groupSources = values.filter(x => x.group == command.group && !x.isGroupCommand && x.enabled);
|
||||||
command.enabled = groupSources.length > 0;
|
command.enabled = groupSources.length > 0;
|
||||||
|
for(const source of groupSources) {
|
||||||
|
source.keyTextFromGroup = command.keyText;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res || dct;
|
return res || dct;
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ export interface GlobalCommand {
|
|||||||
name: string;
|
name: string;
|
||||||
text?: string /* category: name */;
|
text?: string /* category: name */;
|
||||||
keyText?: string;
|
keyText?: string;
|
||||||
|
keyTextFromGroup?: string; // automatically filled from group
|
||||||
group?: string;
|
group?: string;
|
||||||
getSubCommands?: () => SubCommand[];
|
getSubCommands?: () => SubCommand[];
|
||||||
onClick?: Function;
|
onClick?: Function;
|
||||||
|
|||||||
@@ -1,9 +1,26 @@
|
|||||||
import { get } from 'svelte/store';
|
import { getCommands } from '../stores';
|
||||||
import { commands } from '../stores';
|
|
||||||
import { GlobalCommand } from './registerCommand';
|
import { GlobalCommand } from './registerCommand';
|
||||||
|
|
||||||
export default function runCommand(commandId: string) {
|
export default function runCommand(id) {
|
||||||
const commandsValue = get(commands);
|
const commandsValue = getCommands();
|
||||||
const command: GlobalCommand = commandsValue[commandId];
|
const command = commandsValue[id];
|
||||||
if (command.enabled) command.onClick();
|
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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ export function registerFileCommands({
|
|||||||
group: 'save',
|
group: 'save',
|
||||||
category,
|
category,
|
||||||
name: 'Save',
|
name: 'Save',
|
||||||
keyText: 'Ctrl+S',
|
// keyText: 'Ctrl+S',
|
||||||
icon: 'icon save',
|
icon: 'icon save',
|
||||||
toolbar: true,
|
toolbar: true,
|
||||||
testEnabled: () => getCurrentEditor() != null,
|
testEnabled: () => getCurrentEditor() != null,
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
group: 'save',
|
group: 'save',
|
||||||
category: 'Data grid',
|
category: 'Data grid',
|
||||||
name: 'Save',
|
name: 'Save',
|
||||||
keyText: 'Ctrl+S',
|
// keyText: 'Ctrl+S',
|
||||||
toolbar: true,
|
toolbar: true,
|
||||||
icon: 'icon save',
|
icon: 'icon save',
|
||||||
testEnabled: () => getCurrentDataGrid()?.getGrider()?.allowSave,
|
testEnabled: () => getCurrentDataGrid()?.getGrider()?.allowSave,
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
if (command) {
|
if (command) {
|
||||||
return {
|
return {
|
||||||
text: command.name,
|
text: command.name,
|
||||||
keyText: command.keyText,
|
keyText: command.keyText || command.keyTextFromGroup,
|
||||||
onClick: command.onClick,
|
onClick: command.onClick,
|
||||||
disabled: !command.enabled,
|
disabled: !command.enabled,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -90,15 +90,3 @@ commands.subscribe(value => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
export const getCommands = () => commandsValue;
|
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;
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<script context="module">
|
<script context="module">
|
||||||
function getCommandTitle(command) {
|
function getCommandTitle(command) {
|
||||||
let res = command.text;
|
let res = command.text;
|
||||||
if (command.keyText) res += ` (${command.keyText})`;
|
if (command.keyText || command.keyTextFromGroup) res += ` (${command.keyText || command.keyTextFromGroup})`;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user