-
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
import type * as Figma from "figma-js";
import {
LitElement,
TemplateResult,
css,
html,
property,
svg,
} from "lit-element";
import { styleMap, StyleInfo } from "lit-html/directives/style-map";
import * as DistanceGuide from "./DistanceGuide";
import * as Node from "./Node";
type SizedNode = Extract<Figma.Node, { absoluteBoundingBox: any }>;
/**
* A Figma spec viewer. Displays a rendered image alongside sizing guides.
* @element figspec-viewer
*/
export class FigspecViewer extends LitElement {
/**
* A response of "GET file nodes" API.
* https://www.figma.com/developers/api#get-file-nodes-endpoint
*/
@property({
type: Object,
})
nodes: Figma.FileNodesResponse | null = null;
/**
* An image rendered by "GET image" API.
* https://www.figma.com/developers/api#get-images-endpoint
*/
@property({
type: String,
attribute: "rendered-image",
})
renderedImage: string | null = null;
/**
* Current selected node.
*/
@property({
attribute: false,
})
selectedNode: SizedNode | null = null;
/**
* Current zoom level, where 1.0 = 100%.
*/
@property({
attribute: false,
})
scale: number = 1;
/**
* Current pan offset in px for X axis.
* This is a "before the scale" value.
*/
@property({
attribute: false,
})
panX: number = 0;
/**
* Current pan offset in px for Y axis.
* This is a "before the scale" value.
*/
@property({
attribute: false,
})
panY: number = 0;
/**
* How fast zooming when do ctrl+scroll / pinch gestures.
* Available values: 1 ~ 1000
*/
@property({
type: Number,
attribute: "zoom-speed",
})
zoomSpeed: number = 500;
/**
* How fast panning when scroll vertically or horizontally.
* This does not affect to dragging with middle button pressed.
* Available values: 1 ~ 1000.
*/
@property({
type: Number,
attribute: "pan-speed",
})
panSpeed: number = 500;
/**
* The minimum margin for the preview canvas. Will be used when the preview
* setting a default zooming scale for the canvas.
*/
@property({
type: Number,
attribute: "zoom-margin",
})
zoomMargin: number = 50;
#isDragModeOn: boolean = false;
constructor() {
super();
this.addEventListener("click", () => {
this.selectedNode = null;
});
this.addEventListener(
"wheel",
(ev) => {
if (this.parameterError) return;
ev.preventDefault();
if (ev.ctrlKey) {
// Performs zoom when ctrl key is pressed.
let { deltaY } = ev;
if (ev.deltaMode === 1) {
// Firefox quirk
deltaY *= 15;
}
const prevScale = this.scale;
this.scale *= 1 - deltaY / ((1000 - this.zoomSpeed) * 0.5);
// Performs pan to archive "zoom at the point" behavior (I don't know how to call it).
const offsetX = ev.offsetX - this.offsetWidth / 2;
const offsetY = ev.offsetY - this.offsetHeight / 2;
this.panX += offsetX / this.scale - offsetX / prevScale;
this.panY += offsetY / this.scale - offsetY / prevScale;
} else {
// Performs pan otherwise (to be close to native behavior)
// Adjusting panSpeed in order to make panSpeed=500 to match to the Figma's one.
const speed = this.panSpeed * 0.002;
this.panX -= (ev.deltaX * speed) / this.scale;
this.panY -= (ev.deltaY * speed) / this.scale;
}
},
// This component prevents every native wheel behavior on it.
{ passive: false }
);
this.addEventListener("pointermove", (ev) => {
// Performs pan only when middle buttons is pressed.
//
// 4 ... Auxiliary button (usually the mouse wheel button or middle button)
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
if (!(ev.buttons & 4)) return;
ev.preventDefault();
// Moving amount of middle button+pointer move panning should matches to the actual
// pointer travel distance. Since translate goes after scaling, we need to scale
// delta too.
this.movePanel(ev.movementX, ev.movementY);
});
// Listen to keyboard events to enable dragging when Space is pressed, just like in Figma
this.#listenToKeyboardEvents();
/** @private */
this.onmousedown = () => {
if (this.#isDragModeOn) {
document.body.style.cursor = "grabbing";
this.onmousemove = ({ movementX, movementY }: MouseEvent) => {
this.movePanel(movementX, movementY);
};
// cleanup unnecessary listeners when user stops dragging
this.onmouseup = () => {
document.body.style.cursor = "grab";
this.onmousemove = null;
this.onmouseup = null;
};
}
};
}
movePanel(shiftX: number, shiftY: number) {
this.panX += shiftX / this.scale / window.devicePixelRatio;
this.panY += shiftY / this.scale / window.devicePixelRatio;
}
static get styles() {
return [
css`
:host {
--bg: var(--figspec-viewer-bg, #666);
--z-index: var(--figspec-viewer-z-index, 0);
--error-bg: var(--figspec-viewer-error-bg, #870909);
--error-fg: var(--figspec-viewer-error-fg, white);
--guide-thickness: var(--figspec-viewer-guide-thickness, 1.5px);
--guide-color: var(--figspec-viewer-guide-color, tomato);
--guide-selected-color: var(
--figspec-viewer-guide-selected-color,
dodgerblue
);
--guide-tooltip-fg: var(--figspec-viewer-guide-tooltip-fg, white);
--guide-selected-tooltip-fg: var(
--figspec-viewer-guide-selected-tooltip-fg,
white
);
--guide-tooltip-bg: var(
--figspec-viewer-guide-tooltip-bg,
var(--guide-color)
);
--guide-selected-tooltip-bg: var(
--figspec-viewer-guide-selected-tooltip-bg,
var(--guide-selected-color)
);
--guide-tooltip-font-size: var(
--figspec-viewer-guide-tooltip-font-size,
12px
);
position: relative;
display: block;
background-color: var(--bg);
user-select: none;
overflow: hidden;
z-index: var(--z-index);
}
.canvas {
position: absolute;
top: 50%;
left: 50%;
}
.rendered-image {
position: absolute;
top: 0;
left: 0;
}
.error {
position: absolute;
top: 50%;
left: 50%;
max-width: 80%;
padding: 0.75em 1em;
background-color: var(--error-bg);
border-radius: 4px;
color: var(--error-fg);
transform: translate(-50%, -50%);
}
.error-title {
display: block;
font-size: 0.8em;
font-weight: bold;
text-transform: capitalize;
}
.error-description {
display: block;
margin-block-start: 0.5em;
}
.guides {
position: absolute;
overflow: visible;
stroke: var(--guide-color);
fill: var(--guide-color);
pointer-events: none;
z-index: calc(var(--z-index) + 2);
}
`,
Node.styles,
DistanceGuide.styles,
];
}
/**
* Readonly. Document node (= root drawable node).
* @readonly
*/
get documentNode(): SizedNode | null {
if (!this.nodes) {
return null;
}
const documentNode = Object.values(this.nodes.nodes)[0];
if (!documentNode || !("absoluteBoundingBox" in documentNode.document)) {
return null;
}
return documentNode.document;
}
/**
* @private
*/
get parameterError(): TemplateResult | null {
if (!this.nodes || !this.renderedImage) {
return html`Both <code>nodes</code> and <code>rendered-image</code> are
required.`;
}
if (!this.documentNode) {
return html`Document node is empty or does not have size.`;
}
return null;
}
render() {
if (this.parameterError) {
return html`
<p class="error">
<span class="error-title">Parameter error</span>
<span class="error-description">${this.parameterError}</span>
</p>
`;
}
const documentNode = this.documentNode as SizedNode;
const nodes = flattenNode(documentNode);
const margin = getCanvasMargin(documentNode, nodes);
const canvasSize = documentNode.absoluteBoundingBox;
const { scale, panX, panY } = this;
const reverseScale = 1 / scale;
const guideThickness = `calc(var(--guide-thickness) * ${reverseScale})`;
const computedGuideThickness = parseFloat(
getComputedStyle(this).getPropertyValue("--guide-thickness")
);
const computedGuideTooltipFontSize = parseFloat(
getComputedStyle(this).getPropertyValue("--guide-tooltip-font-size")
);
return html`
<div
class="canvas"
style="
width: ${canvasSize.width}px;
height: ${canvasSize.height}px;
transform: translate(-50%, -50%) scale(${scale}) translate(${panX}px, ${panY}px);
"
>
<img
class="rendered-image"
src="${this.renderedImage}"
style="
margin-top: ${-margin.top}px;
margin-left: ${-margin.left}px;
width: ${canvasSize.width + margin.left + margin.right}px;
height: ${canvasSize.height + margin.top + margin.bottom}px;
"
/>
${this.selectedNode &&
Node.Tooltip({
nodeSize: this.selectedNode.absoluteBoundingBox,
offsetX: -canvasSize.x,
offsetY: -canvasSize.y,
reverseScale,
})}
${svg`
<svg class="guides"
viewBox="0 0 5 5"
width="5"
height="5"
style=${styleMap({
left: `${-canvasSize.x}px`,
top: `${-canvasSize.y}px`,
strokeWidth: guideThickness,
})}
>
${
this.selectedNode &&
svg`
<g data-selected>
${Node.Outline({
node: this.selectedNode,
computedThickness: computedGuideThickness * reverseScale,
})}
</g>
`
}
${nodes.map((node) => {
if (node.id === this.selectedNode?.id) {
return null;
}
return svg`
<g>
${Node.Outline({
node,
computedThickness: computedGuideThickness * reverseScale,
onClick: this.#handleNodeClick(node),
})}
${
this.selectedNode &&
DistanceGuide.Guides({
node,
distanceTo: this.selectedNode,
reverseScale,
fontSize: computedGuideTooltipFontSize,
})
}
</g>
`;
})}
</svg>
`}
</div>
`;
}
connectedCallback() {
super.connectedCallback();
this.#resetZoom();
}
disconnectedCallback() {
document.removeEventListener("keyup", this.#keyUp);
document.removeEventListener("keydown", this.#keyDown);
super.disconnectedCallback();
}
#handleNodeClick = (node: SizedNode) => (ev: MouseEvent) => {
ev.preventDefault();
ev.stopPropagation();
this.selectedNode = node;
};
// enable drag mode when holding the spacebar
#keyDown = (event: KeyboardEvent) => {
if (event.code === "Space" && !this.#isDragModeOn) {
this.#isDragModeOn = true;
document.body.style.cursor = "grab";
}
};
// disable drag mode when space lets the spacebar go
#keyUp = (event: KeyboardEvent) => {
if (event.code === "Space" && this.#isDragModeOn) {
this.#isDragModeOn = false;
document.body.style.cursor = "auto";
}
};
#listenToKeyboardEvents = () => {
document.addEventListener("keyup", this.#keyUp);
document.addEventListener("keydown", this.#keyDown);
};
#resetZoom = () => {
if (this.documentNode) {
// Set initial zoom level based on element size
const { width, height } = this.documentNode.absoluteBoundingBox;
const {
width: elementWidth,
height: elementHeight,
} = this.getBoundingClientRect();
const wDiff = elementWidth / (width + this.zoomMargin * 2);
const hDiff = elementHeight / (height + this.zoomMargin * 2);
this.scale = Math.min(wDiff, hDiff, 1);
}
};
}
interface Margin {
top: number;
right: number;
bottom: number;
left: number;
}
function getCanvasMargin(
documentNode: SizedNode,
nodes: readonly SizedNode[]
): Margin {
const points = nodes.map((node) => {
if (!("effects" in node)) {
return {
top: node.absoluteBoundingBox.y,
right: node.absoluteBoundingBox.x + node.absoluteBoundingBox.width,
bottom: node.absoluteBoundingBox.y + node.absoluteBoundingBox.height,
left: node.absoluteBoundingBox.x,
};
}
const blurRadiuses = node.effects
.filter((effect) => effect.visible && effect.type === "LAYER_BLUR")
.map((effect) => effect.radius);
const shadowMargins = node.effects
.filter(
(
effect
): effect is Figma.Effect & {
offset: NonNullable<Figma.Effect["offset"]>;
} => effect.visible && effect.type === "DROP_SHADOW" && !!effect.offset
)
.map<Margin>((effect) => {
return {
left: effect.radius - effect.offset.x,
top: effect.radius - effect.offset.y,
right: effect.radius + effect.offset.x,
bottom: effect.radius + effect.offset.y,
};
});
const margin: Margin = {
top: Math.max(
0,
...blurRadiuses,
...shadowMargins.map((margin) => margin.top)
),
right: Math.max(
0,
...blurRadiuses,
...shadowMargins.map((margin) => margin.right)
),
bottom: Math.max(
0,
...blurRadiuses,
...shadowMargins.map((margin) => margin.bottom)
),
left: Math.max(
0,
...blurRadiuses,
...shadowMargins.map((margin) => margin.left)
),
};
return {
top: node.absoluteBoundingBox.y - margin.top,
right:
node.absoluteBoundingBox.x +
node.absoluteBoundingBox.width +
margin.right,
bottom:
node.absoluteBoundingBox.y +
node.absoluteBoundingBox.height +
margin.bottom,
left: node.absoluteBoundingBox.x - margin.left,
};
});
const bounds = {
top: Math.min(...points.map((p) => p.top)),
right: Math.max(...points.map((p) => p.right)),
bottom: Math.max(...points.map((p) => p.bottom)),
left: Math.min(...points.map((p) => p.left)),
};
return {
top: documentNode.absoluteBoundingBox.y - bounds.top,
right:
bounds.right -
documentNode.absoluteBoundingBox.x -
documentNode.absoluteBoundingBox.width,
bottom:
bounds.bottom -
documentNode.absoluteBoundingBox.y -
documentNode.absoluteBoundingBox.height,
left: documentNode.absoluteBoundingBox.x - bounds.left,
};
}
function flattenNode(
node: Figma.Node,
depth: number = 0
): readonly (SizedNode & {
depth: number;
})[] {
if (!("absoluteBoundingBox" in node)) {
return node.children.map((child) => flattenNode(child, depth + 1)).flat();
}
if (!("children" in node) || node.children.length === 0) {
return [{ ...node, depth }];
}
return [
{ ...node, depth },
...node.children.map((child) => flattenNode(child, depth + 1)).flat(),
];
}