-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.ts
60 lines (52 loc) · 1.61 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
58
59
60
function main() {
if (figma.currentPage.selection.length !== 2) {
figma.closePlugin("⚠️ Please select 2 layers.");
return;
}
figma.showUI(__html__, { width: 360, height: 230 });
let selection1 = figma.currentPage.selection[0];
let selection2 = figma.currentPage.selection[1];
figma.ui.postMessage([selection1.name, selection2.name]);
figma.ui.onmessage = (msg) => {
if (msg.type === "selection-order") {
console.log(msg.order);
let originLayer, targetLayer;
if (msg.order) {
originLayer = selection2;
targetLayer = selection1;
} else {
originLayer = selection1;
targetLayer = selection2;
}
if (targetLayer.type == "GROUP") {
figma.closePlugin("⚠️ Group layer cannot be set as target.");
return;
}
if (targetLayer.type == "SLICE") {
figma.closePlugin("⚠️ Slice layer cannot be set as target.");
return;
}
console.log(originLayer);
originLayer
.exportAsync({ constraint: { type: "SCALE", value: 2 }, format: "PNG" })
.then((bytes) => {
// console.log(bytes);
const image = figma.createImage(bytes);
let newPaint = [];
newPaint = JSON.parse(JSON.stringify(targetLayer.fills));
newPaint.push({
imageHash: image.hash,
scaleMode: "TILE",
scalingFactor: 0.5,
type: "IMAGE",
});
// console.log(newPaint);
targetLayer.fills = newPaint;
})
.then(() => {
figma.closePlugin();
});
}
};
}
main();