forked from opentiny/tiny-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseMaterial.ts
More file actions
540 lines (462 loc) · 15 KB
/
useMaterial.ts
File metadata and controls
540 lines (462 loc) · 15 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/**
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
import { reactive } from 'vue'
import { utils, constants } from '@opentiny/tiny-engine-utils'
import { meta as BuiltinComponentMaterials } from '@opentiny/tiny-engine-builtin-component'
import {
getMergeMeta,
getOptions,
useNotify,
useCanvas,
useBlock,
useMessage,
useResource,
getMetaApi,
META_SERVICE
} from '@opentiny/tiny-engine-meta-register'
import meta from '../../meta'
import { getBlockCompileRes, getBlockByName, updateBlockCompileCache } from './block-compile'
const { camelize, capitalize, deepClone } = utils
const { MATERIAL_TYPE } = constants
// 这里存放所有TinyVue组件、原生HTML、内置组件的缓存,包含了物料插件面板里所有显示的组件,也包含了没显示的一些联动组件
const resource = new Map()
// 这里涉及到区块发布后的更新问题,所以需要单独缓存区块
const blockResource = new Map()
const materialState = reactive({
components: [], // 这里存放的是物料插件面板里所有显示的组件
blocks: [],
componentsDepsMap: { scripts: [], styles: new Set() }, //
packages: [] // 物料依赖的包
})
const componentState = reactive({
componentsMap: {}
})
const getSnippet = (component) => {
let schema = {}
materialState.components.some(({ children }) => {
const child = children.find(({ snippetName }) => snippetName === component)
if (child) {
schema = child.schema
return true
}
return false
})
return schema
}
const generateNode = ({ type, component }) => {
const snippet = getSnippet(component) || {}
const schema = {
componentName: component,
...snippet,
props: {
...snippet.props,
className: getOptions(meta.id).useBaseStyle ? getOptions(meta.id).componentBaseStyle.className : ''
}
}
if (type === 'block') {
schema.componentType = 'Block'
schema.props.className = getOptions(meta.id).useBaseStyle ? getOptions(meta.id).blockBaseStyle.className : ''
}
return schema
}
/**
* 获取物料组件的配置信息
* @returns
*/
const getConfigureMap = () => {
const entries = Object.entries(Object.fromEntries(resource)).map(([key, value]) => {
return [key, value.content?.configure || value.configure]
})
return Object.fromEntries(entries)
}
/**
* 附加基础属性,基础属性可以通过注册表配置
* @param {any[]} schemaProperties
* @returns
*/
const patchBaseProps = (schemaProperties) => {
if (!Array.isArray(schemaProperties)) {
return
}
const { properties = [], insertPosition = 'end' } = getOptions(meta.id).basePropertyOptions || {}
for (const basePropGroup of properties) {
const group = schemaProperties.find((item) => {
// 如果存在了包含'其他'字符串的分组,统一为'其他'分组
if (item.label.zh_CN.includes('其他')) {
item.label.zh_CN = '其他'
}
return (
(basePropGroup.group && basePropGroup.group === item.group) || basePropGroup.label.zh_CN === item.label.zh_CN
)
})
if (group) {
const targetInsertContent = basePropGroup.content.filter(
(item) => !group.content.some((prop) => prop.property === item.property)
)
if (insertPosition === 'start') {
group.content.splice(0, 0, ...deepClone(targetInsertContent))
} else {
group.content.push(...deepClone(targetInsertContent))
}
} else {
schemaProperties.push(deepClone(basePropGroup))
}
}
}
/**
* 将component里的内容注册到resource变量中
* @param {*} data
*/
const registerComponentToResource = (data) => {
patchBaseProps(data.schema?.properties)
if (Array.isArray(data.component)) {
const { component, ...others } = data
component.forEach((item) => {
resource.set(item, { item, ...others, type: MATERIAL_TYPE.Component })
})
} else {
resource.set(data.component, { ...data, type: MATERIAL_TYPE.Component })
}
}
export const fetchBlockDetail = async (blockName) => {
const { getBlockAssetsByVersion } = useBlock()
const currentVersion = componentState.componentsMap?.[blockName]?.version
const block = (await getMetaApi(META_SERVICE.Http).get(`/material-center/api/block?label=${blockName}`))?.[0]
if (!block) {
throw new Error(`区块${blockName}不存在!`)
}
block.assets = getBlockAssetsByVersion(block, currentVersion)
block.assets = history?.assets || block.assets
return block
}
/**
* registerBlock 注册区块
* @deprecated
* @param {String|Object} data 当为字符串时请求详细信息
* @param {*} notFetchResouce 是否添加js css资源到页面
* @returns
*/
const registerBlock = async (data, notFetchResouce) => {
let block = data
if (typeof block === 'string') {
try {
block = await fetchBlockDetail(block)
} catch (error) {
useNotify({
type: 'warning',
title: '区块读取错误',
message: error?.message || error
})
return false
}
}
if (!block) {
return false
}
block.type = MATERIAL_TYPE.Block
block.component = block.component || block.blockName || block.label || block.fileName
// 区块还原备份时, 后台改变current_history, 所以assets优先从current_history里取
const assets = block.assets
const label = block.component
const { scripts = [], styles = [] } = assets || {}
if (!notFetchResouce && !blockResource.get(label)) {
const { addScript, addStyle } = useCanvas().canvasApi.value
const promises = scripts
.filter((item) => item.includes('umd.js'))
.map(addScript)
.concat(styles.map(addStyle))
// 此处删除await,提前放行区块数据,在区块渲染前找到区块数据源映射关系
Promise.allSettled(promises)
blockResource.set(label, block.content)
}
return block
}
const clearMaterials = () => {
materialState.components = []
materialState.blocks = []
resource.clear()
}
const clearBlockResources = () => blockResource.clear()
/**
* 生成组件依赖映射
* @param {array} components 组件物料列表
*/
const generateThirdPartyDeps = (components) => {
const styles = []
const scripts = []
components.forEach((item) => {
const { npm, component } = item
if (!npm || !Object.keys(npm).length) return
const { package: pkg, script, exportName, css } = npm
const currentPkg = scripts.find((item) => item.package === pkg)
if (currentPkg) {
// 保存组件id和导出组件名的对应关系 TinyButton: Button
currentPkg.components[component] = exportName
} else {
scripts.push({
package: pkg,
script,
components: {
[component]: exportName
}
})
}
if (css) {
styles.push(css)
}
})
return { styles, scripts }
}
/**
* 添加组件snippets(分组相同则合并)
* @param {*} componentsSnippets 待添加的组件snippets
* @param {*} snippetsData 当前snippets
* @returns {*} snippetsData 合并后的snippets
*/
const addComponentSnippets = (componentSnippets, snippetsData) => {
if (!componentSnippets) return
const snippetsMap = new Map()
snippetsData.forEach((snippetGroup) => snippetsMap.set(snippetGroup.group, snippetGroup))
componentSnippets.forEach((snippetGroup) => {
if (snippetsMap.has(snippetGroup.group)) {
snippetsMap.get(snippetGroup.group).children.push(...snippetGroup.children)
} else {
const snippetGroupClone = deepClone(snippetGroup)
snippetsData.push(snippetGroupClone)
snippetsMap.set(snippetGroup.group, snippetGroupClone)
}
})
return snippetsData
}
const getCanvasDeps = () => {
const { scripts, styles } = useResource().appSchemaState.materialsDeps
return {
scripts: [...scripts].filter((item) => item.script),
styles: [...styles]
}
}
/**
* 组装画布的依赖,通知画布更新docsrc
*/
const updateCanvasDeps = () => {
useMessage().publish({
topic: 'init_canvas_deps',
data: getCanvasDeps()
})
}
//
const parseMaterialsDependencies = (materialBundle) => {
const { packages, components } = materialBundle
const { scripts: scriptsDeps, styles: stylesDeps } = useResource().appSchemaState.materialsDeps
packages?.forEach((pkg) => {
if (!pkg.script || !pkg.package || scriptsDeps.find((item) => item.package === pkg.package)) {
return
}
scriptsDeps.push(pkg)
if (!pkg.css) {
return
}
if (Array.isArray(pkg.css)) {
pkg.css.forEach((item) => stylesDeps.add(item))
} else {
stylesDeps.add(pkg.css)
}
})
// 解析组件npm字段(兼容旧的物料协议)
const { scripts, styles } = generateThirdPartyDeps(components)
// 合并到canvasDeps中
scripts.forEach((item) => {
const dep = scriptsDeps.find((dep) => dep.package === item.package)
if (dep) {
// 合并组件
dep.components = { ...dep.components, ...(item.components || {}) }
} else {
scriptsDeps.push(item)
}
})
if (!styles) {
return
}
if (Array.isArray(styles)) {
styles.forEach((item) => stylesDeps.add(item))
} else {
stylesDeps.add(styles)
}
}
/**
* 添加物料Bundle文件中的组件类型物料
* @param {*} materialBundle 物料包Bundle.json文件对象
* @returns null
*/
const addComponents = (materialBundle) => {
const { snippets, components } = materialBundle
// 解析物料依赖
parseMaterialsDependencies(materialBundle)
// 注册组件到map中
components.forEach(registerComponentToResource)
// 添加组件snippets
addComponentSnippets(snippets, materialState.components)
}
/**
* 添加物料Bundle文件中的区块类型物料
* @param {*} blocks 物料包Bundle.json文件中blocks对象
*/
const addBlocks = (blocks) => {
if (!Array.isArray(blocks) || !blocks.length) {
return
}
// 提前构建区块
blocks.map((item) => getBlockCompileRes(item))
// 默认区块都会展示在默认分组中
if (!materialState.blocks?.[0]?.children) {
materialState.blocks.push({
groupId: useBlock().DEFAULT_GROUP_ID,
groupName: useBlock().DEFAULT_GROUP_NAME,
children: []
})
}
// 区块存到物料列表
materialState.blocks[0].children.unshift(...blocks)
}
/**
* 获取到符合物料协议的bundle.json之后,处理组件与区块物料
* @param {*} materials
*/
const addMaterials = (materials = {}) => {
addComponents(materials)
addBlocks(materials.blocks)
}
const getMaterial = (name) => {
if (name) {
// 先读取组件缓存,再读取区块缓存
return (
resource.get(name) ||
resource.get(capitalize(camelize(name))) ||
blockResource.get(name) ||
blockResource.get(capitalize(camelize(name))) ||
{}
)
} else {
return {}
}
}
const setMaterial = (name, data) => {
resource.set(name, data)
}
/**
* 获取物料,并返回符合物料协议的bundle.json内容
* @returns getMaterialsRes: () => Promise<Materials>
*/
export const getMaterialsRes = async () => {
const bundleUrls = getMergeMeta('engine.config')?.material || []
const materials = await Promise.allSettled(
bundleUrls.map((url) => (typeof url === 'string' ? getMetaApi(META_SERVICE.Http).get(url) : url))
)
return materials
}
const fetchMaterial = async () => {
const materials = await getMaterialsRes()
materials.forEach((response) => {
if (response.status === 'fulfilled' && response.value.materials) {
addMaterials(response.value.materials)
}
})
updateCanvasDeps()
}
/**
* 获取区块保存的依赖信息,合并到appSchemaState.thirdPartyDeps
* @param {object} dependencies 区块保存的依赖信息
*/
const getBlockDeps = (dependencies = {}) => {
const { scripts = [], styles = [] } = dependencies
if (scripts.length) {
scripts.forEach((npm) => {
const { package: pkg, script, css, components } = npm
const npmInfo = materialState.componentsDepsMap.scripts.find((item) => item.package === pkg)
if (!npmInfo || !npmInfo.script) {
materialState.componentsDepsMap.scripts.push({ package: pkg, script, css, components })
} else {
const components = npmInfo.components || {}
npmInfo.components = { ...components, ...npm.components }
}
})
}
if (Array.isArray(styles)) {
styles.forEach((item) => materialState.componentsDepsMap.styles.add(item))
}
}
const initBuiltinMaterial = () => {
const { Builtin } = useCanvas().canvasApi.value
// 添加画布物料
addMaterials(Builtin.data.materials)
// 添加builtin-component NPM包物料
addMaterials(BuiltinComponentMaterials)
}
const initMaterial = ({ isInit = true, appData = {} } = {}) => {
initBuiltinMaterial()
if (isInit) {
componentState.componentsMap = {}
appData.componentsMap?.forEach((component) => {
if (component.dependencies) {
getBlockDeps(component.dependencies)
}
componentState.componentsMap[component.componentName] = component
})
}
}
const refreshMaterial = async () => {
clearMaterials()
initMaterial()
await fetchMaterial()
}
/**
* 根据组名获取指定分组组件
* @param {Array} components 所有组件
* @param {String} groupName 组件分组名
* @returns
*/
const getComponentsByGroup = (components, groupName) => {
if (!Array.isArray(components)) return []
return components.filter((item) => item.group === groupName)
}
/**
* 增加区块缓存
* @param {String} id 区块 id,也就是 label 字段
* @param {Object} resource 区块信息,区块详情中的 content 字段
*/
export const addBlockResources = (id, resource) => {
blockResource.set(id, resource)
}
export default function () {
return {
materialState, // 存放着组件、物料侧区块、第三方依赖信息
initMaterial, // 物料模块初始化
fetchMaterial, // 请求物料并进行处理
getMaterialsRes, // 获取物料,并返回符合物料协议的bundle.json内容,getMaterialsRes: () => Promise<Materials>
generateNode, // 根据 包含{ type, componentName }的组件信息生成组件schema节点,结构:
clearMaterials, // 清空物料
clearBlockResources, // 清空区块缓存,以便更新最新版区块
getMaterial, // 获取单个物料,(property) getMaterial: (name: string) => Material
setMaterial, // 设置单个物料 (property) setMaterial: (name: string, data: Material) => void
addMaterials, // 添加多个物料
registerBlock, // 注册新的区块
getCanvasDeps, // 组装画布依赖,包含物料和工具类的依赖。
updateCanvasDeps, // 通知画布更新依赖
getConfigureMap, // 获取物料组件的配置信息
getBlockByName,
getBlockCompileRes,
addBlockResources,
updateBlockCompileCache,
getComponentsByGroup,
refreshMaterial
}
}