forked from evanw/figma-fill-rule-editor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode.ts
46 lines (39 loc) · 1.01 KB
/
code.ts
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
figma.showUI(__html__, {
width: 400,
height: 400,
});
let updateTimeout = 0;
let updateCounter = 0;
let prevMsg = '';
function update(): void {
clearTimeout(updateTimeout);
const selection = figma.currentPage.selection;
const node = selection.length === 1 ? selection[0] : null;
const msg = {
node: node === null || node.type !== 'VECTOR' ? null : {
id: node.id,
vectorNetwork: node.vectorNetwork,
},
};
const msgStr = JSON.stringify(msg);
if (msgStr !== prevMsg) {
prevMsg = msgStr;
figma.ui.postMessage(msg);
updateCounter = 0;
}
const timeout = updateCounter++ < 20 ? 16 : 250;
updateTimeout = setTimeout(update, timeout);
}
figma.on('selectionchange', update);
update();
figma.ui.onmessage = async msg => {
if (msg.node) {
const node = await figma.getNodeByIdAsync(msg.node.id);
if (node !== null && node.type === 'VECTOR') {
node.vectorNetwork = msg.node.vectorNetwork;
}
}
if (msg.type === 'open-url') {
figma.openExternal(msg.url);
}
};