Skip to content

Commit

Permalink
refactor(plugin-command): add plugin-command package
Browse files Browse the repository at this point in the history
  • Loading branch information
liujuping committed Feb 1, 2024
1 parent de95b87 commit 3947fcc
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 65 deletions.
18 changes: 17 additions & 1 deletion .github/workflows/test packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,20 @@ jobs:
run: npm i && npm run setup:skip-build

- name: test
run: cd packages/editor-core && npm test
run: cd packages/editor-core && npm test

test-plugin-command
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v2

- uses: actions/setup-node@v2
with:
node-version: '14'

- name: install
run: npm i && npm run setup:skip-build

- name: test
run: cd packages/plugin-command && npm test
2 changes: 1 addition & 1 deletion packages/engine/src/engine-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import { defaultPanelRegistry } from './inner-plugins/default-panel-registry';
import { shellModelFactory } from './modules/shell-model-factory';
import { builtinHotkey } from './inner-plugins/builtin-hotkey';
import { defaultContextMenu } from './inner-plugins/default-context-menu';
import { defaultCommand } from './inner-plugins/default-command';
import { defaultCommand } from '@alilc/lowcode-plugin-command';
import { OutlinePlugin } from '@alilc/lowcode-plugin-outline-pane';

export * from './modules/skeleton-types';
Expand Down
11 changes: 11 additions & 0 deletions packages/plugin-command/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `@alilc/plugin-command`

> TODO: description
## Usage

```
const pluginCommand = require('@alilc/plugin-command');
// TODO: DEMONSTRATE API
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { checkPropTypes } from '@alilc/lowcode-utils/src/check-prop-types';
import { nodeSchemaPropType } from '../../src/inner-plugins/default-command';
import { nodeSchemaPropType } from '../src/node-command';

describe('nodeSchemaPropType', () => {
const componentName = 'NodeComponent';
Expand All @@ -10,8 +10,8 @@ describe('nodeSchemaPropType', () => {
const invalidId = 123; // Not a string
expect(checkPropTypes(validId, 'id', getPropType('id'), componentName)).toBe(true);
expect(checkPropTypes(invalidId, 'id', getPropType('id'), componentName)).toBe(false);
// isRequired
expect(checkPropTypes(undefined, 'id', getPropType('id'), componentName)).toBe(false);
// is not required
expect(checkPropTypes(undefined, 'id', getPropType('id'), componentName)).toBe(true);
});

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

it('should validate the loopArgs as an array', () => {
const validLoopArgs = ['item'];
Expand Down
9 changes: 9 additions & 0 deletions packages/plugin-command/build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": [
"@alilc/build-plugin-lce",
"build-plugin-fusion",
["build-plugin-moment-locales", {
"locales": ["zh-cn"]
}]
]
}
19 changes: 19 additions & 0 deletions packages/plugin-command/build.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"plugins": [
[
"@alilc/build-plugin-lce",
{
"filename": "editor-preset-vision",
"library": "LowcodeEditor",
"libraryTarget": "umd",
"externals": {
"react": "var window.React",
"react-dom": "var window.ReactDOM",
"prop-types": "var window.PropTypes",
"rax": "var window.Rax"
}
}
],
"@alilc/lowcode-test-mate/plugin/index.ts"
]
}
34 changes: 34 additions & 0 deletions packages/plugin-command/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@alilc/lowcode-plugin-command",
"version": "1.3.1",
"description": "> TODO: description",
"author": "liujuping <[email protected]>",
"homepage": "https://github.com/alibaba/lowcode-engine#readme",
"license": "ISC",
"main": "lib/plugin-command.js",
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [
"lib"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/alibaba/lowcode-engine.git"
},
"scripts": {
"test": "build-scripts test --config build.test.json --jest-passWithNoTests",
"build": "build-scripts build"
},
"bugs": {
"url": "https://github.com/alibaba/lowcode-engine/issues"
},
"dependencies": {
"@alilc/lowcode-types": "^1.3.1",
"@alilc/lowcode-utils": "^1.3.1"
}
}
43 changes: 43 additions & 0 deletions packages/plugin-command/src/history-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { IPublicModelPluginContext, IPublicTypePlugin } from '@alilc/lowcode-types';

export const historyCommand: IPublicTypePlugin = (ctx: IPublicModelPluginContext) => {
const { command, project } = ctx;
return {
init() {
command.registerCommand({
name: 'undo',
description: 'Undo the last operation.',
handler: () => {
const state = project.currentDocument?.history.getState() || 0;
const enable = !!(state & 1);
if (!enable) {
throw new Error('Can not undo.');
}
project.currentDocument?.history.back();
},
});

command.registerCommand({
name: 'redo',
description: 'Redo the last operation.',
handler: () => {
const state = project.currentDocument?.history.getState() || 0;
const enable = !!(state & 2);
if (!enable) {
throw new Error('Can not redo.');
}
project.currentDocument?.history.forward();
},
});
},
destroy() {
command.unregisterCommand('history:undo');
command.unregisterCommand('history:redo');
},
};
};

historyCommand.pluginName = '___history_command___';
historyCommand.meta = {
commandScope: 'history',
};
23 changes: 23 additions & 0 deletions packages/plugin-command/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { IPublicModelPluginContext, IPublicTypePlugin } from '@alilc/lowcode-types';
import { nodeCommand } from './node-command';
import { historyCommand } from './history-command';

export const defaultCommand: IPublicTypePlugin = (ctx: IPublicModelPluginContext) => {
const { plugins } = ctx;

return {
async init() {
await plugins.register(nodeCommand, {}, { autoInit: true });
await plugins.register(historyCommand, {}, { autoInit: true });
},
destroy() {
plugins.delete(nodeCommand.pluginName);
plugins.delete(historyCommand.pluginName);
},
};
};

defaultCommand.pluginName = '___default_command___';
defaultCommand.meta = {
commandScope: 'common',
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
IPublicModelPluginContext,
IPublicTypeNodeSchema,
IPublicTypePropType,
} from '@alilc/lowcode-types';
import { IPublicModelPluginContext, IPublicTypeNodeSchema, IPublicTypePlugin, IPublicTypePropType } from '@alilc/lowcode-types';
import { isNodeSchema } from '@alilc/lowcode-utils';

const sampleNodeSchema: IPublicTypePropType = {
Expand Down Expand Up @@ -226,45 +222,7 @@ export const nodeSchemaPropType: IPublicTypePropType = {
],
};

export const historyCommand = (ctx: IPublicModelPluginContext) => {
const { command, project } = ctx;
return {
init() {
command.registerCommand({
name: 'undo',
description: 'Undo the last operation.',
handler: () => {
const state = project.currentDocument?.history.getState() || 0;
const enable = !!(state & 1);
if (!enable) {
throw new Error('Can not undo.');
}
project.currentDocument?.history.back();
},
});

command.registerCommand({
name: 'redo',
description: 'Redo the last operation.',
handler: () => {
const state = project.currentDocument?.history.getState() || 0;
const enable = !!(state & 2);
if (!enable) {
throw new Error('Can not redo.');
}
project.currentDocument?.history.forward();
},
});
},
};
};

historyCommand.pluginName = '___history_command___';
historyCommand.meta = {
commandScope: 'history',
};

export const nodeCommand = (ctx: IPublicModelPluginContext) => {
export const nodeCommand: IPublicTypePlugin = (ctx: IPublicModelPluginContext) => {
const { command, project } = ctx;
return {
init() {
Expand Down Expand Up @@ -521,6 +479,14 @@ export const nodeCommand = (ctx: IPublicModelPluginContext) => {
],
});
},
destroy() {
command.unregisterCommand('node:add');
command.unregisterCommand('node:move');
command.unregisterCommand('node:remove');
command.unregisterCommand('node:update');
command.unregisterCommand('node:updateProps');
command.unregisterCommand('node:removeProps');
},
};
};

Expand All @@ -529,18 +495,3 @@ nodeCommand.meta = {
commandScope: 'node',
};

export const defaultCommand = (ctx: IPublicModelPluginContext) => {
const { plugins } = ctx;
plugins.register(nodeCommand);
plugins.register(historyCommand);

return {
init() {
},
};
};

defaultCommand.pluginName = '___default_command___';
defaultCommand.meta = {
commandScope: 'common',
};

0 comments on commit 3947fcc

Please sign in to comment.