-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathindex.ts
More file actions
647 lines (569 loc) · 15.7 KB
/
index.ts
File metadata and controls
647 lines (569 loc) · 15.7 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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/**
* Supported primitive and nested component values stored on design component properties.
*/
export type ComponentPropertyValue = string | number | boolean | DesignComponent
/**
* Figma node types that TemPad Dev plugins can query against.
*/
export type SupportedDesignNodeType = 'GROUP' | 'FRAME' | 'VECTOR' | 'TEXT' | 'INSTANCE'
/**
* Common fields available on every design node we expose.
*/
interface DesignNodeBase {
/** Human-friendly node name shown in Figma. */
name: string
/** Discriminant describing the concrete node type. */
type: SupportedDesignNodeType
/** Whether the node is visible in the current document. */
visible: boolean
}
/**
* Union of every design node variant exposed to plugins.
*/
export type DesignNode = GroupNode | FrameNode | VectorNode | TextNode | DesignComponent
/**
* Figma text node representation including plain characters.
*/
export interface TextNode extends DesignNodeBase {
/** Identifies the node as a text node. */
type: 'TEXT'
/** Raw text characters contained in the node. */
characters: string
}
/**
* Shared structure for nodes that support children.
*/
interface ContainerNodeBase extends DesignNodeBase {
/** Ordered child nodes that reside within this container. */
children: DesignNode[]
}
/**
* Group node containing an ordered list of child nodes.
*/
export interface GroupNode extends ContainerNodeBase {
/** Identifies the node as a group. */
type: 'GROUP'
}
/**
* Frame node containing an ordered list of child nodes.
*/
export interface FrameNode extends ContainerNodeBase {
/** Identifies the node as a frame. */
type: 'FRAME'
}
/**
* CSS variable reference extracted from Figma data.
*/
export interface Variable {
/** Variable name without the `var(--...)` wrapper. */
name: string
/** Default value for the variable if defined. */
value: string
}
/**
* Fill style as either a literal color or a variable.
*/
export interface Fill {
/** Hex color string or variable reference describing the fill color. */
color: string | Variable
}
export interface VectorNode extends DesignNodeBase {
/** Identifies the node as a vector. */
type: 'VECTOR'
/** Fill styles applied to the vector. */
fills: Fill[]
}
/**
* Instance node with optional reference to its master component.
*/
export interface DesignComponent<
T extends object = Record<string, ComponentPropertyValue>
> extends ContainerNodeBase {
/** Identifies the node as a component instance. */
type: 'INSTANCE'
/** Component property map keyed by property name. */
properties: T
/** Reference to the main component definition, if linked. */
mainComponent?: {
/** ID of the main component this instance is linked to. */
id: string
/** Name of the main component this instance is linked to. */
name: string
} | null
}
/**
* Convenience alias for any node that can contain children.
*/
type ContainerNode = GroupNode | FrameNode | DesignComponent
export interface DevComponent<T extends object = Record<string, unknown>> {
/** Component tag or name to render. */
name: string
/** Props that should be passed to the component. */
props: T
/** Ordered child components or string literals. */
children: (DevComponent | string)[]
}
/**
* Syntax highlighting languages that code blocks can declare.
*/
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: {
/** True when pixel values should be converted to rem units. */
useRem: boolean
/** Root font size used when converting pixels to rem units. */
rootFontSize: number
}
}
/**
* Parameters passed to a `transform` hook for code blocks.
*/
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>
}
/**
* Parameters passed to a `transformVariable` hook.
*/
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
}
/**
* Parameters passed to a `transformPx` hook.
*/
interface TransformPxParams extends TransformBaseParams {
/**
* The length value
* @example 16
*/
value: number
}
/**
* Parameters passed to a `transformComponent` hook.
*/
interface TransformComponentParams {
/**
* The design component
*/
component: DesignComponent
}
/**
* Hook options for built-in or custom code blocks.
*/
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
}
/**
* Shape of a code block configuration or sentinel to disable it.
*/
export type CodeBlockOptions =
| (TransformOptions & {
/**
* The title of the code block
* @example 'SCSS'
*/
title?: string
})
| false
/**
* Built-in code block identifiers.
*/
type BuiltInCodeBlock = 'css' | 'js'
/**
* Declarative configuration for code blocks keyed by identifier.
*/
type CodeOptions = Partial<Record<BuiltInCodeBlock, CodeBlockOptions>> &
Record<string, CodeBlockOptions>
export interface Plugin {
/** Human-readable name displayed in the UI. */
name: string
/** Map of code block identifiers to configuration. */
code: CodeOptions
}
export const RAW_TAG_NAME = '__tempad_raw__'
/**
* Helper to create a raw markup node.
* This is fully compatible with the existing DevComponent type.
*/
export function raw(content: string, injectedProps?: Record<string, string>): DevComponent {
return {
name: RAW_TAG_NAME,
props: { content, injectedProps },
children: []
}
}
/**
* Helper to define a TemPad Dev plugin with full type support.
*
* @param plugin Plugin configuration object supplying metadata and code blocks.
* @returns The same plugin configuration, enabling type inference in user code.
*/
export function definePlugin(plugin: Plugin): Plugin {
return plugin
}
function isDevComponent(value: unknown): value is DevComponent {
if (value === null || typeof value !== 'object') {
return false
}
const name = Reflect.get(value, 'name')
return typeof name === 'string'
}
function isChildrenArgument(
value: unknown
): value is (DevComponent | string)[] | DevComponent | string {
if (Array.isArray(value)) {
return true
}
if (typeof value === 'string') {
return true
}
return isDevComponent(value)
}
function normalizeChildren(
children?: (DevComponent | string)[] | DevComponent | string
): (DevComponent | string)[] {
if (children === undefined) {
return []
}
return Array.isArray(children) ? children : [children]
}
/**
* Hyperscript helper to compose `DevComponent` trees from plugin code.
*
* Overloads:
* - `h(name)`
* - `h(name, children)`
* - `h(name, props)`
* - `h(name, props, children)`
*
* @param name Component name or tag to render.
* @param propsOrChildren Optional props object or children collection.
* @param childrenOrSingle Optional children when props are provided.
* @returns A dev component tree node ready for serialization.
*
* @example
* ```ts
* const preview = h('Container', { size: 'lg' }, [
* h('Heading', { level: 2 }, ['Button']),
* h('Button', { variant: 'primary' }, ['Submit'])
* ])
* ```
*
* @example
* ```ts
* const text = h('Text', 'Hello world')
* ```
*/
export function h(name: string): DevComponent<Record<string, unknown>>
export function h(
name: string,
children: (DevComponent | string)[] | DevComponent | string
): DevComponent<Record<string, unknown>>
export function h<T extends object>(
name: string,
props: T,
children?: (DevComponent | string)[] | DevComponent | string
): DevComponent<T>
export function h<T extends object = Record<string, unknown>>(
name: string,
propsOrChildren?: T | (DevComponent | string)[] | DevComponent | string,
childrenOrSingle?: (DevComponent | string)[] | DevComponent | string
): DevComponent<T> {
const props =
propsOrChildren === undefined || isChildrenArgument(propsOrChildren)
? ({} as T)
: (propsOrChildren as T)
const childSource =
propsOrChildren === undefined || isChildrenArgument(propsOrChildren)
? (propsOrChildren ?? childrenOrSingle)
: childrenOrSingle
return {
name,
props,
children: normalizeChildren(childSource)
}
}
// Mapped type for queryable properties
type QueryableProperties = {
[K in keyof Pick<DesignNode, 'type' | 'name' | 'visible'>]: DesignNode[K] extends string
? DesignNode[K] | DesignNode[K][] | RegExp
: DesignNode[K]
}
/**
* Utility that ensures at least one property is defined.
*/
type RequireAtLeastOne<T> = {
[K in keyof T]: Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>>
}[keyof T]
/**
* Predicate or property-based query for matching nodes.
*/
export type NodeQuery = RequireAtLeastOne<QueryableProperties> | ((node: DesignNode) => boolean)
/**
* Returns true when a node property satisfies the provided condition.
*
* @param value Original property value from the node.
* @param condition Value matcher supporting primitives, arrays, and regular expressions.
* @returns True if the value matches or the condition is undefined.
*/
function matchProperty<T>(
value: T,
condition: T extends string ? T | T[] | RegExp : T | undefined
): boolean {
if (condition === undefined) {
return true
}
if (typeof value === 'string') {
if (Array.isArray(condition)) {
return condition.includes(value)
}
if (condition instanceof RegExp) {
return condition.test(value)
}
}
return value === condition
}
/**
* Checks whether a node satisfies the provided query.
*
* @param node Node being evaluated.
* @param query Predicate or property query description.
* @returns True when the node matches.
*/
function matchNode(node: DesignNode, query: NodeQuery): boolean {
if (typeof query === 'function') return query(node)
return (
matchProperty(node.type, query.type) &&
matchProperty(node.name, query.name) &&
matchProperty(node.visible, query.visible ?? true)
)
}
/**
* Find the first direct child that matches the query.
*
* @param node Parent container to search.
* @param query Predicate or property query description.
* @returns The first matching child or null when none found.
*
* @example
* ```ts
* const title = findChild(component, { type: 'TEXT', name: /title/i })
* ```
*/
export function findChild<T extends DesignNode = DesignNode>(
node: ContainerNode,
query: NodeQuery
): T | null {
return (node.children.find((child) => matchNode(child, query)) as T) ?? null
}
/**
* Find all direct children that match the query.
*
* @param node Parent container to search.
* @param query Predicate or property query description.
* @returns An array of matching direct children.
*
* @example
* ```ts
* const icons = findChildren(toolbar, { type: 'VECTOR' })
* ```
*/
export function findChildren<T extends DesignNode = DesignNode>(
node: ContainerNode,
query: NodeQuery
): T[] {
return node.children.filter((child) => matchNode(child, query)) as T[]
}
/**
* Depth-first search for the first node that matches the query.
*
* @param node Root container to search recursively.
* @param query Predicate or property query description.
* @returns The first nested node that matches or null.
*
* @example
* ```ts
* const submitButton = findOne(frame, { name: 'Submit' })
* ```
*/
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
}
/**
* Depth-first search returning every node that matches the query.
*
* @param node Root container to search recursively.
* @param query Predicate or property query description.
* @returns All nodes within the subtree that satisfy the query.
*
* @example
* ```ts
* const texts = findAll(page, { type: 'TEXT', visible: true })
* ```
*/
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[]
}
/**
* Allowed lookup styles that can be chained inside `queryAll`.
*/
export type QueryType = 'child' | 'children' | 'one' | 'all'
/**
* Execute a sequence of queries, returning the final node collection.
*
* @param node Root container to start the pipeline from.
* @param queries Ordered list describing how each query step should behave.
* @returns The collection of nodes produced by the final query step.
*
* @example
* ```ts
* const buttons = queryAll(frame, [
* { query: 'children', name: 'Footer' },
* { query: 'all', type: 'INSTANCE', name: /Button/ }
* ])
* ```
*/
export function queryAll<T extends DesignNode = DesignNode>(
node: ContainerNode,
queries: (NodeQuery & { query: QueryType })[]
): T[] {
if (queries.length === 0) {
return []
}
let current: DesignNode[] = [node]
for (const query of queries) {
const seen = new Set<DesignNode>()
const next: DesignNode[] = []
for (const node of current) {
if (!('children' in node)) {
continue
}
seen.add(node)
if (query.query === 'child' || query.query === 'one') {
const one = query.query === 'child' ? findChild(node, query) : findOne(node, query)
if (one && !seen.has(one)) {
seen.add(one)
next.push(one)
}
} else {
const all = query.query === 'children' ? findChildren(node, query) : findAll(node, query)
for (const item of all) {
if (!seen.has(item)) {
seen.add(item)
next.push(item)
}
}
}
}
current = next
}
return current as T[]
}
/**
* Execute a sequence of queries and return only the first match.
*
* @param node Root container to start the pipeline from.
* @param queries Ordered list describing how each query step should behave.
* @returns The first node produced by the query sequence or null.
*
* @example
* ```ts
* const header = queryOne(page, [
* { query: 'children', name: 'Header' },
* { query: 'child', type: 'FRAME', name: /Top Bar/ }
* ])
* ```
*/
export function queryOne<T extends DesignNode = DesignNode>(
node: ContainerNode,
queries: (NodeQuery & { query: QueryType })[]
): T | null {
return queryAll<T>(node, queries)[0] ?? null
}