forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskeleton.ts
More file actions
406 lines (376 loc) · 10.7 KB
/
skeleton.ts
File metadata and controls
406 lines (376 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import { Editor, action, makeObservable } from '@alilc/lowcode-editor-core';
import {
DockConfig,
PanelConfig,
WidgetConfig,
IWidgetBaseConfig,
PanelDockConfig,
DialogDockConfig,
isDockConfig,
isPanelDockConfig,
isPanelConfig,
DividerConfig,
isDividerConfig,
IWidgetConfigArea,
} from './types';
import Panel, { isPanel } from './widget/panel';
import WidgetContainer from './widget/widget-container';
import Area from './area';
import Widget, { isWidget, IWidget } from './widget/widget';
import PanelDock from './widget/panel-dock';
import Dock from './widget/dock';
import { Stage, StageConfig } from './widget/stage';
import { isValidElement } from 'react';
import { isPlainObject, uniqueId } from '@alilc/lowcode-utils';
import { Divider } from '@alifd/next';
import { EditorConfig, PluginClassSet } from '@alilc/lowcode-types';
export enum SkeletonEvents {
PANEL_DOCK_ACTIVE = 'skeleton.panel-dock.active',
PANEL_DOCK_UNACTIVE = 'skeleton.panel-dock.unactive',
PANEL_SHOW = 'skeleton.panel.show',
PANEL_HIDE = 'skeleton.panel.hide',
WIDGET_SHOW = 'skeleton.widget.show',
WIDGET_HIDE = 'skeleton.widget.hide',
WIDGET_DISABLE = 'skeleton.widget.disable',
WIDGET_ENABLE = 'skeleton.widget.enable',
}
export class Skeleton {
private panels = new Map<string, Panel>();
private containers = new Map<string, WidgetContainer<any>>();
readonly leftArea: Area<DockConfig | PanelDockConfig | DialogDockConfig>;
readonly topArea: Area<DockConfig | DividerConfig | PanelDockConfig | DialogDockConfig>;
readonly toolbar: Area<DockConfig | DividerConfig | PanelDockConfig | DialogDockConfig>;
readonly leftFixedArea: Area<PanelConfig, Panel>;
readonly leftFloatArea: Area<PanelConfig, Panel>;
readonly rightArea: Area<PanelConfig, Panel>;
readonly mainArea: Area<WidgetConfig | PanelConfig, Widget | Panel>;
readonly bottomArea: Area<PanelConfig, Panel>;
readonly stages: Area<StageConfig, Stage>;
constructor(readonly editor: Editor) {
makeObservable(this);
this.leftArea = new Area(
this,
'leftArea',
(config) => {
if (isWidget(config)) {
return config;
}
return this.createWidget(config);
},
false,
);
this.topArea = new Area(
this,
'topArea',
(config) => {
if (isWidget(config)) {
return config;
}
return this.createWidget(config);
},
false,
);
this.toolbar = new Area(
this,
'toolbar',
(config) => {
if (isWidget(config)) {
return config;
}
return this.createWidget(config);
},
false,
);
this.leftFixedArea = new Area(
this,
'leftFixedArea',
(config) => {
if (isPanel(config)) {
return config;
}
return this.createPanel(config);
},
true,
);
this.leftFloatArea = new Area(
this,
'leftFloatArea',
(config) => {
if (isPanel(config)) {
return config;
}
return this.createPanel(config);
},
true,
);
this.rightArea = new Area(
this,
'rightArea',
(config) => {
if (isPanel(config)) {
return config;
}
return this.createPanel(config);
},
false,
true,
);
this.mainArea = new Area(
this,
'mainArea',
(config) => {
if (isWidget(config)) {
return config as Widget;
}
return this.createWidget(config) as Widget;
},
true,
true,
);
this.bottomArea = new Area(
this,
'bottomArea',
(config) => {
if (isPanel(config)) {
return config;
}
return this.createPanel(config);
},
true,
);
this.stages = new Area(this, 'stages', (config) => {
if (isWidget(config)) {
return config;
}
return new Stage(this, config);
});
this.setupPlugins();
this.setupEvents();
}
/**
* setup events
*
* @memberof Skeleton
*/
setupEvents() {
// adjust pinned status when panel shown
this.editor.on('skeleton.panel.show', (panelName, panel) => {
const panelNameKey = `${panelName}-pinned-status-isFloat`;
const isInFloatAreaPreferenceExists = this.editor?.getPreference()?.contains(panelNameKey, 'skeleton');
if (isInFloatAreaPreferenceExists) {
const isInFloatAreaFromPreference = this.editor?.getPreference()?.get(panelNameKey, 'skeleton');
const isCurrentInFloatArea = panel?.isChildOfFloatArea();
if (isInFloatAreaFromPreference !== isCurrentInFloatArea) {
this.toggleFloatStatus(panel);
}
}
});
}
/**
* set isFloat status for panel
*
* @param {*} panel
* @memberof Skeleton
*/
@action
toggleFloatStatus(panel: Panel) {
const isFloat = panel?.parent?.name === 'leftFloatArea';
if (isFloat) {
this.leftFloatArea.remove(panel);
this.leftFixedArea.add(panel);
this.leftFixedArea.container.active(panel);
} else {
this.leftFixedArea.remove(panel);
this.leftFloatArea.add(panel);
this.leftFloatArea.container.active(panel);
}
this.editor?.getPreference()?.set(`${panel.name}-pinned-status-isFloat`, !isFloat, 'skeleton');
}
buildFromConfig(config?: EditorConfig, components: PluginClassSet = {}) {
if (config) {
this.editor.init(config, components);
}
this.setupPlugins();
}
private setupPlugins() {
const { config, components = {} } = this.editor;
if (!config) {
return;
}
const { plugins } = config;
if (!plugins) {
return;
}
Object.keys(plugins).forEach((area) => {
plugins[area].forEach((item) => {
const { pluginKey, type, props = {}, pluginProps } = item;
const config: Partial<IWidgetBaseConfig> = {
area: area as IWidgetConfigArea,
type: 'Widget',
name: pluginKey,
contentProps: pluginProps,
};
const { dialogProps, balloonProps, panelProps, linkProps, ...restProps } = props;
config.props = restProps;
if (dialogProps) {
config.dialogProps = dialogProps;
}
if (balloonProps) {
config.balloonProps = balloonProps;
}
if (panelProps) {
config.panelProps = panelProps;
}
if (linkProps) {
config.linkProps = linkProps;
}
if (type === 'TabPanel') {
config.type = 'Panel';
} else if (/Icon$/.test(type)) {
config.type = type.replace('Icon', 'Dock');
}
if (pluginKey in components) {
config.content = components[pluginKey];
}
this.add(config as IWidgetBaseConfig);
});
});
}
postEvent(event: SkeletonEvents, ...args: any[]) {
this.editor.emit(event, ...args);
}
readonly widgets: IWidget[] = [];
createWidget(config: IWidgetBaseConfig | IWidget) {
if (isWidget(config)) {
return config;
}
config = this.parseConfig(config);
let widget: IWidget;
if (isDockConfig(config)) {
if (isPanelDockConfig(config)) {
widget = new PanelDock(this, config);
} else if (false) {
// DialogDock
// others...
} else {
widget = new Dock(this, config);
}
} else if (isDividerConfig(config)) {
widget = new Widget(this, {
...config,
type: 'Widget',
content: Divider,
});
} else if (isPanelConfig(config)) {
widget = this.createPanel(config);
} else {
widget = new Widget(this, config as WidgetConfig);
}
this.widgets.push(widget);
return widget;
}
getWidget(name: string): IWidget | undefined {
return this.widgets.find(widget => widget.name === name);
}
createPanel(config: PanelConfig) {
const parsedConfig = this.parseConfig(config);
const panel = new Panel(this, parsedConfig as PanelConfig);
this.panels.set(panel.name, panel);
return panel;
}
getPanel(name: string): Panel | undefined {
return this.panels.get(name);
}
getStage(name: string) {
return this.stages.container.get(name);
}
createStage(config: any) {
const stage = this.add({
name: uniqueId('stage'),
area: 'stages',
...config,
});
return stage?.getName?.();
}
createContainer(
name: string,
handle: (item: any) => any,
exclusive = false,
checkVisible: () => boolean = () => true,
defaultSetCurrent = false,
) {
const container = new WidgetContainer(name, handle, exclusive, checkVisible, defaultSetCurrent);
this.containers.set(name, container);
return container;
}
private parseConfig(config: IWidgetBaseConfig) {
if (config.parsed) {
return config;
}
const { content, ...restConfig } = config;
if (content) {
if (isPlainObject(content) && !isValidElement(content)) {
Object.keys(content).forEach((key) => {
if (/props$/i.test(key) && restConfig[key]) {
restConfig[key] = {
...restConfig[key],
...content[key],
};
} else {
restConfig[key] = content[key];
}
});
} else {
restConfig.content = content;
}
}
restConfig.pluginKey = restConfig.name;
restConfig.parsed = true;
return restConfig;
}
add(config: IWidgetBaseConfig, extraConfig?: Record<string, any>) {
const parsedConfig = {
...this.parseConfig(config),
...extraConfig,
};
let { area } = parsedConfig;
if (!area) {
if (parsedConfig.type === 'Panel') {
area = 'leftFloatArea';
} else if (parsedConfig.type === 'Widget') {
area = 'mainArea';
} else {
area = 'leftArea';
}
}
switch (area) {
case 'leftArea':
case 'left':
return this.leftArea.add(parsedConfig as PanelDockConfig);
case 'rightArea':
case 'right':
return this.rightArea.add(parsedConfig as PanelConfig);
case 'topArea':
case 'top':
return this.topArea.add(parsedConfig as PanelDockConfig);
case 'toolbar':
return this.toolbar.add(parsedConfig as PanelDockConfig);
case 'mainArea':
case 'main':
case 'center':
case 'centerArea':
return this.mainArea.add(parsedConfig as PanelConfig);
case 'bottomArea':
case 'bottom':
return this.bottomArea.add(parsedConfig as PanelConfig);
case 'leftFixedArea':
return this.leftFixedArea.add(parsedConfig as PanelConfig);
case 'leftFloatArea':
return this.leftFloatArea.add(parsedConfig as PanelConfig);
case 'stages':
return this.stages.add(parsedConfig as StageConfig);
default:
// do nothing
}
}
}