mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2025-12-12 19:52:51 +00:00
feat: support the use of events in workspace mode to communicate in different views
This commit is contained in:
parent
c7c7d93e1a
commit
163416fcfe
@ -326,3 +326,5 @@ export class Editor extends (EventEmitter as any) implements IPublicModelEditor
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const commonEvent = new EventBus(new EventEmitter());
|
||||||
@ -5,6 +5,7 @@ import { render, unmountComponentAtNode } from 'react-dom';
|
|||||||
import {
|
import {
|
||||||
globalContext,
|
globalContext,
|
||||||
Editor,
|
Editor,
|
||||||
|
commonEvent,
|
||||||
engineConfig,
|
engineConfig,
|
||||||
Setters as InnerSetters,
|
Setters as InnerSetters,
|
||||||
Hotkey as InnerHotkey,
|
Hotkey as InnerHotkey,
|
||||||
@ -87,7 +88,7 @@ editor.set('setters' as any, setters);
|
|||||||
editor.set('material', material);
|
editor.set('material', material);
|
||||||
editor.set('innerHotkey', innerHotkey);
|
editor.set('innerHotkey', innerHotkey);
|
||||||
const config = engineConfig;
|
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 logger = new Logger({ level: 'warn', bizName: 'common' });
|
||||||
const common = new Common(editor, innerSkeleton);
|
const common = new Common(editor, innerSkeleton);
|
||||||
const canvas = new Canvas(editor);
|
const canvas = new Canvas(editor);
|
||||||
@ -102,7 +103,7 @@ const pluginContextApiAssembler: ILowCodePluginContextApiAssembler = {
|
|||||||
context.setters = setters;
|
context.setters = setters;
|
||||||
context.material = material;
|
context.material = material;
|
||||||
const eventPrefix = meta?.eventPrefix || 'common';
|
const eventPrefix = meta?.eventPrefix || 'common';
|
||||||
context.event = new Event(editor, { prefix: eventPrefix });
|
context.event = new Event(commonEvent, { prefix: eventPrefix });
|
||||||
context.config = config;
|
context.config = config;
|
||||||
context.common = common;
|
context.common = common;
|
||||||
context.canvas = canvas;
|
context.canvas = canvas;
|
||||||
|
|||||||
@ -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 { getLogger, isPublicEventName, isPluginEventName } from '@alilc/lowcode-utils';
|
||||||
import { editorSymbol } from '../symbols';
|
|
||||||
import { IPublicApiEvent, IPublicTypeDisposable } from '@alilc/lowcode-types';
|
import { IPublicApiEvent, IPublicTypeDisposable } from '@alilc/lowcode-types';
|
||||||
|
|
||||||
const logger = getLogger({ level: 'warn', bizName: 'shell-event' });
|
const logger = getLogger({ level: 'warn', bizName: 'shell-event' });
|
||||||
@ -9,10 +8,10 @@ type EventOptions = {
|
|||||||
prefix: string;
|
prefix: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
const innerEditorSymbol = Symbol('editor');
|
const eventBusSymbol = Symbol('eventBus');
|
||||||
|
|
||||||
export class Event implements IPublicApiEvent {
|
export class Event implements IPublicApiEvent {
|
||||||
private readonly [editorSymbol]: InnerEditor;
|
private readonly [eventBusSymbol]: EventBus;
|
||||||
private readonly options: EventOptions;
|
private readonly options: EventOptions;
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
@ -21,8 +20,8 @@ export class Event implements IPublicApiEvent {
|
|||||||
*/
|
*/
|
||||||
readonly names = [];
|
readonly names = [];
|
||||||
|
|
||||||
constructor(editor: InnerEditor, options: EventOptions, public workspaceMode = false) {
|
constructor(eventBus: EventBus, options: EventOptions, public workspaceMode = false) {
|
||||||
this[editorSymbol] = editor;
|
this[eventBusSymbol] = eventBus;
|
||||||
this.options = options;
|
this.options = options;
|
||||||
if (!this.options.prefix) {
|
if (!this.options.prefix) {
|
||||||
logger.warn('prefix is required while initializing Event');
|
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 {
|
on(event: string, listener: (...args: any[]) => void): IPublicTypeDisposable {
|
||||||
if (isPluginEventName(event) || isPublicEventName(event)) {
|
if (isPluginEventName(event) || isPublicEventName(event)) {
|
||||||
return this[editorSymbol].eventBus.on(event, listener);
|
return this[eventBusSymbol].on(event, listener);
|
||||||
} else {
|
} else {
|
||||||
logger.warn(`fail to monitor on event ${event}, which is neither a engine public event nor a plugin event`);
|
logger.warn(`fail to monitor on event ${event}, which is neither a engine public event nor a plugin event`);
|
||||||
return () => {};
|
return () => {};
|
||||||
@ -49,7 +48,7 @@ export class Event implements IPublicApiEvent {
|
|||||||
* @param listener 事件回调
|
* @param listener 事件回调
|
||||||
*/
|
*/
|
||||||
off(event: string, listener: (...args: any[]) => void) {
|
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');
|
logger.warn('Event#emit has been forbidden while prefix is not specified');
|
||||||
return;
|
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
|
* @param args
|
||||||
*/
|
*/
|
||||||
__internalEmit__(event: string, ...args: unknown[]) {
|
__internalEmit__(event: string, ...args: unknown[]) {
|
||||||
this[editorSymbol].emit(event, ...args);
|
this[eventBusSymbol].emit(event, ...args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getEvent(editor: InnerEditor, options: any = { prefix: 'common' }) {
|
export function getEvent(editor: InnerEditor, options: any = { prefix: 'common' }) {
|
||||||
return new Event(editor, options);
|
return new Event(editor.eventBus, options);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import {
|
|||||||
Editor,
|
Editor,
|
||||||
engineConfig, Setters as InnerSetters,
|
engineConfig, Setters as InnerSetters,
|
||||||
Hotkey as InnerHotkey,
|
Hotkey as InnerHotkey,
|
||||||
|
commonEvent,
|
||||||
} from '@alilc/lowcode-editor-core';
|
} from '@alilc/lowcode-editor-core';
|
||||||
import {
|
import {
|
||||||
Designer,
|
Designer,
|
||||||
@ -80,7 +81,7 @@ export class BasicContext {
|
|||||||
const material = new Material(editor, true);
|
const material = new Material(editor, true);
|
||||||
const project = new Project(innerProject, true);
|
const project = new Project(innerProject, true);
|
||||||
const config = engineConfig;
|
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 logger = getLogger({ level: 'warn', bizName: 'common' });
|
||||||
const skeleton = new Skeleton(innerSkeleton, 'any', true);
|
const skeleton = new Skeleton(innerSkeleton, 'any', true);
|
||||||
editor.set('setters', setters);
|
editor.set('setters', setters);
|
||||||
@ -114,8 +115,7 @@ export class BasicContext {
|
|||||||
context.setters = setters;
|
context.setters = setters;
|
||||||
context.material = material;
|
context.material = material;
|
||||||
const eventPrefix = meta?.eventPrefix || 'common';
|
const eventPrefix = meta?.eventPrefix || 'common';
|
||||||
context.event = new Event(editor, { prefix: eventPrefix });
|
context.event = new Event(commonEvent, { prefix: eventPrefix });
|
||||||
context.event = event;
|
|
||||||
context.config = config;
|
context.config = config;
|
||||||
context.common = common;
|
context.common = common;
|
||||||
context.plugins = plugins;
|
context.plugins = plugins;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user