-
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
import { css, html } from "lit-element";
import * as Figma from "figma-js";
import * as copy from "copy-to-clipboard";
import {
BorderRadiusIcon,
HorizontalPaddingIcon,
VerticalPaddingIcon,
CloseIcon,
CopyIcon,
} from "./Icons";
type Color = {
a: number;
r: number;
g: number;
b: number;
};
export type FigmaNode = Figma.Node & {
name: string;
characters: string;
background: { color: Color }[];
backgroundColor: Color;
fills: { color: Color }[];
absoluteBoundingBox: {
height: number;
width: number;
};
cornerRadius?: number;
rectangleCornerRadii?: number[];
horizontalPadding: number;
verticalPadding: number;
style?: {
fontFamily: string;
fontSize: number;
fontWeight: number;
lineHeightPx: number;
textAlignHorizontal: string;
textAlignVertical: string;
};
type: "TEXT" | "INSTANCE" | "FRAME" | "VECTOR" | "RECTANGLE";
};
export type InspectorViewProps = {
node: FigmaNode;
onClose: () => void;
};
/**
* Figma returns rbg in values from 0 to 1. We multiply by 255 and truncate them
* https://www.figma.com/plugin-docs/api/RGB/
*/
const rgbToHex = ({ r, g, b }: { r: number; g: number; b: number }) =>
"#" +
(
(1 << 24) +
(Math.trunc(255 * r) << 16) +
(Math.trunc(255 * g) << 8) +
Math.trunc(255 * b)
)
.toString(16)
.slice(1);
class NodeStyles {
background;
borderRadius;
color;
fontFamily;
fontSize;
fontWeight;
height;
horizontalPadding;
lineHeight;
verticalPadding;
width;
hasStyles = false;
hasPadding = false;
constructor(node: FigmaNode) {
this.height = `${Math.trunc(node.absoluteBoundingBox.height)}px`;
this.width = `${Math.trunc(node.absoluteBoundingBox.width)}px`;
if (node.horizontalPadding || node.verticalPadding) {
this.hasPadding = true;
this.horizontalPadding = `${node.horizontalPadding}px`;
this.verticalPadding = `${node.verticalPadding}px`;
}
if (node.style) {
this.fontFamily = node.style.fontFamily;
this.fontWeight = node.style.fontWeight;
this.fontSize = `${node.style.fontSize}px`;
this.lineHeight = `${Math.trunc(node.style.lineHeightPx)}px`;
}
if (node.rectangleCornerRadii) {
this.borderRadius =
node.rectangleCornerRadii.filter(
(radius) => radius === node.cornerRadius
).length < 4
? `${node.rectangleCornerRadii.join("px, ")}px`
: `${node.cornerRadius}px`;
}
if (node.backgroundColor || node.backgroundColor) {
const colors =
node.backgroundColor ||
node.background?.[0].color ||
node.fills?.[0].color;
this.background = rgbToHex(colors);
}
if (node.fills && node.fills.length > 0) {
this.color = rgbToHex(node.fills[0].color);
}
this.hasStyles = !!(
node.style ||
node.rectangleCornerRadii ||
this.background ||
this.color
);
}
getStyles() {
return [
this.height && `height: ${this.height};`,
this.width && `width: ${this.width};`,
this.fontFamily && `font-family: ${this.fontFamily};`,
this.fontSize && `font-size: ${this.fontSize};`,
this.fontWeight && `font-weight: ${this.fontWeight};`,
this.lineHeight && `line-height: ${this.lineHeight};`,
this.borderRadius && `border-radius: ${this.borderRadius};`,
this.background && `background: ${this.background};`,
this.color && `color: ${this.color};`,
].filter(Boolean) as string[];
}
copyStyleSheet() {
copy(this.getStyles().join("\n"));
}
}
export const View = ({ node, onClose }: InspectorViewProps) => {
if (!node) {
return null;
}
const nodeStyles = new NodeStyles(node);
return html`
<div class="inspector-view">
<div class="inspector-section title-section">
<h4>${node.name}</h4>
${CloseIcon({ onClick: onClose })}
</div>
<div class="inspector-section">
<h4>Properties</h4>
<p class="inspector-property">W: ${nodeStyles.width}</p>
<p class="inspector-property">H: ${nodeStyles.height}</p>
${nodeStyles.borderRadius
? html`<p class="inspector-property">
${BorderRadiusIcon()} ${nodeStyles.borderRadius}
</p>`
: null}
</div>
${nodeStyles.hasPadding
? html`<div class="inspector-section">
<h4>Layout</h4>
${nodeStyles.horizontalPadding &&
html`<p class="inspector-property">
${HorizontalPaddingIcon()} ${nodeStyles.horizontalPadding}
</p>`}
${nodeStyles.verticalPadding &&
html`<p class="inspector-property">
${VerticalPaddingIcon()} ${nodeStyles.verticalPadding}
</p>`}
</div>`
: null}
${node.characters
? html`<div class="inspector-section">
<div class="title-section">
<h4>Content</h4>
${CopyIcon({ onClick: () => copy(node.characters) })}
</div>
<p class="node-content node-code">${node.characters}</p>
</div>`
: null}
${nodeStyles.hasStyles ? StylesSection(nodeStyles) : null}
</div>
`;
};
export const StylesSection = (nodeStyles: NodeStyles) => {
const onClick = () => nodeStyles.copyStyleSheet();
const styles = nodeStyles.getStyles();
return html`<div class="inspector-section">
<div class="title-section style-section">
<h4>CSS</h4>
${CopyIcon({ onClick })}
</div>
<div class="node-code">
${styles.map(
(style) =>
html`<p class="css-property" @click=${() => copy(style)}>${style}</p>`
)}
</div>
</div> `;
};
export const styles = css`
.css-property:hover {
cursor: pointer;
background-color: #e8e8e8;
}
.style-section {
padding: 1rem 0;
}
.title-section {
display: flex;
align-items: center;
}
.title-section svg {
cursor: pointer;
margin-left: auto;
}
.node-content {
cursor: text;
user-select: text;
}
.node-code {
padding: 0.5rem;
background: #f3f3f3;
font-family: monospace;
}
.inspector-view {
height: 100vh;
width: 300px;
position: fixed;
right: 0;
background: white;
border-left: 1px solid #ccc;
}
.inspector-section {
padding: 1rem;
border-bottom: 1px solid #ccc;
}
.inspector-section h4 {
margin: 0;
}
.inspector-property {
display: flex;
align-items: center;
margin-bottom: 0;
}
.inspector-property svg {
margin-right: 8px;
}
`;