diff --git a/packages/designer/src/plugin/plugin-context.ts b/packages/designer/src/plugin/plugin-context.ts index 740e70ba1..60b71fee0 100644 --- a/packages/designer/src/plugin/plugin-context.ts +++ b/packages/designer/src/plugin/plugin-context.ts @@ -1,4 +1,4 @@ -import { Editor, engineConfig } from '@ali/lowcode-editor-core'; +import { Editor, EngineConfig, engineConfig } from '@ali/lowcode-editor-core'; import { Designer } from '@ali/lowcode-designer'; import { Skeleton as InnerSkeleton } from '@ali/lowcode-editor-skeleton'; import { @@ -7,12 +7,13 @@ import { Skeleton, Setters, Material, + Event, editorSymbol, designerSymbol, skeletonSymbol, } from '@ali/lowcode-shell'; -import { getLogger, Logger } from '../utils/logger'; -import { ILowCodePluginContext } from './plugin-types'; +import { getLogger, Logger } from '@ali/lowcode-utils'; +import { ILowCodePluginContext, PluginContextOptions } from './plugin-types'; /** * 一些 API 设计约定: @@ -29,14 +30,15 @@ export default class PluginContext implements ILowCodePluginContext { public logger: Logger; public setters: Setters; public material: Material; + public config: EngineConfig; + public event: Event; - constructor(editor: Editor) { + constructor(editor: Editor, options: PluginContextOptions) { this[editorSymbol] = editor; const designer = this[designerSymbol] = editor.get('designer')!; const skeleton = this[skeletonSymbol] = editor.get('skeleton')!; - // TODO: to be deleted - // this.editor = editor; + const { pluginName = 'anonymous' } = options; const project = designer.project; this.hotkey = new Hotkey(); this.project = new Project(project); @@ -44,7 +46,7 @@ export default class PluginContext implements ILowCodePluginContext { this.setters = new Setters(); this.material = new Material(editor); this.config = engineConfig; - // TODO: pluginName - this.logger = getLogger({ level: 'warn', bizName: 'designer:plugin:' }); + this.event = new Event(editor, { prefix: `plugin:${pluginName}` }); + this.logger = getLogger({ level: 'warn', bizName: `designer:plugin:${pluginName}` }); } } diff --git a/packages/designer/src/plugin/plugin-manager.ts b/packages/designer/src/plugin/plugin-manager.ts index 184083e23..7d70ed670 100644 --- a/packages/designer/src/plugin/plugin-manager.ts +++ b/packages/designer/src/plugin/plugin-manager.ts @@ -1,8 +1,16 @@ import { Editor } from '@ali/lowcode-editor-core'; -import { ILowCodePlugin, ILowCodePluginConfig, ILowCodePluginManager, ILowCodePluginContext, LowCodeRegisterOptions } from './plugin-types'; +import { getLogger } from '@ali/lowcode-utils'; +import { + ILowCodePlugin, + ILowCodePluginConfig, + ILowCodePluginManager, + ILowCodePluginContext, + LowCodeRegisterOptions, + PluginContextOptions, +} from './plugin-types'; import { LowCodePlugin } from './plugin'; import LowCodePluginContext from './plugin-context'; -import { getLogger, invariant } from '../utils'; +import { invariant } from '../utils'; import sequencify from './sequencify'; const logger = getLogger({ level: 'warn', bizName: 'designer:pluginManager' }); @@ -18,24 +26,22 @@ export class LowCodePluginManager implements ILowCodePluginManager { this.editor = editor; } - private _getLowCodePluginContext(config: any) { - return new LowCodePluginContext(this.editor, config); + private _getLowCodePluginContext(options: PluginContextOptions) { + return new LowCodePluginContext(this.editor, options); } - // private getNewContext(config: any) { - // return new LowCodePluginContext2(this.editor, config); - // } - async register( pluginConfigCreator: (ctx: ILowCodePluginContext, pluginOptions?: any) => ILowCodePluginConfig, pluginOptions?: any, options?: LowCodeRegisterOptions, ): Promise { - const ctx = this._getLowCodePluginContext({ name: pluginConfigCreator.pluginName }); - // ctx.newCtx = this.getNewContext(); + const ctx = this._getLowCodePluginContext({ pluginName: pluginConfigCreator.pluginName }); const config = pluginConfigCreator(ctx, pluginOptions); - invariant(config.name, `${config.name} required`, config); - // ctx.setLogger(config); + invariant( + pluginConfigCreator.pluginName || config.name, + 'pluginConfigCreator.pluginName required', + config, + ); const allowOverride = options?.override === true; if (this.pluginsMap.has(config.name)) { if (!allowOverride) { @@ -43,7 +49,12 @@ export class LowCodePluginManager implements ILowCodePluginManager { } else { // clear existing plugin const originalPlugin = this.pluginsMap.get(config.name); - logger.log('plugin override, originalPlugin with name ', config.name, ' will be destroyed, config:', originalPlugin?.config); + logger.log( + 'plugin override, originalPlugin with name ', + config.name, + ' will be destroyed, config:', + originalPlugin?.config, + ); originalPlugin?.destroy(); this.pluginsMap.delete(config.name); } @@ -70,7 +81,7 @@ export class LowCodePluginManager implements ILowCodePluginManager { } async delete(pluginName: string): Promise { - const idx = this.plugins.findIndex(plugin => plugin.name === pluginName); + const idx = this.plugins.findIndex((plugin) => plugin.name === pluginName); if (idx === -1) return false; const plugin = this.plugins[idx]; await plugin.destroy(); @@ -82,7 +93,7 @@ export class LowCodePluginManager implements ILowCodePluginManager { async init() { const pluginNames: string[] = []; const pluginObj: { [name: string]: ILowCodePlugin } = {}; - this.plugins.forEach(plugin => { + this.plugins.forEach((plugin) => { pluginNames.push(plugin.name); pluginObj[plugin.name] = plugin; }); @@ -94,7 +105,9 @@ export class LowCodePluginManager implements ILowCodePluginManager { try { await this.pluginsMap.get(pluginName)!.init(); } catch (e) { - logger.error(`Failed to init plugin:${pluginName}, it maybe affect those plugins which depend on this.`); + logger.error( + `Failed to init plugin:${pluginName}, it maybe affect those plugins which depend on this.`, + ); logger.error(e); } } diff --git a/packages/designer/src/plugin/plugin-types.ts b/packages/designer/src/plugin/plugin-types.ts index a76c83cd5..aac8528ae 100644 --- a/packages/designer/src/plugin/plugin-types.ts +++ b/packages/designer/src/plugin/plugin-types.ts @@ -6,6 +6,7 @@ import { MetadataTransducer, Designer, } from '@ali/lowcode-designer'; +import { Event, Material } from '@ali/lowcode-shell'; import { Setters, Utils } from '../types'; export interface ILowCodePluginConfig { @@ -55,7 +56,8 @@ export interface ILowCodePluginContext { setters: Setters; // utils: Utils; engineConfig: EngineConfig; - material: any; + material: Material; + event: Event; } interface ILowCodePluginManagerPluginAccessor { @@ -83,3 +85,7 @@ export type LowCodeRegisterOptions = { // allow overriding existing plugin with same name when override === true override?: boolean; }; + +export type PluginContextOptions = { + pluginName: string; +}; diff --git a/packages/designer/src/plugin/plugin.ts b/packages/designer/src/plugin/plugin.ts index f99b3acee..c95ac7eb4 100644 --- a/packages/designer/src/plugin/plugin.ts +++ b/packages/designer/src/plugin/plugin.ts @@ -1,11 +1,12 @@ +import { CompositeObject } from '@ali/lowcode-types'; +import { getLogger, Logger } from '@ali/lowcode-utils'; import { ILowCodePlugin, ILowCodePluginConfig, ILowCodePluginManager, } from './plugin-types'; -import { CompositeObject } from '@ali/lowcode-types'; import { EventEmitter } from 'events'; -import { getLogger, Logger, invariant } from '../utils'; +import { invariant } from '../utils'; export class LowCodePlugin implements ILowCodePlugin { config: ILowCodePluginConfig; diff --git a/packages/designer/src/utils/index.ts b/packages/designer/src/utils/index.ts index ab4a166b5..785586dcb 100644 --- a/packages/designer/src/utils/index.ts +++ b/packages/designer/src/utils/index.ts @@ -1,4 +1,3 @@ export * from './invariant'; export * from './slot'; export * from './tree'; -export * from './logger'; diff --git a/packages/shell/src/event.ts b/packages/shell/src/event.ts index 44c3a9d20..04de387ff 100644 --- a/packages/shell/src/event.ts +++ b/packages/shell/src/event.ts @@ -1,3 +1,34 @@ -export default class Event { +import { Editor as InnerEditor } from '@ali/lowcode-editor-core'; +import { getLogger } from '@ali/lowcode-utils'; +import { editorSymbol } from './symbols'; -} \ No newline at end of file +const logger = getLogger({ level: 'warn', bizName: 'shell:event:' }); + +type EventOptions = { + prefix: string; +}; + +export default class Event { + private readonly [editorSymbol]: InnerEditor; + private readonly options: EventOptions; + + constructor(editor: InnerEditor, options: EventOptions) { + this[editorSymbol] = editor; + this.options = options; + if (!this.options.prefix) { + logger.warn('prefix is required while initializing Event'); + } + } + + on(event: string, listener: (...args: unknown[]) => void) { + this[editorSymbol].on(event, listener); + } + + emit(event: string, ...args: unknown[]) { + if (!this.options.prefix) { + logger.warn('Event#emit has been forbidden while prefix is not specified'); + return; + } + this[editorSymbol].emit(`${this.options.prefix}:${event}`, ...args); + } +} diff --git a/packages/shell/src/prop.ts b/packages/shell/src/prop.ts index ffe20599c..f53de57c3 100644 --- a/packages/shell/src/prop.ts +++ b/packages/shell/src/prop.ts @@ -1,5 +1,5 @@ import { Prop as InnerProp } from '@ali/lowcode-designer'; -import { CompositeValue } from '@ali/lowcode-types'; +import { CompositeValue, TransformStage } from '@ali/lowcode-types'; import { propSymbol } from './symbols'; import Node from './node'; @@ -27,5 +27,7 @@ export default class Prop { return this[propSymbol].getValue(); } - exportSchema() {} + exportSchema(stage: TransformStage) { + return this[propSymbol].export(stage); + } } \ No newline at end of file diff --git a/packages/utils/package.json b/packages/utils/package.json index dbf04371d..dbe55a10d 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -15,7 +15,8 @@ "@ali/lowcode-types": "1.0.74", "@alifd/next": "^1.19.16", "lodash": "^4.17.21", - "react": "^16" + "react": "^16", + "zen-logger": "^1.1.0" }, "devDependencies": { "@alib/build-scripts": "^0.1.18", diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 874a15e72..1277f4481 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -24,4 +24,5 @@ export * from './misc'; export * from './schema'; export * from './node-helper'; export * from './clone-enumerable-property'; +export * from './logger'; export * as css from './css-helper'; diff --git a/packages/designer/src/utils/logger.ts b/packages/utils/src/logger.ts similarity index 100% rename from packages/designer/src/utils/logger.ts rename to packages/utils/src/logger.ts