feat: support the use of events in workspace mode to communicate in different views

This commit is contained in:
liujuping 2022-12-23 20:16:12 +08:00 committed by 林熠
parent c7c7d93e1a
commit 163416fcfe
4 changed files with 18 additions and 16 deletions

View File

@ -326,3 +326,5 @@ export class Editor extends (EventEmitter as any) implements IPublicModelEditor
}
}
}
export const commonEvent = new EventBus(new EventEmitter());

View File

@ -5,6 +5,7 @@ import { render, unmountComponentAtNode } from 'react-dom';
import {
globalContext,
Editor,
commonEvent,
engineConfig,
Setters as InnerSetters,
Hotkey as InnerHotkey,
@ -87,7 +88,7 @@ editor.set('setters' as any, setters);
editor.set('material', material);
editor.set('innerHotkey', innerHotkey);
const config = engineConfig;
const event = new Event(editor, { prefix: 'common' });
const event = new Event(commonEvent, { prefix: 'common' });
const logger = new Logger({ level: 'warn', bizName: 'common' });
const common = new Common(editor, innerSkeleton);
const canvas = new Canvas(editor);
@ -102,7 +103,7 @@ const pluginContextApiAssembler: ILowCodePluginContextApiAssembler = {
context.setters = setters;
context.material = material;
const eventPrefix = meta?.eventPrefix || 'common';
context.event = new Event(editor, { prefix: eventPrefix });
context.event = new Event(commonEvent, { prefix: eventPrefix });
context.config = config;
context.common = common;
context.canvas = canvas;

View File

@ -1,6 +1,5 @@
import { Editor as InnerEditor, globalContext } from '@alilc/lowcode-editor-core';
import { Editor as InnerEditor, EventBus } from '@alilc/lowcode-editor-core';
import { getLogger, isPublicEventName, isPluginEventName } from '@alilc/lowcode-utils';
import { editorSymbol } from '../symbols';
import { IPublicApiEvent, IPublicTypeDisposable } from '@alilc/lowcode-types';
const logger = getLogger({ level: 'warn', bizName: 'shell-event' });
@ -9,10 +8,10 @@ type EventOptions = {
prefix: string;
};
const innerEditorSymbol = Symbol('editor');
const eventBusSymbol = Symbol('eventBus');
export class Event implements IPublicApiEvent {
private readonly [editorSymbol]: InnerEditor;
private readonly [eventBusSymbol]: EventBus;
private readonly options: EventOptions;
// TODO:
@ -21,8 +20,8 @@ export class Event implements IPublicApiEvent {
*/
readonly names = [];
constructor(editor: InnerEditor, options: EventOptions, public workspaceMode = false) {
this[editorSymbol] = editor;
constructor(eventBus: EventBus, options: EventOptions, public workspaceMode = false) {
this[eventBusSymbol] = eventBus;
this.options = options;
if (!this.options.prefix) {
logger.warn('prefix is required while initializing Event');
@ -36,7 +35,7 @@ export class Event implements IPublicApiEvent {
*/
on(event: string, listener: (...args: any[]) => void): IPublicTypeDisposable {
if (isPluginEventName(event) || isPublicEventName(event)) {
return this[editorSymbol].eventBus.on(event, listener);
return this[eventBusSymbol].on(event, listener);
} else {
logger.warn(`fail to monitor on event ${event}, which is neither a engine public event nor a plugin event`);
return () => {};
@ -49,7 +48,7 @@ export class Event implements IPublicApiEvent {
* @param listener
*/
off(event: string, listener: (...args: any[]) => void) {
this[editorSymbol].eventBus.off(event, listener);
this[eventBusSymbol].off(event, listener);
}
/**
@ -63,7 +62,7 @@ export class Event implements IPublicApiEvent {
logger.warn('Event#emit has been forbidden while prefix is not specified');
return;
}
this[editorSymbol].eventBus.emit(`${this.options.prefix}:${event}`, ...args);
this[eventBusSymbol].emit(`${this.options.prefix}:${event}`, ...args);
}
/**
@ -72,10 +71,10 @@ export class Event implements IPublicApiEvent {
* @param args
*/
__internalEmit__(event: string, ...args: unknown[]) {
this[editorSymbol].emit(event, ...args);
this[eventBusSymbol].emit(event, ...args);
}
}
export function getEvent(editor: InnerEditor, options: any = { prefix: 'common' }) {
return new Event(editor, options);
return new Event(editor.eventBus, options);
}

View File

@ -4,6 +4,7 @@ import {
Editor,
engineConfig, Setters as InnerSetters,
Hotkey as InnerHotkey,
commonEvent,
} from '@alilc/lowcode-editor-core';
import {
Designer,
@ -80,7 +81,7 @@ export class BasicContext {
const material = new Material(editor, true);
const project = new Project(innerProject, true);
const config = engineConfig;
const event = new Event(editor, { prefix: 'common' });
const event = new Event(commonEvent, { prefix: 'common' });
const logger = getLogger({ level: 'warn', bizName: 'common' });
const skeleton = new Skeleton(innerSkeleton, 'any', true);
editor.set('setters', setters);
@ -114,8 +115,7 @@ export class BasicContext {
context.setters = setters;
context.material = material;
const eventPrefix = meta?.eventPrefix || 'common';
context.event = new Event(editor, { prefix: eventPrefix });
context.event = event;
context.event = new Event(commonEvent, { prefix: eventPrefix });
context.config = config;
context.common = common;
context.plugins = plugins;