diff --git a/packages/designer/src/plugin/plugin-context.ts b/packages/designer/src/plugin/plugin-context.ts index 7ddc3a5bd..971600ba1 100644 --- a/packages/designer/src/plugin/plugin-context.ts +++ b/packages/designer/src/plugin/plugin-context.ts @@ -42,7 +42,6 @@ export default class PluginContext implements ILowCodePluginContext, ILowCodePlu contextApiAssembler: ILowCodePluginContextApiAssembler, ) { contextApiAssembler.assembleApis(this); - this.plugins = plugins; const { pluginName = 'anonymous' } = options; 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 27891d4de..785024048 100644 --- a/packages/designer/src/plugin/plugin-manager.ts +++ b/packages/designer/src/plugin/plugin-manager.ts @@ -33,13 +33,13 @@ export class LowCodePluginManager implements ILowCodePluginManager { contextApiAssembler: ILowCodePluginContextApiAssembler; - constructor(contextApiAssembler: ILowCodePluginContextApiAssembler) { + constructor(contextApiAssembler: ILowCodePluginContextApiAssembler, readonly name = 'unknown') { this.contextApiAssembler = contextApiAssembler; } - _getLowCodePluginContext(options: IPluginContextOptions) { + _getLowCodePluginContext = (options: IPluginContextOptions) => { return new LowCodePluginContext(this, options, this.contextApiAssembler); - } + }; isEngineVersionMatched(versionExp: string): boolean { const engineVersion = engineConfig.get('ENGINE_VERSION'); diff --git a/packages/designer/src/plugin/plugin-types.ts b/packages/designer/src/plugin/plugin-types.ts index 279a1e459..644425651 100644 --- a/packages/designer/src/plugin/plugin-types.ts +++ b/packages/designer/src/plugin/plugin-types.ts @@ -10,6 +10,7 @@ import { CompositeObject, ComponentAction, MetadataTransducer, + IPublicApiPlugins, } from '@alilc/lowcode-types'; import { EngineConfig } from '@alilc/lowcode-editor-core'; import { Setters } from '../types'; @@ -126,6 +127,7 @@ export interface ILowCodePluginContextPrivate { set event(event: IPublicApiEvent); set config(config: EngineConfig); set common(common: IPublicApiCommon); + set plugins(plugins: IPublicApiPlugins); } export interface ILowCodePluginContextApiAssembler { assembleApis: (context: ILowCodePluginContextPrivate) => void; diff --git a/packages/editor-skeleton/src/skeleton.ts b/packages/editor-skeleton/src/skeleton.ts index b21aae2bf..e4759ccef 100644 --- a/packages/editor-skeleton/src/skeleton.ts +++ b/packages/editor-skeleton/src/skeleton.ts @@ -367,7 +367,7 @@ export class Skeleton { ...extraConfig, }; parsedConfig.contentProps = { - context: this.editor.get('plugins')?._getLowCodePluginContext({ + context: this.editor.get('innerPlugins')?._getLowCodePluginContext({ pluginName: 'any', }), ...(parsedConfig.contentProps || {}), diff --git a/packages/engine/src/engine-core.ts b/packages/engine/src/engine-core.ts index ab7d92740..cbad7caba 100644 --- a/packages/engine/src/engine-core.ts +++ b/packages/engine/src/engine-core.ts @@ -34,6 +34,7 @@ import { Setters, Material, Event, + Plugins, DocumentModel, Common, } from '@alilc/lowcode-shell'; @@ -82,6 +83,7 @@ const config = engineConfig; const event = new Event(editor, { prefix: 'common' }); const logger = getLogger({ level: 'warn', bizName: 'common' }); const common = new Common(editor, innerSkeleton); +let plugins: any; const pluginContextApiAssembler: ILowCodePluginContextApiAssembler = { assembleApis: (context: ILowCodePluginContextPrivate) => { @@ -93,9 +95,13 @@ const pluginContextApiAssembler: ILowCodePluginContextApiAssembler = { context.event = event; context.config = config; context.common = common; + context.plugins = plugins; }, }; -const plugins = new LowCodePluginManager(pluginContextApiAssembler).toProxy(); + +const innerPlugins = new LowCodePluginManager(pluginContextApiAssembler); +plugins = new Plugins(innerPlugins).toProxy(); +editor.set('innerPlugins' as any, innerPlugins); editor.set('plugins' as any, plugins); export { diff --git a/packages/shell/src/index.ts b/packages/shell/src/index.ts index 8b6096e9b..9a3302e1f 100644 --- a/packages/shell/src/index.ts +++ b/packages/shell/src/index.ts @@ -15,6 +15,7 @@ import Dragon from './dragon'; import SettingPropEntry from './setting-prop-entry'; import SettingTopEntry from './setting-top-entry'; import Common from './common'; +import Plugins from './plugins'; export * from './symbols'; @@ -44,4 +45,5 @@ export { Dragon, Common, getEvent, + Plugins, }; \ No newline at end of file diff --git a/packages/shell/src/plugins.ts b/packages/shell/src/plugins.ts new file mode 100644 index 000000000..f5dee678c --- /dev/null +++ b/packages/shell/src/plugins.ts @@ -0,0 +1,52 @@ +import { + LowCodePluginManager, +} from '@alilc/lowcode-designer'; +import { globalContext } from '@alilc/lowcode-editor-core'; +import { + IPublicApiPlugins, +} from '@alilc/lowcode-types'; +import { pluginsSymbol } from './symbols'; + +const innerPluginsSymbol = Symbol('plugin'); +export default class Plugins implements IPublicApiPlugins { + private readonly [innerPluginsSymbol]: LowCodePluginManager; + get [pluginsSymbol]() { + if (this.workspaceMode) { + return this[innerPluginsSymbol]; + } + const workSpace = globalContext.get('workSpace'); + if (workSpace.isActive) { + return workSpace.window.innerPlugins; + } + + return this[innerPluginsSymbol]; + } + + constructor(plugins: LowCodePluginManager, public workspaceMode: boolean = false) { + this[innerPluginsSymbol] = plugins; + } + + async register( + pluginConfigCreator: (ctx: any, options: any) => any, + options?: any, + registerOptions?: any, + ): Promise { + await this[pluginsSymbol].register(pluginConfigCreator, options, registerOptions); + } + + toProxy() { + return new Proxy(this, { + get(target, prop, receiver) { + const _target = target[pluginsSymbol]; + if (_target.pluginsMap.has(prop as string)) { + // 禁用态的插件,直接返回 undefined + if (_target.pluginsMap.get(prop as string)!.disabled) { + return undefined; + } + return _target.pluginsMap.get(prop as string)?.toProxy(); + } + return Reflect.get(target, prop, receiver); + }, + }); + } +} diff --git a/packages/shell/src/symbols.ts b/packages/shell/src/symbols.ts index 8493d4b82..fa96e1451 100644 --- a/packages/shell/src/symbols.ts +++ b/packages/shell/src/symbols.ts @@ -25,4 +25,5 @@ export const simulatorRendererSymbol = Symbol('simulatorRenderer'); export const dragObjectSymbol = Symbol('dragObject'); export const locateEventSymbol = Symbol('locateEvent'); export const designerCabinSymbol = Symbol('designerCabin'); -export const hotkeySymbol = Symbol('hotkey'); \ No newline at end of file +export const hotkeySymbol = Symbol('hotkey'); +export const pluginsSymbol = Symbol('plugins'); \ No newline at end of file diff --git a/packages/types/src/shell/api/index.ts b/packages/types/src/shell/api/index.ts index 9e5152b9e..d5f4cbcc6 100644 --- a/packages/types/src/shell/api/index.ts +++ b/packages/types/src/shell/api/index.ts @@ -5,4 +5,5 @@ export * from './material'; export * from './project'; export * from './setters'; export * from './simulator-host'; -export * from './skeleton'; \ No newline at end of file +export * from './skeleton'; +export * from './plugins'; \ No newline at end of file diff --git a/packages/types/src/shell/api/plugins.ts b/packages/types/src/shell/api/plugins.ts new file mode 100644 index 000000000..316d4a81c --- /dev/null +++ b/packages/types/src/shell/api/plugins.ts @@ -0,0 +1,7 @@ +export interface IPublicApiPlugins { + register( + pluginConfigCreator: (ctx: any, options: any) => any, + options?: any, + registerOptions?: any, + ): Promise; +} \ No newline at end of file diff --git a/packages/workspace/src/base-context.ts b/packages/workspace/src/base-context.ts index ba745fde6..237880a65 100644 --- a/packages/workspace/src/base-context.ts +++ b/packages/workspace/src/base-context.ts @@ -4,6 +4,7 @@ import { ILowCodePluginContextApiAssembler, LowCodePluginManager, ILowCodePluginContextPrivate, + Project as InnerProject, } from '@alilc/lowcode-designer'; import { Skeleton as InnerSkeleton, @@ -13,7 +14,16 @@ import { WorkSpace, } from '@alilc/lowcode-workspace'; -import { Hotkey, Project, Skeleton, Setters, Material, Event, Common } from '@alilc/lowcode-shell'; +import { + Hotkey, + Plugins, + Project, + Skeleton, + Setters, + Material, + Event, + Common, +} from '@alilc/lowcode-shell'; import { getLogger } from '@alilc/lowcode-utils'; import { setterRegistry } from 'engine/src/inner-plugins/setter-registry'; import { componentMetaParser } from 'engine/src/inner-plugins/component-meta-parser'; @@ -23,34 +33,27 @@ import { EditorWindow } from './editor-window/context'; import { shellModelFactory } from './shell-model-factory'; export class BasicContext { - skeleton; - plugins; - project; - setters; - material; + skeleton: Skeleton; + plugins: Plugins; + project: Project; + setters: Setters; + material: Material; config; event; logger; - hotkey; - innerProject; + hotkey: Hotkey; + innerProject: InnerProject; editor: Editor; - designer; - registerInnerPlugins: any; - innerSetters: any; + designer: Designer; + registerInnerPlugins: () => Promise; + innerSetters: InnerSetters; innerSkeleton: any; - innerHotkey: any; + innerHotkey: InnerHotkey; + innerPlugins: LowCodePluginManager; constructor(workSpace: WorkSpace, name: string, public editorWindow?: EditorWindow) { const editor = new Editor(name, true); - // globalContext.register(editor, Editor); - // globalContext.register(editor, 'editor'); - // if (editorWindow) { - // } - // const project = editorWindow ? editorWindow.project : new Project(innerProject); - // if (editorWindow) { - // } - const innerSkeleton = new InnerSkeleton(editor, name); editor.set('skeleton' as any, innerSkeleton); @@ -93,6 +96,7 @@ export class BasicContext { this.editor = editor; this.designer = designer; const common = new Common(editor, innerSkeleton); + let plugins: any; const pluginContextApiAssembler: ILowCodePluginContextApiAssembler = { assembleApis: (context: ILowCodePluginContextPrivate) => { @@ -104,16 +108,19 @@ export class BasicContext { context.event = event; context.config = config; context.common = common; + context.plugins = plugins; }, }; - const plugins = new LowCodePluginManager(pluginContextApiAssembler).toProxy(); + const innerPlugins = new LowCodePluginManager(pluginContextApiAssembler, name); + this.innerPlugins = innerPlugins; + plugins = new Plugins(innerPlugins, true).toProxy(); editor.set('plugins' as any, plugins); + editor.set('innerPlugins' as any, innerPlugins); this.plugins = plugins; // 注册一批内置插件 this.registerInnerPlugins = async function registerPlugins() { - // console.log('ctx', ctx); await plugins.register(componentMetaParser(designer)); await plugins.register(setterRegistry); await plugins.register(defaultPanelRegistry(editor, designer)); diff --git a/packages/workspace/src/editor-view/context.ts b/packages/workspace/src/editor-view/context.ts index 289147f6d..c9972453f 100644 --- a/packages/workspace/src/editor-view/context.ts +++ b/packages/workspace/src/editor-view/context.ts @@ -26,10 +26,10 @@ export class Context extends BasicContext { init = flow(function* (this: any) { yield this.registerInnerPlugins(); - yield this.editorView?.init(this.plugins._getLowCodePluginContext({ + yield this.editorView?.init(this.innerPlugins._getLowCodePluginContext({ pluginName: 'any', })); - yield this.plugins.init(); + yield this.innerPlugins.init(); this.isInit = true; }); } \ No newline at end of file diff --git a/packages/workspace/src/editor-window/context.ts b/packages/workspace/src/editor-window/context.ts index 84bbd2815..ae9ed5421 100644 --- a/packages/workspace/src/editor-window/context.ts +++ b/packages/workspace/src/editor-window/context.ts @@ -96,4 +96,8 @@ export class EditorWindow { get designer() { return this.editorView?.designer; } + + get innerPlugins() { + return this.editorView?.innerPlugins; + } } \ No newline at end of file