-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
250 lines (213 loc) · 5.22 KB
/
index.ts
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
export type ComponentPropertyValue = string | number | boolean | DesignComponent
export type SupportedDesignNodeType = 'GROUP' | 'FRAME' | 'TEXT' | 'INSTANCE'
interface DesignNodeBase {
name: string
type: SupportedDesignNodeType
}
export type DesignNode = GroupNode | FrameNode | TextNode | DesignComponent
export interface TextNode extends DesignNodeBase {
type: 'TEXT'
characters: string
}
interface ContainerNodeBase extends DesignNodeBase {
children: DesignNode[]
}
export interface GroupNode extends ContainerNodeBase {
type: 'GROUP'
}
export interface FrameNode extends ContainerNodeBase {
type: 'FRAME'
}
export interface DesignComponent extends ContainerNodeBase {
type: 'INSTANCE'
properties: Record<string, ComponentPropertyValue>
mainComponent?: {
id: string
name: string
} | null
}
type ContainerNode = GroupNode | FrameNode | DesignComponent
export interface DevComponent {
name: string
props: Record<string, unknown>
children: (DevComponent | string)[]
}
export type SupportedLang =
| 'text'
| 'tsx'
| 'jsx'
| 'ts'
| 'js'
| 'vue'
| 'html'
| 'css'
| 'sass'
| 'scss'
| 'less'
| 'stylus'
| 'json'
interface TransformBaseParams {
/**
* The user preferences related to code transformation
* @example { useRem: true, rootFontSize: 16 }
*/
options: {
useRem: boolean
rootFontSize: number
}
}
interface TransformParams extends TransformBaseParams {
/**
* The generated CSS code
* @example 'background-color: red; color: blue;'
*/
code: string
/**
* The parsed CSS properties
* @example { 'background-color': 'red', 'color': 'blue' }
*/
style: Record<string, string>
}
interface TransformVariableParams extends TransformBaseParams {
/**
* The generated CSS variable code
* @example 'var(--color-primary, #6699cc)'
*/
code: string
/**
* The variable name
* @example 'color-primary'
*/
name: string
/**
* The variable value
* @example '#6699cc'
*/
value?: string
}
interface TransformPxParams extends TransformBaseParams {
/**
* The length value
* @example 16
*/
value: number
}
interface TransformComponentParams {
/**
* The design component
*/
component: DesignComponent
}
export type TransformOptions = {
/**
* The language of the code block for syntax highlighting
* @example 'scss'
*/
lang?: SupportedLang
/**
* Transform the generated CSS code
*/
transform?: (params: TransformParams) => string
/**
* Transform the generated CSS variable code
* @example 'var(--kui-color-primary, #6699cc)' -> '$ui-color-primary'
*/
transformVariable?: (params: TransformVariableParams) => string
/**
* Transform the pixel value to the desired unit and scale
* @example 16 -> '1rem'
*/
transformPx?: (params: TransformPxParams) => string
/**
* Transform the design component to a dev component
*/
transformComponent?: (params: TransformComponentParams) => DevComponent | string
}
export type CodeBlockOptions =
| (TransformOptions & {
/**
* The title of the code block
* @example 'SCSS'
*/
title?: string
})
| false
type BuiltInCodeBlock = 'css' | 'js'
type CodeOptions = Partial<Record<BuiltInCodeBlock, CodeBlockOptions>> &
Record<string, CodeBlockOptions>
export interface Plugin {
name: string
code: CodeOptions
}
export function definePlugin(plugin: Plugin): Plugin {
return plugin
}
export function h(
name: string,
props?: Record<string, unknown>,
children?: (DevComponent | string)[]
): DevComponent {
return { name, props: props || {}, children: children || [] }
}
type RequireAtLeastOne<T, Keys extends keyof T> = {
[K in Keys]: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>
}[Keys]
export type NodeQuery =
| RequireAtLeastOne<DesignNode, 'type' | 'name'>
| ((node: DesignNode) => boolean)
function matchNode(node: DesignNode, query: NodeQuery): boolean {
if (typeof query === 'function') {
return query(node)
}
if (query.type && node.type !== query.type) {
return false
}
if (query.name && node.name !== query.name) {
return false
}
return true
}
export function findChild<T extends DesignNode = DesignNode>(
node: ContainerNode,
query: NodeQuery
): T | null {
return (node.children.find((child) => matchNode(child, query)) as T) ?? null
}
export function findChildren<T extends DesignNode = DesignNode>(
node: ContainerNode,
query: NodeQuery
): T[] {
return node.children.filter((child) => matchNode(child, query)) as T[]
}
export function findOne<T extends DesignNode = DesignNode>(
node: ContainerNode,
query: NodeQuery
): T | null {
for (const child of node.children) {
if (matchNode(child, query)) {
return child as T
}
if ('children' in child) {
const result = findOne(child, query)
if (result) {
return result as T
}
}
}
return null
}
export function findAll<T extends DesignNode = DesignNode>(
node: ContainerNode,
query: NodeQuery
): T[] {
const result: DesignNode[] = []
for (const child of node.children) {
if (matchNode(child, query)) {
result.push(child)
}
if ('children' in child) {
result.push(...findAll(child, query))
}
}
return result as T[]
}