X Tutup
Skip to content

Commit 163416f

Browse files
liujupingJackLian
authored andcommitted
feat: support the use of events in workspace mode to communicate in different views
1 parent c7c7d93 commit 163416f

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

packages/editor-core/src/editor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,5 @@ export class Editor extends (EventEmitter as any) implements IPublicModelEditor
326326
}
327327
}
328328
}
329+
330+
export const commonEvent = new EventBus(new EventEmitter());

packages/engine/src/engine-core.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { render, unmountComponentAtNode } from 'react-dom';
55
import {
66
globalContext,
77
Editor,
8+
commonEvent,
89
engineConfig,
910
Setters as InnerSetters,
1011
Hotkey as InnerHotkey,
@@ -87,7 +88,7 @@ editor.set('setters' as any, setters);
8788
editor.set('material', material);
8889
editor.set('innerHotkey', innerHotkey);
8990
const config = engineConfig;
90-
const event = new Event(editor, { prefix: 'common' });
91+
const event = new Event(commonEvent, { prefix: 'common' });
9192
const logger = new Logger({ level: 'warn', bizName: 'common' });
9293
const common = new Common(editor, innerSkeleton);
9394
const canvas = new Canvas(editor);
@@ -102,7 +103,7 @@ const pluginContextApiAssembler: ILowCodePluginContextApiAssembler = {
102103
context.setters = setters;
103104
context.material = material;
104105
const eventPrefix = meta?.eventPrefix || 'common';
105-
context.event = new Event(editor, { prefix: eventPrefix });
106+
context.event = new Event(commonEvent, { prefix: eventPrefix });
106107
context.config = config;
107108
context.common = common;
108109
context.canvas = canvas;

packages/shell/src/api/event.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Editor as InnerEditor, globalContext } from '@alilc/lowcode-editor-core';
1+
import { Editor as InnerEditor, EventBus } from '@alilc/lowcode-editor-core';
22
import { getLogger, isPublicEventName, isPluginEventName } from '@alilc/lowcode-utils';
3-
import { editorSymbol } from '../symbols';
43
import { IPublicApiEvent, IPublicTypeDisposable } from '@alilc/lowcode-types';
54

65
const logger = getLogger({ level: 'warn', bizName: 'shell-event' });
@@ -9,10 +8,10 @@ type EventOptions = {
98
prefix: string;
109
};
1110

12-
const innerEditorSymbol = Symbol('editor');
11+
const eventBusSymbol = Symbol('eventBus');
1312

1413
export class Event implements IPublicApiEvent {
15-
private readonly [editorSymbol]: InnerEditor;
14+
private readonly [eventBusSymbol]: EventBus;
1615
private readonly options: EventOptions;
1716

1817
// TODO:
@@ -21,8 +20,8 @@ export class Event implements IPublicApiEvent {
2120
*/
2221
readonly names = [];
2322

24-
constructor(editor: InnerEditor, options: EventOptions, public workspaceMode = false) {
25-
this[editorSymbol] = editor;
23+
constructor(eventBus: EventBus, options: EventOptions, public workspaceMode = false) {
24+
this[eventBusSymbol] = eventBus;
2625
this.options = options;
2726
if (!this.options.prefix) {
2827
logger.warn('prefix is required while initializing Event');
@@ -36,7 +35,7 @@ export class Event implements IPublicApiEvent {
3635
*/
3736
on(event: string, listener: (...args: any[]) => void): IPublicTypeDisposable {
3837
if (isPluginEventName(event) || isPublicEventName(event)) {
39-
return this[editorSymbol].eventBus.on(event, listener);
38+
return this[eventBusSymbol].on(event, listener);
4039
} else {
4140
logger.warn(`fail to monitor on event ${event}, which is neither a engine public event nor a plugin event`);
4241
return () => {};
@@ -49,7 +48,7 @@ export class Event implements IPublicApiEvent {
4948
* @param listener 事件回调
5049
*/
5150
off(event: string, listener: (...args: any[]) => void) {
52-
this[editorSymbol].eventBus.off(event, listener);
51+
this[eventBusSymbol].off(event, listener);
5352
}
5453

5554
/**
@@ -63,7 +62,7 @@ export class Event implements IPublicApiEvent {
6362
logger.warn('Event#emit has been forbidden while prefix is not specified');
6463
return;
6564
}
66-
this[editorSymbol].eventBus.emit(`${this.options.prefix}:${event}`, ...args);
65+
this[eventBusSymbol].emit(`${this.options.prefix}:${event}`, ...args);
6766
}
6867

6968
/**
@@ -72,10 +71,10 @@ export class Event implements IPublicApiEvent {
7271
* @param args
7372
*/
7473
__internalEmit__(event: string, ...args: unknown[]) {
75-
this[editorSymbol].emit(event, ...args);
74+
this[eventBusSymbol].emit(event, ...args);
7675
}
7776
}
7877

7978
export function getEvent(editor: InnerEditor, options: any = { prefix: 'common' }) {
80-
return new Event(editor, options);
79+
return new Event(editor.eventBus, options);
8180
}

packages/workspace/src/base-context.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Editor,
55
engineConfig, Setters as InnerSetters,
66
Hotkey as InnerHotkey,
7+
commonEvent,
78
} from '@alilc/lowcode-editor-core';
89
import {
910
Designer,
@@ -80,7 +81,7 @@ export class BasicContext {
8081
const material = new Material(editor, true);
8182
const project = new Project(innerProject, true);
8283
const config = engineConfig;
83-
const event = new Event(editor, { prefix: 'common' });
84+
const event = new Event(commonEvent, { prefix: 'common' });
8485
const logger = getLogger({ level: 'warn', bizName: 'common' });
8586
const skeleton = new Skeleton(innerSkeleton, 'any', true);
8687
editor.set('setters', setters);
@@ -114,8 +115,7 @@ export class BasicContext {
114115
context.setters = setters;
115116
context.material = material;
116117
const eventPrefix = meta?.eventPrefix || 'common';
117-
context.event = new Event(editor, { prefix: eventPrefix });
118-
context.event = event;
118+
context.event = new Event(commonEvent, { prefix: eventPrefix });
119119
context.config = config;
120120
context.common = common;
121121
context.plugins = plugins;

0 commit comments

Comments
 (0)
X Tutup