forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaterial.ts
More file actions
213 lines (188 loc) · 5.91 KB
/
material.ts
File metadata and controls
213 lines (188 loc) · 5.91 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
import { globalContext } from '@alilc/lowcode-editor-core';
import {
IDesigner,
isComponentMeta,
} from '@alilc/lowcode-designer';
import { IPublicTypeAssetsJson, getLogger } from '@alilc/lowcode-utils';
import {
IPublicTypeComponentAction,
IPublicTypeComponentMetadata,
IPublicApiMaterial,
IPublicTypeMetadataTransducer,
IPublicModelComponentMeta,
IPublicTypeNpmInfo,
IPublicModelEditor,
IPublicTypeDisposable,
IPublicTypeContextMenuAction,
IPublicTypeContextMenuItem,
} from '@alilc/lowcode-types';
import { Workspace as InnerWorkspace } from '@alilc/lowcode-workspace';
import { editorSymbol, designerSymbol } from '../symbols';
import { ComponentMeta as ShellComponentMeta } from '../model';
import { ComponentType } from 'react';
const logger = getLogger({ level: 'warn', bizName: 'shell-material' });
const innerEditorSymbol = Symbol('editor');
export class Material implements IPublicApiMaterial {
private readonly [innerEditorSymbol]: IPublicModelEditor;
get [editorSymbol](): IPublicModelEditor {
if (this.workspaceMode) {
return this[innerEditorSymbol];
}
const workspace: InnerWorkspace = globalContext.get('workspace');
if (workspace.isActive) {
if (!workspace.window.editor) {
logger.error('Material api 调用时机出现问题,请检查');
return this[innerEditorSymbol];
}
return workspace.window.editor;
}
return this[innerEditorSymbol];
}
get [designerSymbol](): IDesigner {
return this[editorSymbol].get('designer')!;
}
constructor(editor: IPublicModelEditor, readonly workspaceMode: boolean = false) {
this[innerEditorSymbol] = editor;
}
/**
* 获取组件 map 结构
*/
get componentsMap(): { [key: string]: IPublicTypeNpmInfo | ComponentType<any> | object } {
return this[designerSymbol].componentsMap;
}
/**
* 设置「资产包」结构
* @param assets
* @returns
*/
async setAssets(assets: IPublicTypeAssetsJson) {
return await this[editorSymbol].setAssets(assets);
}
/**
* 获取「资产包」结构
* @returns
*/
getAssets(): IPublicTypeAssetsJson | undefined {
return this[editorSymbol].get('assets');
}
/**
* 加载增量的「资产包」结构,该增量包会与原有的合并
* @param incrementalAssets
* @returns
*/
loadIncrementalAssets(incrementalAssets: IPublicTypeAssetsJson) {
return this[designerSymbol].loadIncrementalAssets(incrementalAssets);
}
/**
* 注册物料元数据管道函数
* @param transducer
* @param level
* @param id
*/
registerMetadataTransducer = (
transducer: IPublicTypeMetadataTransducer,
level?: number,
id?: string | undefined,
) => {
this[designerSymbol].componentActions.registerMetadataTransducer(transducer, level, id);
};
/**
* 获取所有物料元数据管道函数
* @returns
*/
getRegisteredMetadataTransducers() {
return this[designerSymbol].componentActions.getRegisteredMetadataTransducers();
}
/**
* 获取指定名称的物料元数据
* @param componentName
* @returns
*/
getComponentMeta(componentName: string): IPublicModelComponentMeta | null {
const innerMeta = this[designerSymbol].getComponentMeta(componentName);
return ShellComponentMeta.create(innerMeta);
}
/**
* create an instance of ComponentMeta by given metadata
* @param metadata
* @returns
*/
createComponentMeta(metadata: IPublicTypeComponentMetadata) {
return ShellComponentMeta.create(this[designerSymbol].createComponentMeta(metadata));
}
/**
* test if the given object is a ComponentMeta instance or not
* @param obj
* @returns
*/
isComponentMeta(obj: any) {
return isComponentMeta(obj);
}
/**
* 获取所有已注册的物料元数据
* @returns
*/
getComponentMetasMap(): Map<string, IPublicModelComponentMeta> {
const map = new Map<string, IPublicModelComponentMeta>();
const originalMap = this[designerSymbol].getComponentMetasMap();
for (let componentName of originalMap.keys()) {
map.set(componentName, this.getComponentMeta(componentName)!);
}
return map;
}
/**
* 在设计器辅助层增加一个扩展 action
* @param action
*/
addBuiltinComponentAction = (action: IPublicTypeComponentAction) => {
this[designerSymbol].componentActions.addBuiltinComponentAction(action);
};
/**
* 刷新 componentMetasMap,可触发模拟器里的 components 重新构建
*/
refreshComponentMetasMap = () => {
this[designerSymbol].refreshComponentMetasMap();
};
/**
* 移除设计器辅助层的指定 action
* @param name
*/
removeBuiltinComponentAction(name: string) {
this[designerSymbol].componentActions.removeBuiltinComponentAction(name);
}
/**
* 修改已有的设计器辅助层的指定 action
* @param actionName
* @param handle
*/
modifyBuiltinComponentAction(
actionName: string,
handle: (action: IPublicTypeComponentAction) => void,
) {
this[designerSymbol].componentActions.modifyBuiltinComponentAction(actionName, handle);
}
/**
* 监听 assets 变化的事件
* @param fn
*/
onChangeAssets(fn: () => void): IPublicTypeDisposable {
const dispose = [
// 设置 assets,经过 setAssets 赋值
this[editorSymbol].onChange('assets', fn),
// 增量设置 assets,经过 loadIncrementalAssets 赋值
this[editorSymbol].eventBus.on('designer.incrementalAssetsReady', fn),
];
return () => {
dispose.forEach(d => d && d());
};
}
addContextMenuOption(option: IPublicTypeContextMenuAction) {
this[designerSymbol].contextMenuActions.addMenuAction(option);
}
removeContextMenuOption(name: string) {
this[designerSymbol].contextMenuActions.removeMenuAction(name);
}
adjustContextMenuLayout(fn: (actions: IPublicTypeContextMenuItem[]) => IPublicTypeContextMenuItem[]) {
this[designerSymbol].contextMenuActions.adjustMenuLayout(fn);
}
}