context menu

This commit is contained in:
Jan Prochazka
2021-03-20 08:16:32 +01:00
parent dcb4c5071a
commit efc07280a6
8 changed files with 106 additions and 20 deletions

View File

@@ -30,11 +30,38 @@ 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;
}
// for (const source of groupSources) {
// source.keyTextFromGroup = command.keyText;
// }
}
}
return res || dct;
});
}
let isInvalidatedDefinitions = false;
export async function invalidateCommandDefinitions() {
if (isInvalidatedDefinitions) return;
isInvalidatedDefinitions = true;
await tick();
isInvalidatedDefinitions = false;
commands.update(dct => {
let res = { ...dct };
const values = Object.values(res) as GlobalCommand[];
// test enabled for group commands
for (const command of values) {
if (!command.isGroupCommand) continue;
const groupSources = values.filter(x => x.group == command.group && !x.isGroupCommand);
for (const source of groupSources) {
source.keyTextFromGroup = command.keyText;
}
}
return res;
});
invalidateCommands();
}

View File

@@ -1,5 +1,5 @@
import { commands } from '../stores';
import invalidateCommands from './invalidateCommands';
import { invalidateCommandDefinitions } from './invalidateCommands';
export interface SubCommand {
text: string;
@@ -39,16 +39,5 @@ export default function registerCommand(command: GlobalCommand) {
enabled: !testEnabled,
},
}));
invalidateCommands();
// if (enabledStore) {
// enabledStore.subscribe(value => {
// commands.update(x => ({
// ...x,
// [command.id]: {
// ...x[command.id],
// enabled: value,
// },
// }));
// });
// }
invalidateCommandDefinitions();
}

View File

@@ -142,6 +142,24 @@ registerCommand({
group: 'saveAs',
});
registerCommand({
id: 'group.undo',
category: null,
isGroupCommand: true,
name: 'Undo',
keyText: 'Ctrl+Z',
group: 'undo',
});
registerCommand({
id: 'group.redo',
category: null,
isGroupCommand: true,
name: 'Redo',
keyText: 'Ctrl+Y',
group: 'redo',
});
if (electron) {
registerCommand({
id: 'file.open',
@@ -175,6 +193,7 @@ export function registerFileCommands({
execute = false,
toggleComment = false,
findReplace = false,
undoRedo = false,
}) {
registerCommand({
id: idPrefix + '.save',
@@ -248,4 +267,22 @@ export function registerFileCommands({
onClick: () => getCurrentEditor().replace(),
});
}
if (undoRedo) {
registerCommand({
id: idPrefix + '.undo',
category,
name: 'Undo',
group: 'undo',
testEnabled: () => getCurrentEditor()?.canUndo(),
onClick: () => getCurrentEditor().undo(),
});
registerCommand({
id: idPrefix + '.redo',
category,
group: 'redo',
name: 'Replace',
testEnabled: () => getCurrentEditor()?.canRedo(),
onClick: () => getCurrentEditor().redo(),
});
}
}