-
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
import { css, html } from "lit";
import {
HorizontalPaddingIcon,
VerticalPaddingIcon,
CloseIcon,
CopyIcon,
} from "../Icons";
import { FigmaNode, getStyleRule, NodeStyles } from "./utils";
import type { CSSRule } from "./utils";
const copy = async (text: string) => {
// workaround for Firefox & Safari which do not need clipboard-write permission
let status = { state: "granted" };
try {
status = await navigator.permissions.query({
name: "clipboard-write" as unknown as PermissionName,
});
} catch (e) {
// Firefox throws because it doesn't support clipboard-write allowed
// by default in secure environments. Safari throws because it does not have
// or need navigator.permissions.
}
if (status.state === "granted") {
await navigator.clipboard.writeText(text);
}
};
export type InspectorViewProps = {
node: FigmaNode;
onClose: () => void;
};
export const View = ({ node, onClose }: InspectorViewProps) => {
if (!node) {
return null;
}
const nodeStyles = new NodeStyles(node);
// In order to disable canvas interactions (e.g. pan, click to
// deselect), we need to cancel JavaScript event propagation
// on the root element.
const stopPropagation = (ev: Event) => ev.stopPropagation();
return html`
<div
class="inspector-view"
@click=${stopPropagation}
@wheel=${stopPropagation}
@keydown=${stopPropagation}
@keyup=${stopPropagation}
@pointermove=${stopPropagation}
>
<div class="inspector-section selectable-content">
<div class="title-section">
<h4>${node.name}</h4>
${CloseIcon({ onClick: onClose })}
</div>
<div class="properties-overview">
<div class="title-section">
<p class="inspector-property">
<span>W: </span>${nodeStyles.width}
</p>
<p class="inspector-property" style="margin-left: 16px;">
<span>H: </span>${nodeStyles.height}
</p>
</div>
${nodeStyles.fontPostScriptName
? html`<p class="inspector-property">
<span>Font:</span>
${nodeStyles.fontPostScriptName}
</p>`
: null}
</div>
</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 code-section selectable-content">
${node.characters}
</p>
</div>`
: null}
${StylesSection(nodeStyles)}
</div>
`;
};
export const StylesSection = (nodeStyles: NodeStyles) => {
const onClick = () => copy(nodeStyles.getStyleSheet());
const styles = nodeStyles.getStyles();
return html`<div class="inspector-section">
<div class="title-section style-section">
<h4>CSS</h4>
${CopyIcon({ onClick })}
</div>
<div class="code-section selectable-content">
${styles.map(CSSProperty)}
</div>
</div>`;
};
const CSSProperty = (cssProperty: CSSRule) => {
const { property, value, color } = cssProperty;
let coloredSquare = null;
switch (property) {
case "background":
case "fill":
case "border":
case "box-shadow":
case "color":
coloredSquare = html`<span
class="color-preview"
style="background-color: ${color}"
></span>`;
break;
case "background-image":
coloredSquare = html`<span
class="color-preview"
style="background-image: ${value}"
></span>`;
break;
}
return html`<div class="css-property" @click=${() =>
copy(getStyleRule(cssProperty))}>
<span>${property}:</span>${coloredSquare}<span class="css-value">${value}</span>;</span>
</div>`;
};
export const styles = css`
.inspector-view {
height: 100%;
width: 300px;
position: absolute;
right: 0;
background: white;
border-left: 1px solid #ccc;
overflow-y: auto;
z-index: calc(var(--z-index) + 2);
}
.inspector-view h4 {
font-size: 16px;
margin: 0;
}
.style-section {
margin-bottom: 12px;
}
.title-section {
display: flex;
align-items: center;
}
.code-section {
padding: 8px;
background: #f3f3f3;
font-family: monospace;
}
.title-section svg {
cursor: pointer;
margin-left: auto;
}
.inspector-section {
padding: 16px;
border-bottom: 1px solid #eee;
}
.properties-overview {
font-family: monospace;
color: #518785;
}
.properties-overview p span {
color: #121212;
}
.inspector-property {
display: flex;
align-items: center;
margin-bottom: 0;
}
.inspector-property span {
color: #b3b3b3;
margin-right: 4px;
}
.inspector-property svg {
margin-right: 8px;
}
.css-property {
margin: 8px;
transition: background-color ease-in-out 100ms;
}
.css-property:hover {
cursor: pointer;
background-color: #e8e8e8;
}
.css-value {
color: #518785;
margin-left: 4px;
}
.color-preview {
display: inline-block;
width: 12px;
height: 12px;
border: 1px solid #ccc;
margin-left: 4px;
vertical-align: middle;
}
.selectable-content {
cursor: text;
user-select: text;
}
`;