-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.ts
executable file
·57 lines (47 loc) · 1.39 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
47
48
49
50
51
52
53
54
55
56
57
figma.showUI(__html__)
figma.ui.resize(500, 600)
// main().then(res => figma.closePlugin(res))
main()
async function main() {
const blobs = [];
const { selection } = figma.currentPage;
for (let node of selection) {
const scale = node.exportSettings[0] ? node.exportSettings[0].constraint.value : 1; // Check export settings or default to 1
const blobPromise = exportPostNodeAsJPG(node, scale);
blobs.push([node.name, scale, blobPromise]);
}
const resolvedBlobs = await Promise.all(
blobs.map(async ([nodeName, scale, blobPromise]) => [nodeName, scale, await blobPromise])
);
figma.ui.postMessage(resolvedBlobs);
// return new Promise(res => {
// figma.ui.onmessage = () => res()
// })
}
async function exportFrameAsJPG(frameNode, scale) {
const options = {
format: 'JPG',
constraint: {
type: 'SCALE',
value: scale // Get the value from Figma's export setting
}
};
const bytes = await frameNode.exportAsync(options);
return new Uint8Array(bytes);
}
async function exportPostNodeAsJPG(node, scale) {
const postFrame = node;
if (!postFrame) {
return;
}
try {
const imageBytes = await exportFrameAsJPG(postFrame, scale);
return imageBytes;
} catch (error) {
console.error('Error exporting frame as JPG:', error);
throw error; // Rethrow the error to propagate it
}
}
figma.ui.onmessage = async() => {
main()
}