-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
107 lines (107 loc) · 5.34 KB
/
code.js
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
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
function transferFills(sourceNode, targetNode) {
if ("fills" in sourceNode && "fills" in targetNode) {
targetNode.fills = sourceNode.fills;
}
if ("children" in sourceNode && "children" in targetNode) {
for (let i = 0; i < sourceNode.children.length; i++) {
if (i < targetNode.children.length) {
transferFills(sourceNode.children[i], targetNode.children[i]);
}
}
}
}
function main() {
var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function* () {
let instance = null;
let all = null;
for (const node of figma.currentPage.selection) {
if (node.type === "INSTANCE" || node.type === "COMPONENT") {
instance = node;
}
if (node.type === "FRAME" || node.type === "TEXT") {
all = (_a = node.parent) === null || _a === void 0 ? void 0 : _a.children;
}
}
for (const node of figma.currentPage.selection) {
if (instance === null) {
figma.notify("Please select one instance or one component and multiple frames");
figma.closePlugin();
}
if (node.type === "TEXT") {
const frame = figma.createFrame();
frame.resize(node.width, node.height);
frame.x = node.x;
frame.y = node.y;
frame.appendChild(node.clone()); // Clone the text node and add it to the frame
const nodeIndex = all === null || all === void 0 ? void 0 : all.indexOf(node);
(_b = node.parent) === null || _b === void 0 ? void 0 : _b.insertChild(nodeIndex !== null && nodeIndex !== void 0 ? nodeIndex : 0, frame);
// node.remove(); // Remove the original text node
// Clone the instance and place it at the same position as the frame
let thisInstance = instance === null || instance === void 0 ? void 0 : instance.clone();
if ((thisInstance === null || thisInstance === void 0 ? void 0 : thisInstance.type) === "COMPONENT") {
thisInstance = thisInstance.createInstance();
}
const text = frame.findAllWithCriteria({
types: ["TEXT"],
});
const instanceText = thisInstance.findAllWithCriteria({
types: ["TEXT"],
});
for (let i = 0; i < instanceText.length; i++) {
if (typeof instanceText[i].fontName !== "symbol" &&
instanceText[i].fontName !== figma.mixed &&
typeof instanceText[i].fontName !== "symbol") {
yield figma.loadFontAsync(instanceText[i].fontName);
}
instanceText[i].characters = text[i].characters;
}
thisInstance.x = frame.x;
thisInstance.y = frame.y;
(_c = frame.parent) === null || _c === void 0 ? void 0 : _c.insertChild(all === null || all === void 0 ? void 0 : all.indexOf(node), thisInstance); // Use nodeIndex instead of frameIndex
node.remove(); // Remove the original text node
frame.remove(); // Remove the original frame
}
if (node.type === "FRAME") {
let thisInstance = instance.clone();
if (thisInstance.type === "COMPONENT") {
thisInstance = thisInstance.createInstance();
}
transferFills(node, thisInstance);
const text = node.findAllWithCriteria({
types: ["TEXT"],
});
const instanceText = thisInstance.findAllWithCriteria({
types: ["TEXT"],
});
if (text.length != instanceText.length) {
figma.notify("Instance/Component and frame don't have the same layout!");
figma.closePlugin();
}
for (let i = 0; i < instanceText.length; i++) {
if (typeof instanceText[i].fontName !== "symbol") {
yield figma.loadFontAsync(instanceText[i].fontName);
}
instanceText[i].characters = text[i].characters;
}
thisInstance.y = node.y;
thisInstance.x = node.x;
(_d = node.parent) === null || _d === void 0 ? void 0 : _d.insertChild(all === null || all === void 0 ? void 0 : all.indexOf(node), thisInstance);
node.remove();
}
}
});
}
main().then(() => {
figma.closePlugin();
});