forked from alibaba/lowcode-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.tsx
More file actions
317 lines (288 loc) · 10.1 KB
/
utils.tsx
File metadata and controls
317 lines (288 loc) · 10.1 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
import * as queryString from 'query-string';
import fetchJsonp from 'fetch-jsonp';
import * as React from 'react';
import { pascal } from 'case';
import { Notification } from '@alifd/next';
import { buildComponents } from '@alilc/lowcode-utils';
const typeMap = {
vc: ['prototype', 'view'],
vs: ['setter'],
vp: ['plugin'],
ve: ['pane'],
vu: ['utils'],
plugin: ['designerPlugin'],
component: ['meta', 'view'],
};
const queryFlag = '__injectFrom'; // 不推荐
const injectTypeFlag = '__injectType'; // 不推荐
const injectEnvFlag = '__injectEnv'; // 不推荐
const debugFlag = 'debug'; // 推荐
const arrayFlag = '__components';
const jsonpFlag = '__injectComponent';
const prototypeKeyFlag = '__prototype';
const injectDeviceFlag = '__device';
window[arrayFlag] = [];
window[jsonpFlag] = function addComponents(component) {
window[arrayFlag].push(component);
};
let injectServerHost = '127.0.0.1';
const searchParams = new URLSearchParams(window.location.search);
// 是否需要开启 inject 逻辑
export const needInject = searchParams.get('__injectFrom') // 历史兼容
|| searchParams.get('__injectType') === 'auto' // 历史兼容
|| searchParams.has('debug')
|| (window as any).injectConfig;
let urlCache = null;
export function setInjectServerHost(finalInjectServerHost) {
injectServerHost = finalInjectServerHost;
console.log('inject server host', injectServerHost);
}
// 获取 inject 资源的 url,格式:['url1', 'url2']
function getInjectUrls(resourceType, type = 'url'): Promise<any> {
const filter = (_urls) => {
if (!resourceType) {
return type === 'url' ? _urls.map(item => item.url || item) : _urls;
}
const filteredUrls = _urls.filter((item) => {
if (typeof item === 'string') {
return item.indexOf(`name=@ali/${resourceType}-`) >= 0;
}
if (item.type) {
return typeMap[resourceType].indexOf(item.type) >= 0;
}
return false;
})
return type === 'url' ? filteredUrls.map(item => item.url || item) : filteredUrls;
};
return new Promise((resolve) => {
if (!urlCache) {
const urlParams = queryString.parse(window.location.search);
let urls = urlParams[queryFlag] || [];
urls = Array.isArray(urls) ? urls : [urls];
const { type, injects } = window.injectConfig || {};
if (type === 'auto' || urlParams[injectTypeFlag] === 'auto' || urlParams[debugFlag] !== undefined) {
fetchJsonp(`http://${injectServerHost}:8899/apis/injectInfo`).then(res => res.json()).then((data) => {
urls = envFilter(data.content);
urlCache = urls;
resolve(filter(urlCache));
}).catch((err) => {
urlCache = [];
resolve([]);
console.error(err);
});
} else if (type === 'custom' && injects) {
urls = urls.concat(injects);
urlCache = urls;
resolve(filter(urlCache));
} else {
urlCache = urls;
resolve(filter(urlCache));
}
} else {
resolve(filter(urlCache));
}
});
}
function loadScript(url, callback) {
const src = ((_url) => {
const isInFileProtocol = _url.indexOf('//') === 0 && window.location.protocol === 'file:';
return isInFileProtocol ? `//${_url}` : _url;
})(url);
const scriptElement = document.createElement('script');
scriptElement.crossOrigin = 'anonymous';
scriptElement.src = src;
scriptElement.async = true;
if (callback) {
scriptElement.onload = () => callback();
scriptElement.onerror = () => callback(new Error(`Inject ${url} failed`));
}
document.body.insertBefore(scriptElement, document.body.firstChild);
}
function promiseLoadScript(url) {
return new Promise((rs, rj) => {
loadScript(url, e => (e ? rj(e) : rs({})));
}).then(
() => {
console.info(`%c Injected ${url}`, 'font-weight:bold; font-size: 20px; color: orange;');
},
(e) => {
console.error(e);
},
);
}
function loadComponentFromSources(sources) {
return Promise.all(sources.map(url => promiseLoadScript(url)));
}
// 获取 inject 的资源,格式 [{name, module, pluginType}]
export async function getInjectedResource(type) {
const urls = await getInjectUrls(type);
await loadComponentFromSources(urls);
return window[arrayFlag].filter((item) => {
const _item = item.default || item;
if (!type) {
return true;
}
if (_item.type && typeMap[type].indexOf(_item.type) < 0) {
return false;
}
if (!_item.type && _item.name && _item.name.indexOf(`@ali/${type}-`) < 0) {
return false;
}
return true;
}).map((item) => {
const _item = item.default || item;
_item.module = getModule(_item.module);
return _item;
});
}
function getModule(module) {
if (Array.isArray(module)) {
return module.map(item => getModule(item));
}
return module.default || module;
}
function envFilter(injects) {
if (!injects) {
return [];
}
const urlParams = queryString.parse(window.location.search);
// 从 window 或者 url 中获取当前是设计器还是预览环境;没有配置则读取 window 是否有 VisualEngine
const env = window.injectEnv || urlParams[injectEnvFlag] || (window.VisualEngine || window.LowcodeEditor || window.AliLowCodeEngine ? 'design' : 'preview') || 'design';
let device = urlParams[injectDeviceFlag] || (window.g_config && window.g_config.device) || (window.pageConfig && window.pageConfig.device) || 'web';
if (device === 'both') { // 乐高有双端的能力,开启后 device 是 both
device = /Mobile/.test(window.navigator.userAgent) ? 'mobile' : 'web';
}
let prototypeKey = urlParams[prototypeKeyFlag] || (window.pageConfig
&& window.pageConfig.designerConfigs
&& window.pageConfig.designerConfigs.prototypeKey);
prototypeKey = prototypeKey === 'default' ? '' : prototypeKey;
return injects.filter((item) => {
if (env === 'design') {
// 设计器不需要注入组件的 view 和 vu
if (['utils'].indexOf(item.type) >= 0) {
return false;
}
// 注入指定的 prototype
if (item.type === 'prototype') {
if (item.subType && item.subType !== prototypeKey) {
return false;
}
if (!item.subType && prototypeKey) {
// 看有没有对应的 prototype.js 如果没有则用默认的
const proto = injects.find(item2 => item2.packageName === item.packageName && item2.type === 'prototype' && item2.subType === prototypeKey);
if (proto) {
return false;
}
}
}
}
if (env === 'preview') {
// 预览不需要注入 prototype、vp、setter、pane
if (['prototype', 'plugin', 'setter', 'pane'].indexOf(item.type) >= 0) {
return false;
}
// PC 端应用不需要加载 view.mobile
if (device === 'web' && item.type === 'view' && item.subType === 'mobile') {
return false;
}
// 移动端应用如果有 view.mobile 则不需要加载 view,否则还是加载 view
if (device === 'mobile' && item.type === 'view' && item.subType !== 'mobile') {
// 看当前组件有没有 view.mobile
const viewMobile = injects.find(item2 => item2.packageName === item.packageName && item2.type === 'view' && item2.subType === 'mobile');
if (viewMobile) {
return false;
}
}
}
return true;
});
}
function getComponentFromUrlItems(items) {
const map = {};
items.forEach((item) => {
const { packageName, type, url, library } = item;
if (!map[packageName]) {
map[packageName] = {
packageName,
};
}
map[packageName][type] = url;
map[packageName]['library'] = library;
})
return map;
}
export async function injectAssets(assets) {
if (!needInject) return assets;
try {
const injectUrls = await getInjectUrls('component', 'item');
const components = getComponentFromUrlItems(injectUrls)
Object.keys(components).forEach((name) => {
const item = components[name];
const pascalCaseName = pascal(name);
if (!assets.packages) assets.packages = [];
if (!assets.components) assets.components = [];
assets.packages.push({
"package": name,
"version": '0.1.0',
"library": item.library || pascalCaseName,
"urls": [item.view],
"editUrls": [item.view],
});
assets.components.push({
exportName: `${pascalCaseName}Meta`,
url: item.meta,
});
})
if (Object.keys(components).length > 0) {
Notification.success({
title: '成功注入以下组件',
content: (
<div>
{Object.keys(components).map((name) => (
<p>组件:<b>{name}</b></p>
))}
</div>
)
})
}
} catch (err) {}
return assets;
}
export async function injectComponents(components) {
if (!needInject) return components;
const injectUrls = await getInjectUrls('component', 'item');
await loadComponentFromSources(injectUrls.map(item => item.url || item));
const injectedComponents = getComponentFromUrlItems(injectUrls);
const libraryMap = {};
const componentsMap = {};
Object.keys(injectedComponents).forEach((name) => {
const { library } = injectedComponents[name];
const pascalName = pascal(name);
libraryMap[name] = library || pascalName;
window[`${pascalName}Meta`]?.components?.forEach((item) => {
componentsMap[item.componentName] = item.npm;
})
})
const injectedComponentsForRenderer = await buildComponents(libraryMap, componentsMap, undefined);
if (Object.keys(injectedComponents).length > 0) {
Notification.success({
title: '成功注入以下组件',
content: (
<div>
{Object.keys(injectedComponents).map((name) => (
<p>组件:<b>{name}</b></p>
))}
</div>
)
})
}
return { ...components, ...injectedComponentsForRenderer };
}
export async function filterPackages(packages = []) {
if (!needInject) return packages;
const injectUrls = await getInjectUrls('component', 'item');
const injectedComponents = getComponentFromUrlItems(injectUrls);
return packages.filter((item) => {
return !(item.package in injectedComponents)
});
}