-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCodegen.ts
More file actions
153 lines (143 loc) · 4.38 KB
/
Codegen.ts
File metadata and controls
153 lines (143 loc) · 4.38 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
import { getComponentName } from '../utils'
import { getProps } from './props'
import { getSelectorProps } from './props/selector'
import { renderComponent, renderNode } from './render'
import { renderText } from './render/text'
import { checkAssetNode } from './utils/check-asset-node'
import { checkSameColor } from './utils/check-same-color'
import {
getDevupComponentByNode,
getDevupComponentByProps,
} from './utils/get-devup-component'
import { buildCssUrl } from './utils/wrap-url'
export class Codegen {
components: Map<
SceneNode,
{ code: string; variants: Record<string, string> }
> = new Map()
code: string = ''
constructor(private node: SceneNode) {
if (node.type === 'COMPONENT' && node.parent?.type === 'COMPONENT_SET') {
this.node = node.parent
} else {
this.node = node
}
}
getCode() {
return this.code
}
getComponentsCodes() {
return Array.from(this.components.entries()).map(
([node, { code, variants }]) =>
[
getComponentName(node),
renderComponent(getComponentName(node), code, variants),
] as const,
)
}
async addComponent(node: ComponentNode) {
const childrenCodes =
'children' in node
? node.children.map(async (child) => {
if (child.type === 'INSTANCE') {
const mainComponent = await child.getMainComponentAsync()
if (mainComponent) await this.addComponent(mainComponent)
}
return await this.run(child, 0)
})
: []
const props = await getProps(node)
const selectorProps = await getSelectorProps(node)
const variants = {}
if (selectorProps) {
Object.assign(props, selectorProps.props)
Object.assign(variants, selectorProps.variants)
}
this.components.set(node, {
code: renderNode(
getDevupComponentByProps(props),
props,
2,
await Promise.all(childrenCodes),
),
variants,
})
}
async run(node: SceneNode = this.node, dep: number = 0): Promise<string> {
const assetNode = checkAssetNode(node)
if (assetNode) {
const props = await getProps(node)
props.src = `/icons/${node.name}.${assetNode}`
if (assetNode === 'svg') {
const maskColor = await checkSameColor(node)
if (maskColor) {
// support mask image icon
props.maskImage = buildCssUrl(props.src)
props.maskRepeat = 'no-repeat'
props.maskSize = 'contain'
props.bg = maskColor
delete props.src
}
}
const ret = renderNode('src' in props ? 'Image' : 'Box', props, dep, [])
if (node === this.node) this.code = ret
return ret
}
const props = await getProps(node)
if (
(node.type === 'COMPONENT_SET' || node.type === 'COMPONENT') &&
((this.node.type === 'COMPONENT_SET' &&
node === this.node.defaultVariant) ||
this.node.type === 'COMPONENT')
) {
await this.addComponent(
node.type === 'COMPONENT_SET' ? node.defaultVariant : node,
)
}
if (node.type === 'INSTANCE') {
const mainComponent = await node.getMainComponentAsync()
if (mainComponent) await this.addComponent(mainComponent)
let ret = renderNode(getComponentName(mainComponent || node), {}, dep, [])
if (props.pos) {
ret = renderNode(
'Box',
{
pos: props.pos,
top: props.top,
left: props.left,
right: props.right,
bottom: props.bottom,
},
dep,
[ret],
)
}
if (node === this.node) this.code = ret
return ret
}
const childrenCodes = await Promise.all(
'children' in node
? node.children.map(async (child) => {
if (child.type === 'INSTANCE') {
const mainComponent = await child.getMainComponentAsync()
if (mainComponent) await this.addComponent(mainComponent)
}
return await this.run(child)
})
: [],
)
if (node.type === 'TEXT') {
const { children, props: _props } = await renderText(node)
childrenCodes.push(...children)
Object.assign(props, _props)
}
const ret = renderNode(
getDevupComponentByNode(node, props),
props,
dep,
childrenCodes,
)
if (node === this.node) this.code = ret
return ret
}
}