Skip to content

Commit 3947fcc

Browse files
committed
refactor(plugin-command): add plugin-command package
1 parent de95b87 commit 3947fcc

File tree

10 files changed

+171
-65
lines changed

10 files changed

+171
-65
lines changed

.github/workflows/test packages.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,20 @@ jobs:
121121
run: npm i && npm run setup:skip-build
122122

123123
- name: test
124-
run: cd packages/editor-core && npm test
124+
run: cd packages/editor-core && npm test
125+
126+
test-plugin-command
127+
runs-on: ubuntu-latest
128+
steps:
129+
- name: checkout
130+
uses: actions/checkout@v2
131+
132+
- uses: actions/setup-node@v2
133+
with:
134+
node-version: '14'
135+
136+
- name: install
137+
run: npm i && npm run setup:skip-build
138+
139+
- name: test
140+
run: cd packages/plugin-command && npm test

packages/engine/src/engine-core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ import { defaultPanelRegistry } from './inner-plugins/default-panel-registry';
6666
import { shellModelFactory } from './modules/shell-model-factory';
6767
import { builtinHotkey } from './inner-plugins/builtin-hotkey';
6868
import { defaultContextMenu } from './inner-plugins/default-context-menu';
69-
import { defaultCommand } from './inner-plugins/default-command';
69+
import { defaultCommand } from '@alilc/lowcode-plugin-command';
7070
import { OutlinePlugin } from '@alilc/lowcode-plugin-outline-pane';
7171

7272
export * from './modules/skeleton-types';

packages/plugin-command/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# `@alilc/plugin-command`
2+
3+
> TODO: description
4+
5+
## Usage
6+
7+
```
8+
const pluginCommand = require('@alilc/plugin-command');
9+
10+
// TODO: DEMONSTRATE API
11+
```

packages/engine/tests/inner-plugins/default-command.test.ts renamed to packages/plugin-command/__tests__/node-command.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { checkPropTypes } from '@alilc/lowcode-utils/src/check-prop-types';
2-
import { nodeSchemaPropType } from '../../src/inner-plugins/default-command';
2+
import { nodeSchemaPropType } from '../src/node-command';
33

44
describe('nodeSchemaPropType', () => {
55
const componentName = 'NodeComponent';
@@ -10,8 +10,8 @@ describe('nodeSchemaPropType', () => {
1010
const invalidId = 123; // Not a string
1111
expect(checkPropTypes(validId, 'id', getPropType('id'), componentName)).toBe(true);
1212
expect(checkPropTypes(invalidId, 'id', getPropType('id'), componentName)).toBe(false);
13-
// isRequired
14-
expect(checkPropTypes(undefined, 'id', getPropType('id'), componentName)).toBe(false);
13+
// is not required
14+
expect(checkPropTypes(undefined, 'id', getPropType('id'), componentName)).toBe(true);
1515
});
1616

1717
it('should validate the componentName as a string', () => {
@@ -71,7 +71,7 @@ describe('nodeSchemaPropType', () => {
7171
const invalidLoop = { type: 'JSExpression', value: 123 }; // Not a string
7272
expect(checkPropTypes(validLoop, 'loop', getPropType('loop'), componentName)).toBe(true);
7373
expect(checkPropTypes(invalidLoop, 'loop', getPropType('loop'), componentName)).toBe(false);
74-
})
74+
});
7575

7676
it('should validate the loopArgs as an array', () => {
7777
const validLoopArgs = ['item'];

packages/plugin-command/build.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"plugins": [
3+
"@alilc/build-plugin-lce",
4+
"build-plugin-fusion",
5+
["build-plugin-moment-locales", {
6+
"locales": ["zh-cn"]
7+
}]
8+
]
9+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"plugins": [
3+
[
4+
"@alilc/build-plugin-lce",
5+
{
6+
"filename": "editor-preset-vision",
7+
"library": "LowcodeEditor",
8+
"libraryTarget": "umd",
9+
"externals": {
10+
"react": "var window.React",
11+
"react-dom": "var window.ReactDOM",
12+
"prop-types": "var window.PropTypes",
13+
"rax": "var window.Rax"
14+
}
15+
}
16+
],
17+
"@alilc/lowcode-test-mate/plugin/index.ts"
18+
]
19+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "@alilc/lowcode-plugin-command",
3+
"version": "1.3.1",
4+
"description": "> TODO: description",
5+
"author": "liujuping <[email protected]>",
6+
"homepage": "https://github.com/alibaba/lowcode-engine#readme",
7+
"license": "ISC",
8+
"main": "lib/plugin-command.js",
9+
"directories": {
10+
"lib": "lib",
11+
"test": "__tests__"
12+
},
13+
"files": [
14+
"lib"
15+
],
16+
"publishConfig": {
17+
"access": "public"
18+
},
19+
"repository": {
20+
"type": "git",
21+
"url": "git+https://github.com/alibaba/lowcode-engine.git"
22+
},
23+
"scripts": {
24+
"test": "build-scripts test --config build.test.json --jest-passWithNoTests",
25+
"build": "build-scripts build"
26+
},
27+
"bugs": {
28+
"url": "https://github.com/alibaba/lowcode-engine/issues"
29+
},
30+
"dependencies": {
31+
"@alilc/lowcode-types": "^1.3.1",
32+
"@alilc/lowcode-utils": "^1.3.1"
33+
}
34+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { IPublicModelPluginContext, IPublicTypePlugin } from '@alilc/lowcode-types';
2+
3+
export const historyCommand: IPublicTypePlugin = (ctx: IPublicModelPluginContext) => {
4+
const { command, project } = ctx;
5+
return {
6+
init() {
7+
command.registerCommand({
8+
name: 'undo',
9+
description: 'Undo the last operation.',
10+
handler: () => {
11+
const state = project.currentDocument?.history.getState() || 0;
12+
const enable = !!(state & 1);
13+
if (!enable) {
14+
throw new Error('Can not undo.');
15+
}
16+
project.currentDocument?.history.back();
17+
},
18+
});
19+
20+
command.registerCommand({
21+
name: 'redo',
22+
description: 'Redo the last operation.',
23+
handler: () => {
24+
const state = project.currentDocument?.history.getState() || 0;
25+
const enable = !!(state & 2);
26+
if (!enable) {
27+
throw new Error('Can not redo.');
28+
}
29+
project.currentDocument?.history.forward();
30+
},
31+
});
32+
},
33+
destroy() {
34+
command.unregisterCommand('history:undo');
35+
command.unregisterCommand('history:redo');
36+
},
37+
};
38+
};
39+
40+
historyCommand.pluginName = '___history_command___';
41+
historyCommand.meta = {
42+
commandScope: 'history',
43+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { IPublicModelPluginContext, IPublicTypePlugin } from '@alilc/lowcode-types';
2+
import { nodeCommand } from './node-command';
3+
import { historyCommand } from './history-command';
4+
5+
export const defaultCommand: IPublicTypePlugin = (ctx: IPublicModelPluginContext) => {
6+
const { plugins } = ctx;
7+
8+
return {
9+
async init() {
10+
await plugins.register(nodeCommand, {}, { autoInit: true });
11+
await plugins.register(historyCommand, {}, { autoInit: true });
12+
},
13+
destroy() {
14+
plugins.delete(nodeCommand.pluginName);
15+
plugins.delete(historyCommand.pluginName);
16+
},
17+
};
18+
};
19+
20+
defaultCommand.pluginName = '___default_command___';
21+
defaultCommand.meta = {
22+
commandScope: 'common',
23+
};

packages/engine/src/inner-plugins/default-command.ts renamed to packages/plugin-command/src/node-command.ts

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
IPublicModelPluginContext,
3-
IPublicTypeNodeSchema,
4-
IPublicTypePropType,
5-
} from '@alilc/lowcode-types';
1+
import { IPublicModelPluginContext, IPublicTypeNodeSchema, IPublicTypePlugin, IPublicTypePropType } from '@alilc/lowcode-types';
62
import { isNodeSchema } from '@alilc/lowcode-utils';
73

84
const sampleNodeSchema: IPublicTypePropType = {
@@ -226,45 +222,7 @@ export const nodeSchemaPropType: IPublicTypePropType = {
226222
],
227223
};
228224

229-
export const historyCommand = (ctx: IPublicModelPluginContext) => {
230-
const { command, project } = ctx;
231-
return {
232-
init() {
233-
command.registerCommand({
234-
name: 'undo',
235-
description: 'Undo the last operation.',
236-
handler: () => {
237-
const state = project.currentDocument?.history.getState() || 0;
238-
const enable = !!(state & 1);
239-
if (!enable) {
240-
throw new Error('Can not undo.');
241-
}
242-
project.currentDocument?.history.back();
243-
},
244-
});
245-
246-
command.registerCommand({
247-
name: 'redo',
248-
description: 'Redo the last operation.',
249-
handler: () => {
250-
const state = project.currentDocument?.history.getState() || 0;
251-
const enable = !!(state & 2);
252-
if (!enable) {
253-
throw new Error('Can not redo.');
254-
}
255-
project.currentDocument?.history.forward();
256-
},
257-
});
258-
},
259-
};
260-
};
261-
262-
historyCommand.pluginName = '___history_command___';
263-
historyCommand.meta = {
264-
commandScope: 'history',
265-
};
266-
267-
export const nodeCommand = (ctx: IPublicModelPluginContext) => {
225+
export const nodeCommand: IPublicTypePlugin = (ctx: IPublicModelPluginContext) => {
268226
const { command, project } = ctx;
269227
return {
270228
init() {
@@ -521,6 +479,14 @@ export const nodeCommand = (ctx: IPublicModelPluginContext) => {
521479
],
522480
});
523481
},
482+
destroy() {
483+
command.unregisterCommand('node:add');
484+
command.unregisterCommand('node:move');
485+
command.unregisterCommand('node:remove');
486+
command.unregisterCommand('node:update');
487+
command.unregisterCommand('node:updateProps');
488+
command.unregisterCommand('node:removeProps');
489+
},
524490
};
525491
};
526492

@@ -529,18 +495,3 @@ nodeCommand.meta = {
529495
commandScope: 'node',
530496
};
531497

532-
export const defaultCommand = (ctx: IPublicModelPluginContext) => {
533-
const { plugins } = ctx;
534-
plugins.register(nodeCommand);
535-
plugins.register(historyCommand);
536-
537-
return {
538-
init() {
539-
},
540-
};
541-
};
542-
543-
defaultCommand.pluginName = '___default_command___';
544-
defaultCommand.meta = {
545-
commandScope: 'common',
546-
};

0 commit comments

Comments
 (0)