refactor(plugin-command): add plugin-command package

This commit is contained in:
liujuping 2024-02-01 12:23:31 +08:00 committed by 林熠
parent de95b87b1e
commit 24393211b4
10 changed files with 171 additions and 65 deletions

View File

@ -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

View File

@ -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';

View File

@ -0,0 +1,11 @@
# `@alilc/plugin-command`
> TODO: description
## Usage
```
const pluginCommand = require('@alilc/plugin-command');
// TODO: DEMONSTRATE API
```

View File

@ -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';
@ -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', () => {
@ -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'];

View File

@ -0,0 +1,9 @@
{
"plugins": [
"@alilc/build-plugin-lce",
"build-plugin-fusion",
["build-plugin-moment-locales", {
"locales": ["zh-cn"]
}]
]
}

View File

@ -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"
]
}

View File

@ -0,0 +1,34 @@
{
"name": "@alilc/lowcode-plugin-command",
"version": "1.3.1",
"description": "> TODO: description",
"author": "liujuping <liujup@foxmail.com>",
"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"
}
}

View File

@ -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',
};

View File

@ -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',
};

View File

@ -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 = {
@ -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() {
@ -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');
},
};
};
@ -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',
};