-
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
-
648
-
649
// This module defines data types used in Figma API.
// The purpose of these type definition is for our rendering and inspector
// panel only. Properties not used in those feature would be omitted.
/**
* https://www.figma.com/developers/api#color-type
*/
export interface Color {
readonly r: number;
readonly g: number;
readonly b: number;
readonly a: number;
}
function isColor(x: unknown): x is Color {
return (
!!x &&
typeof x === "object" &&
"r" in x &&
Number.isFinite(x.r) &&
"g" in x &&
Number.isFinite(x.g) &&
"b" in x &&
Number.isFinite(x.b) &&
"a" in x &&
Number.isFinite(x.a)
);
}
/**
* https://www.figma.com/developers/api#blendmode-type
*/
export type BlendMode =
| "PASS_THROUGH"
| "NORMAL"
| "DARKEN"
| "MULTIPLY"
| "LINEAR_BURN"
| "COLOR_BURN"
| "LIGHTEN"
| "SCREEN"
| "LINEAR_DODGE"
| "COLOR_DODGE"
| "OVERLAY"
| "SOFT_LIGHT"
| "HARD_LIGHT"
| "DIFFERENCE"
| "EXCLUSION"
| "HUE"
| "SATURATION"
| "COLOR"
| "LUMINOSITY";
function isBlendMode(x: unknown): x is BlendMode {
switch (x) {
case "PASS_THROUGH":
case "NORMAL":
case "DARKEN":
case "MULTIPLY":
case "LINEAR_BURN":
case "COLOR_BURN":
case "LIGHTEN":
case "SCREEN":
case "LINEAR_DODGE":
case "COLOR_DODGE":
case "OVERLAY":
case "SOFT_LIGHT":
case "HARD_LIGHT":
case "DIFFERENCE":
case "EXCLUSION":
case "HUE":
case "SATURATION":
case "COLOR":
case "LUMINOSITY":
return true;
default:
return false;
}
}
/**
* https://www.figma.com/developers/api#vector-type
*/
export interface Vector {
readonly x: number;
readonly y: number;
}
function isVector(x: unknown): x is Vector {
return (
!!x &&
typeof x === "object" &&
"x" in x &&
Number.isFinite(x.x) &&
"y" in x &&
Number.isFinite(x.y)
);
}
export interface Effect {
readonly type: "LAYER_BLUR" | "BACKGROUND_BLUR" | string;
readonly visible: boolean;
readonly radius: number;
}
function isEffect(x: unknown): x is Effect {
return (
!!x &&
typeof x === "object" &&
"type" in x &&
typeof x.type === "string" &&
"visible" in x &&
typeof x.visible === "boolean" &&
"radius" in x &&
typeof x.radius === "number"
);
}
export interface ShadowEffect {
readonly type: "INNER_SHADOW" | "DROP_SHADOW";
readonly visible: boolean;
readonly radius: number;
readonly color: Color;
readonly blendMode: BlendMode;
readonly offset: Vector;
readonly spread?: number;
readonly showShadowBehindNode?: boolean;
}
export function isShadowEffect(x: Effect): x is ShadowEffect {
if (x.type !== "INNER_SHADOW" && x.type !== "DROP_SHADOW") {
return false;
}
return (
"color" in x &&
isColor(x.color) &&
"blendMode" in x &&
isBlendMode(x.blendMode) &&
"offset" in x &&
isVector(x.offset) &&
(!("spread" in x) || Number.isFinite(x.spread)) &&
(!("showShadowBehindNode" in x) ||
typeof x.showShadowBehindNode === "boolean")
);
}
/**
* https://www.figma.com/developers/api#rectangle-type
*/
export interface Rectangle {
readonly x: number;
readonly y: number;
readonly width: number;
readonly height: number;
}
function isRectangle(x: unknown): x is Rectangle {
return (
!!x &&
typeof x === "object" &&
"x" in x &&
typeof x.x === "number" &&
"y" in x &&
typeof x.y === "number" &&
"width" in x &&
typeof x.width === "number" &&
"height" in x &&
typeof x.height === "number"
);
}
/**
* https://www.figma.com/developers/api#colorstop-type
*/
export interface ColorStop {
readonly position: number;
readonly color: Color;
}
function isColorStop(x: unknown): x is ColorStop {
return (
!!x &&
typeof x === "object" &&
"position" in x &&
typeof x.position === "number" &&
"color" in x &&
isColor(x.color)
);
}
export interface PaintGlobalProperties {
readonly type: string;
/**
* @default true
*/
readonly visible?: boolean;
/**
* @default 1
*/
readonly opacity?: number;
readonly blendMode: BlendMode;
}
function isPaintGlobalProperties(x: unknown): x is PaintGlobalProperties {
return (
!!x &&
typeof x === "object" &&
"type" in x &&
typeof x.type === "string" &&
(!("visible" in x) || typeof x.visible === "boolean") &&
(!("opacity" in x) || typeof x.opacity === "number") &&
"blendMode" in x &&
isBlendMode(x.blendMode)
);
}
export interface SolidPaint extends PaintGlobalProperties {
readonly type: "SOLID";
readonly color: Color;
}
function isSolidPaint(x: PaintGlobalProperties): x is SolidPaint {
return x.type === "SOLID" && "color" in x && isColor(x.color);
}
export interface GradientPaint extends PaintGlobalProperties {
readonly type:
| "GRADIENT_LINEAR"
| "GRADIENT_RADIAL"
| "GRADIENT_ANGULAR"
| "GRADIENT_DIAMOND";
readonly gradientHandlePositions: readonly [Vector, Vector, Vector];
readonly gradientStops: readonly ColorStop[];
}
const GRADIENT_TYPE_PATTERN = /^GRADIENT_(LINEAR|RADIAL|ANGULAR|DIAMOND)$/;
function isGradientPaint(x: PaintGlobalProperties): x is GradientPaint {
return (
GRADIENT_TYPE_PATTERN.test(x.type) &&
"gradientHandlePositions" in x &&
Array.isArray(x.gradientHandlePositions) &&
x.gradientHandlePositions.every(isVector) &&
"gradientStops" in x &&
Array.isArray(x.gradientStops) &&
x.gradientStops.every(isColorStop)
);
}
export interface ImagePaint extends PaintGlobalProperties {
readonly type: "IMAGE";
readonly scaleMode: "FILL" | "FIT" | "TILE" | "STRETCH";
}
function isImagePaint(x: PaintGlobalProperties): x is ImagePaint {
if (!("scaleMode" in x)) {
return false;
}
switch (x.scaleMode) {
case "FILL":
case "FIT":
case "TILE":
case "STRETCH":
return true;
default:
return false;
}
}
export interface OtherPaint extends PaintGlobalProperties {
readonly type: "VIDEO" | "EMOJI";
}
function isOtherPaint(x: PaintGlobalProperties): x is OtherPaint {
switch (x.type) {
case "VIDEO":
case "EMOJI":
return true;
default:
return false;
}
}
/**
* https://www.figma.com/developers/api#paint-type
*/
export type Paint = SolidPaint | GradientPaint | ImagePaint | OtherPaint;
function isPaint(x: unknown): x is Paint {
if (!isPaintGlobalProperties(x)) {
return false;
}
return (
isSolidPaint(x) || isGradientPaint(x) || isImagePaint(x) || isOtherPaint(x)
);
}
interface HasBackgroundColor {
/**
* Background color of the canvas.
*/
backgroundColor: Color;
}
export function hasBackgroundColor(
node: Node,
): node is Node & HasBackgroundColor {
return "backgroundColor" in node && isColor(node.backgroundColor);
}
interface HasFills {
/**
* @default []
*/
readonly fills: Paint[];
}
export function hasFills(node: Node): node is Node & HasFills {
return (
"fills" in node && Array.isArray(node.fills) && node.fills.every(isPaint)
);
}
interface HasStroke {
/**
* @default []
*/
readonly strokes: readonly Paint[];
readonly strokeWeight: number;
readonly strokeAlign: "INSIDE" | "OUTSIDE" | "CENTER";
/**
* @default []
*/
readonly strokeDashes?: readonly number[];
}
export function hasStroke(node: Node): node is Node & HasStroke {
if (!("strokeAlign" in node)) {
return false;
}
switch (node.strokeAlign) {
case "INSIDE":
case "OUTSIDE":
case "CENTER":
break;
default:
return false;
}
return (
"strokes" in node &&
Array.isArray(node.strokes) &&
node.strokes.every(isPaint) &&
"strokeWeight" in node &&
Number.isFinite(node.strokeWeight) &&
(!("strokeDashes" in node) ||
(Array.isArray(node.strokeDashes) &&
node.strokeDashes.every(Number.isFinite)))
);
}
export interface HasEffects {
effects: readonly (Effect | ShadowEffect)[];
}
export function hasEffects(node: Node): node is Node & HasEffects {
return (
"effects" in node &&
Array.isArray(node.effects) &&
node.effects.every(isEffect)
);
}
interface HasCharacters {
readonly characters: string;
}
export function hasCharacters(node: Node): node is Node & HasCharacters {
return "characters" in node && typeof node.characters === "string";
}
// https://www.figma.com/developers/api#typestyle-type
interface HasTypeStyle {
readonly style: {
readonly fontFamily: string;
readonly fontPostScriptName?: string;
readonly italic: boolean;
readonly fontWeight: number;
readonly fontSize: number;
readonly textCase?:
| "ORIGINAL"
| "UPPER"
| "LOWER"
| "TITLE"
| "SMALL_CAPS"
| "SMALL_CAPS_FORCED";
readonly textDecoration?: "NONE" | "STRIKETHROUGH" | "UNDERLINE";
readonly textAlignHorizontal: "LEFT" | "RIGHT" | "CENTER" | "JUSTIFIED";
readonly letterSpacing: number;
readonly lineHeightPx: number;
readonly lineHeightPercentFontSize?: number;
readonly lineHeightUnit: "PIXELS" | "FONT_SIZE_%" | "INTRINSIC_%";
};
}
export function hasTypeStyle(node: Node): node is Node & HasTypeStyle {
return (
"style" in node &&
typeof node.style === "object" &&
!!node.style &&
"fontFamily" in node.style &&
typeof node.style.fontFamily === "string"
);
}
export interface HasBoundingBox {
readonly absoluteBoundingBox: Rectangle;
/**
* Old data may not have this property.
*/
readonly absoluteRenderBounds?: Rectangle;
}
export function hasBoundingBox(node: Node): node is Node & HasBoundingBox {
return (
"absoluteBoundingBox" in node &&
isRectangle(node.absoluteBoundingBox) &&
(!("absoluteRenderBounds" in node) ||
isRectangle(node.absoluteRenderBounds))
);
}
export interface HasPadding {
paddingTop: number;
paddingRight: number;
paddingBottom: number;
paddingLeft: number;
}
export function hasPadding(node: Node): node is Node & HasPadding {
return (
"paddingTop" in node &&
Number.isFinite(node.paddingTop) &&
"paddingRight" in node &&
Number.isFinite(node.paddingRight) &&
"paddingBottom" in node &&
Number.isFinite(node.paddingBottom) &&
"paddingLeft" in node &&
Number.isFinite(node.paddingLeft)
);
}
export interface HasLegacyPadding {
horizontalPadding: number;
verticalPadding: number;
}
export function hasLegacyPadding(node: Node): node is Node & HasLegacyPadding {
return (
"horizontalPadding" in node &&
Number.isFinite(node.horizontalPadding) &&
"verticalPadding" in node &&
Number.isFinite(node.verticalPadding)
);
}
export interface HasChildren {
readonly children: readonly Node[];
}
export function hasChildren(node: Node): node is Node & HasChildren {
return (
"children" in node &&
Array.isArray(node.children) &&
node.children.every(isNode)
);
}
interface HasRadius {
readonly cornerRadius: number;
}
export function hasRadius(node: Node): node is Node & HasRadius {
return "cornerRadius" in node && typeof node.cornerRadius === "number";
}
interface HasRadii {
readonly rectangleCornerRadii: readonly [number, number, number, number];
}
export function hasRadii(node: Node): node is Node & HasRadii {
return (
"rectangleCornerRadii" in node &&
Array.isArray(node.rectangleCornerRadii) &&
node.rectangleCornerRadii.length === 4
);
}
export type KnownNodeType =
| "DOCUMENT"
| "CANVAS"
| "FRAME"
| "GROUP"
| "SECTION"
| "VECTOR"
| "BOOLEAN_OPERATION"
| "STAR"
| "LINE"
| "ELLIPSE"
| "REGULAR_POLYGON"
| "RECTANGLE"
| "TABLE"
| "TABLE_CELL"
| "TEXT"
| "SLICE"
| "COMPONENT"
| "COMPONENT_SET"
| "INSTANCE"
| "STICKY"
| "SHAPE_WITH_TEXT"
| "CONNECTOR"
| "WASHI_TAPE";
/**
* https://www.figma.com/developers/api#global-properties
*/
export interface Node<Type extends string = KnownNodeType | string> {
readonly id: string;
readonly name: string;
/**
* @default true
*/
readonly visible?: boolean;
readonly type: Type;
}
export function isNode(x: unknown): x is Node {
return (
typeof x === "object" &&
!!x &&
"id" in x &&
typeof x.id === "string" &&
"name" in x &&
typeof x.name === "string" &&
(!("visible" in x) || typeof x.visible === "boolean") &&
"type" in x &&
typeof x.type === "string"
);
}
/**
* Walk over the node and its descendants.
* Iterator is superior to the common callback-style walk function:
* - Ability to abort the traverse with standard language feature (return, break)
* - No implicit call timing convention - TypeScript's poor inference engine completely
* ignores assignments even if the callback function will be called immediately.
* The inference system is built upon unrealistic illusion.
* https://github.com/microsoft/TypeScript/issues/9998
* - (subjective) `for ~ of` over iterator is way more readable and easier to grasp than
* function invocation with unknown callback. When will the callback be invoked?
* What will happen when the callback function returned something?
*
* @param node - The root node to start traversing from. This function returns this parameter as a result at the very first.
* @example
* for (const node of walk(root)) {
* console.log(node.id)
* }
*/
export function* walk(node: Node): Generator<Node, void, undefined> {
yield node;
if (hasChildren(node)) {
for (const child of node.children) {
for (const iter of walk(child)) {
yield iter;
}
}
}
}
export type Canvas = Node & HasChildren & HasBackgroundColor;
function isCanvas(node: Node): node is Canvas {
return (
node.type === "CANVAS" && hasChildren(node) && hasBackgroundColor(node)
);
}
/**
* Returns an iterator of CANVAS nodes.
*/
export function* getCanvases(node: Node): Generator<Canvas, void, undefined> {
if (isCanvas(node)) {
yield node;
// CANVAS cannot be nested, so safe to quit lookup
return;
}
if (!hasChildren(node)) {
return;
}
for (const child of node.children) {
for (const iter of getCanvases(child)) {
yield iter;
}
}
}
export interface GetFileNodesResponse {
readonly name: string;
readonly lastModified: string;
readonly nodes: Record<
string,
{
readonly document: Node;
}
>;
}
export interface GetFileResponse {
readonly name: string;
readonly lastModified: string;
readonly document: Node & HasChildren;
}