forked from opentiny/tiny-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-renderer.ts
More file actions
47 lines (40 loc) · 1.35 KB
/
Copy pathcustom-renderer.ts
File metadata and controls
47 lines (40 loc) · 1.35 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
import { h } from 'vue'
import CanvasEmpty from './CanvasEmpty.vue'
import renderer from '../render'
function defaultRenderer(schema, refreshKey, entry, active, isPage = true) {
// 渲染画布增加根节点,与出码和预览保持一致
const rootChildrenSchema = {
id: 0,
componentName: 'div',
componentType: 'PageSection',
// 手动添加一个唯一的属性,后续在画布选中此节点时方便处理额外的逻辑。由于没有修改schema,不会影响出码
props: { ...schema.props, 'data-id': 'root-container', 'data-page-active': active },
children: schema.children
}
if (!entry) {
return schema.children?.length || !active
? h(renderer, { schema: rootChildrenSchema, parent: schema })
: [h(CanvasEmpty)]
}
const PageStartSchema = {
componentName: 'div',
componentType: 'PageStart',
props: { 'data-id': 'root-container', className: 'design-page' }
}
return isPage
? h(renderer, { schema: PageStartSchema, parent: schema })
: schema.children?.length
? h(renderer, { schema: rootChildrenSchema, parent: schema })
: [h(CanvasEmpty)]
}
export function useCustomRenderer() {
let canvasRenderer = null
const getRenderer = () => canvasRenderer || defaultRenderer
const setRenderer = (fn) => {
canvasRenderer = fn
}
return {
getRenderer,
setRenderer
}
}