Skip to content

Commit ee5aed7

Browse files
committed
feat(command): add command apis
1 parent 947c621 commit ee5aed7

File tree

19 files changed

+1262
-4
lines changed

19 files changed

+1262
-4
lines changed

packages/designer/src/plugin/plugin-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
IPublicModelWindow,
2020
IPublicEnumPluginRegisterLevel,
2121
IPublicApiCommonUI,
22+
IPublicApiCommand,
2223
} from '@alilc/lowcode-types';
2324
import PluginContext from './plugin-context';
2425

@@ -63,6 +64,7 @@ export interface ILowCodePluginContextPrivate {
6364
set registerLevel(level: IPublicEnumPluginRegisterLevel);
6465
set isPluginRegisteredInWorkspace(flag: boolean);
6566
set commonUI(commonUI: IPublicApiCommonUI);
67+
set command(command: IPublicApiCommand);
6668
}
6769
export interface ILowCodePluginContextApiAssembler {
6870
assembleApis(
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { IPublicApiCommand, IPublicEnumTransitionType, IPublicModelPluginContext, IPublicTypeCommand, IPublicTypeCommandHandlerArgs, IPublicTypeListCommand } from '@alilc/lowcode-types';
2+
import { checkPropTypes } from '@alilc/lowcode-utils';
3+
export interface ICommand extends Omit<IPublicApiCommand, 'registerCommand' | 'batchExecuteCommand'> {
4+
registerCommand(command: IPublicTypeCommand, options?: {
5+
commandScope?: string;
6+
}): void;
7+
8+
batchExecuteCommand(commands: { name: string; args: IPublicTypeCommandHandlerArgs }[], pluginContext?: IPublicModelPluginContext): void;
9+
}
10+
11+
export interface ICommandOptions {
12+
commandScope?: string;
13+
}
14+
15+
export class Command implements ICommand {
16+
private commands: Map<string, IPublicTypeCommand> = new Map();
17+
private commandErrors: Function[] = [];
18+
19+
registerCommand(command: IPublicTypeCommand, options?: ICommandOptions): void {
20+
if (!options?.commandScope) {
21+
throw new Error('plugin meta.commandScope is required.');
22+
}
23+
const name = `${options.commandScope}:${command.name}`;
24+
if (this.commands.has(name)) {
25+
throw new Error(`Command '${command.name}' is already registered.`);
26+
}
27+
this.commands.set(name, {
28+
...command,
29+
name,
30+
});
31+
}
32+
33+
unregisterCommand(name: string): void {
34+
if (!this.commands.has(name)) {
35+
throw new Error(`Command '${name}' is not registered.`);
36+
}
37+
this.commands.delete(name);
38+
}
39+
40+
executeCommand(name: string, args: IPublicTypeCommandHandlerArgs): void {
41+
const command = this.commands.get(name);
42+
if (!command) {
43+
throw new Error(`Command '${name}' is not registered.`);
44+
}
45+
command.parameters?.forEach(d => {
46+
if (!checkPropTypes(args[d.name], d.name, d.propType, 'command')) {
47+
throw new Error(`Command '${name}' arguments ${d.name} is invalid.`);
48+
}
49+
});
50+
try {
51+
command.handler(args);
52+
} catch (error) {
53+
if (this.commandErrors && this.commandErrors.length) {
54+
this.commandErrors.forEach(callback => callback(name, error));
55+
} else {
56+
throw error;
57+
}
58+
}
59+
}
60+
61+
batchExecuteCommand(commands: { name: string; args: IPublicTypeCommandHandlerArgs }[], pluginContext: IPublicModelPluginContext): void {
62+
if (!commands || !commands.length) {
63+
return;
64+
}
65+
pluginContext.common.utils.executeTransaction(() => {
66+
commands.forEach(command => this.executeCommand(command.name, command.args));
67+
}, IPublicEnumTransitionType.REPAINT);
68+
}
69+
70+
listCommands(): IPublicTypeListCommand[] {
71+
return Array.from(this.commands.values()).map(d => {
72+
const result: IPublicTypeListCommand = {
73+
name: d.name,
74+
};
75+
76+
if (d.description) {
77+
result.description = d.description;
78+
}
79+
80+
if (d.parameters) {
81+
result.parameters = d.parameters;
82+
}
83+
84+
return result;
85+
});
86+
}
87+
88+
onCommandError(callback: (name: string, error: Error) => void): void {
89+
this.commandErrors.push(callback);
90+
}
91+
}

packages/editor-core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from './hotkey';
66
export * from './widgets';
77
export * from './config';
88
export * from './event-bus';
9+
export * from './command';

0 commit comments

Comments
 (0)