Skip to content

Commit 39d9e07

Browse files
committed
feat: encapsulate Action2 for workbench
1 parent 7c1a08d commit 39d9e07

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/monaco/common.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {
2+
IDisposable,
3+
DisposableStore,
4+
} from 'monaco-editor/esm/vs/base/common/lifecycle';
5+
import { ContextKeyExpr } from 'monaco-editor/esm/vs/platform/contextkey/common/contextkey';
6+
import { KeybindingsRegistry } from 'monaco-editor/esm/vs/platform/keybinding/common/keybindingsRegistry';
7+
import { ServicesAccessor } from 'monaco-editor/esm/vs/platform/instantiation/common/instantiation';
8+
import { CommandsRegistry } from 'monaco-editor/esm/vs/platform/commands/common/commands';
9+
10+
export enum KeybindingWeight {
11+
EditorCore = 0,
12+
EditorContrib = 100,
13+
WorkbenchContrib = 200,
14+
BuiltinExtension = 300,
15+
ExternalExtension = 400,
16+
}
17+
18+
export abstract class Action2 {
19+
constructor(readonly desc: Readonly<any>) {}
20+
abstract run(accessor: ServicesAccessor, ...args: any[]): any;
21+
}
22+
23+
export function registerAction2(ctor: { new (): Action2 }): IDisposable {
24+
const disposables = new DisposableStore();
25+
// eslint-disable-next-line new-cap
26+
const action = new ctor();
27+
28+
const { f1, menu, keybinding, description, ...command } = action.desc;
29+
30+
// command
31+
disposables.add(
32+
CommandsRegistry.registerCommand({
33+
id: command.id,
34+
handler: (accessor, ...args) => action.run(accessor, ...args),
35+
description: description,
36+
})
37+
);
38+
39+
// keybinding
40+
if (Array.isArray(keybinding)) {
41+
for (const item of keybinding) {
42+
KeybindingsRegistry.registerKeybindingRule({
43+
...item,
44+
id: command.id,
45+
when: command.precondition
46+
? ContextKeyExpr.and(command.precondition, item.when)
47+
: item.when,
48+
});
49+
}
50+
} else if (keybinding) {
51+
KeybindingsRegistry.registerKeybindingRule({
52+
...keybinding,
53+
id: command.id,
54+
when: command.precondition
55+
? ContextKeyExpr.and(command.precondition, keybinding.when)
56+
: keybinding.when,
57+
});
58+
}
59+
60+
return disposables;
61+
}

0 commit comments

Comments
 (0)