diff --git a/.github/workflows/test packages.yml b/.github/workflows/test packages.yml index abb297370..573a1ade0 100644 --- a/.github/workflows/test packages.yml +++ b/.github/workflows/test packages.yml @@ -121,4 +121,20 @@ jobs: run: npm i && npm run setup:skip-build - name: test - run: cd packages/editor-core && npm test \ No newline at end of file + 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 \ No newline at end of file diff --git a/packages/engine/src/engine-core.ts b/packages/engine/src/engine-core.ts index 778438a3d..5a2cbbc65 100644 --- a/packages/engine/src/engine-core.ts +++ b/packages/engine/src/engine-core.ts @@ -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'; diff --git a/packages/plugin-command/README.md b/packages/plugin-command/README.md new file mode 100644 index 000000000..8476b47e5 --- /dev/null +++ b/packages/plugin-command/README.md @@ -0,0 +1,11 @@ +# `@alilc/plugin-command` + +> TODO: description + +## Usage + +``` +const pluginCommand = require('@alilc/plugin-command'); + +// TODO: DEMONSTRATE API +``` diff --git a/packages/engine/tests/inner-plugins/default-command.test.ts b/packages/plugin-command/__tests__/node-command.test.ts similarity index 97% rename from packages/engine/tests/inner-plugins/default-command.test.ts rename to packages/plugin-command/__tests__/node-command.test.ts index 0e28c642b..2e9d21b35 100644 --- a/packages/engine/tests/inner-plugins/default-command.test.ts +++ b/packages/plugin-command/__tests__/node-command.test.ts @@ -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']; diff --git a/packages/plugin-command/build.json b/packages/plugin-command/build.json new file mode 100644 index 000000000..d0aec1038 --- /dev/null +++ b/packages/plugin-command/build.json @@ -0,0 +1,9 @@ +{ + "plugins": [ + "@alilc/build-plugin-lce", + "build-plugin-fusion", + ["build-plugin-moment-locales", { + "locales": ["zh-cn"] + }] + ] +} diff --git a/packages/plugin-command/build.test.json b/packages/plugin-command/build.test.json new file mode 100644 index 000000000..9596d43e7 --- /dev/null +++ b/packages/plugin-command/build.test.json @@ -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" + ] +} diff --git a/packages/plugin-command/package.json b/packages/plugin-command/package.json new file mode 100644 index 000000000..3c84e265e --- /dev/null +++ b/packages/plugin-command/package.json @@ -0,0 +1,34 @@ +{ + "name": "@alilc/lowcode-plugin-command", + "version": "1.3.1", + "description": "> TODO: description", + "author": "liujuping ", + "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" + } +} diff --git a/packages/plugin-command/src/history-command.ts b/packages/plugin-command/src/history-command.ts new file mode 100644 index 000000000..ea7e491bc --- /dev/null +++ b/packages/plugin-command/src/history-command.ts @@ -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', +}; diff --git a/packages/plugin-command/src/index.ts b/packages/plugin-command/src/index.ts new file mode 100644 index 000000000..8e5d64ebc --- /dev/null +++ b/packages/plugin-command/src/index.ts @@ -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', +}; diff --git a/packages/engine/src/inner-plugins/default-command.ts b/packages/plugin-command/src/node-command.ts similarity index 88% rename from packages/engine/src/inner-plugins/default-command.ts rename to packages/plugin-command/src/node-command.ts index 68d65c500..eeda1d168 100644 --- a/packages/engine/src/inner-plugins/default-command.ts +++ b/packages/plugin-command/src/node-command.ts @@ -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', -};