-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcode.js
executable file
·289 lines (289 loc) · 10.5 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
function main() {
return __awaiter(this, void 0, void 0, function* () {
if (figma.currentPage.selection.length === 0) {
return "Select frames you want to replace with instances";
}
let skippedCount = 0;
let processedCount = 0;
let originalInstances = {}; // cache found instances
const clonedSelection = Object.assign([], figma.currentPage.selection);
for (let index in clonedSelection) {
if (clonedSelection[index].type !== "FRAME") {
skippedCount += 1;
continue;
}
const frame = clonedSelection[index];
let componentReference = null;
if (!(frame.name in originalInstances)) {
// Try to find an instance or master for the frame
componentReference = figma.root.findOne(node => isEquivalentNode(frame, node));
originalInstances[frame.name] = componentReference;
}
else {
componentReference = originalInstances[frame.name];
}
// If instance was found, replace frame with it
if (componentReference !== null) {
let instanceClone;
if (componentReference.type === "INSTANCE") {
instanceClone = componentReference.masterComponent.createInstance();
}
else {
instanceClone = componentReference.createInstance();
}
// Insert instance right above the frame
let frameIndex = frame.parent.children.indexOf(frame);
frame.parent.insertChild(frameIndex + 1, instanceClone);
// Position and resize new instance to frame
instanceClone.x = frame.x;
instanceClone.y = frame.y;
instanceClone.resize(frame.width, frame.height);
yield overrideProperties(frame, instanceClone);
frame.remove();
processedCount += 1;
continue;
}
skippedCount += 1;
continue;
}
return `${processedCount} processed, ${skippedCount} skipped`;
});
}
// Check if node is a component that can replace an instance
function isEquivalentNode(frame, node) {
if (node.type !== "INSTANCE" && node.type !== "COMPONENT")
return false;
if (node.name !== frame.name)
return false;
return true;
}
// Recursively overrides all properties on an instance
function overrideProperties(source, target) {
return __awaiter(this, void 0, void 0, function* () {
// If source is text, the long and tedious process
// of replacing all text properties begins!
if (source.type === "TEXT" && target.type === "TEXT") {
yield overrideTextProperties(source, target);
}
// Try to override every other property
// if it's present in source and not mixed
allProperties.forEach(prop => {
if (!(prop in source))
return;
if (source[prop] === undefined)
return;
if (areEquivalent(target[prop], source[prop]) || isMixed(source[prop]))
return;
try {
target[prop] = source[prop];
}
catch (e) {
console.error(e);
}
});
// Instances can be overriden too
if (source.type === "INSTANCE" && target.type === "INSTANCE") {
if (target.masterComponent.id !== source.masterComponent.id) {
target.masterComponent = source.masterComponent;
}
}
// Recursively change all children
if (supportsChildren(source) && supportsChildren(target)) {
for (let i = 0; i < target.children.length; i++) {
const sourceChild = source.children[i];
const targetChild = target.children[i];
if (!sourceChild || !targetChild)
continue;
yield overrideProperties(sourceChild, targetChild);
}
}
});
}
// Override all properties of text layers
// Mixed properties are applied on each character because there are no alternatives
function overrideTextProperties(source, target) {
return __awaiter(this, void 0, void 0, function* () {
// Collect mixed properties to replace them later
const mixedProperties = [];
const isStringSame = target.characters === source.characters;
// Hacky methods to get character setter/getter methods
// from property names
const getPropertyAtChar = (prop, char) => {
const rangeMethod = "getRange" + prop[0].toUpperCase() + prop.slice(1);
return source[rangeMethod](char, char + 1);
};
const setPropertyAtChar = (prop, char, value) => {
const rangeMethod = "setRange" + prop[0].toUpperCase() + prop.slice(1);
target[rangeMethod](char, char + 1, value);
};
// Start off by checking if characters are the same and changing them
const fontName = source.getRangeFontName(0, 1);
yield figma.loadFontAsync(fontName).then(() => {
// With font loaded we reset all properties to
// ones applied to the first character of source
textProperties.forEach(prop => {
if (isMixed(source[prop])) {
mixedProperties.push(prop);
}
if (isStringSame)
return;
const value = getPropertyAtChar(prop, 0);
if (areEquivalent(target[prop], value)
&& value !== undefined)
return;
target[prop] = value;
});
if (!isStringSame) {
target.characters = source.characters;
}
}).catch((e) => { console.error(e); });
// All properties are set on the whole text without mixed ones!
if (mixedProperties.length === 0) {
return;
}
// Don't override too long texts with many different properties
// because it would take too much time
if (target.characters.length > 150
&& mixedProperties.length > 2) {
return;
}
// Load all fonts on the text block
for (let char = 0; char < target.characters.length; char++) {
const fontName = source.getRangeFontName(char, char + 1);
yield figma.loadFontAsync(fontName).catch((e) => { console.error(e); });
}
// Set properties character by character on mixed fields
// This can't be done in the previous loop
for (let char = 0; char < target.characters.length; char++) {
for (let i = 0; i < mixedProperties.length; i++) {
const prop = mixedProperties[i];
try {
const value = getPropertyAtChar(prop, char);
if (value === null)
continue;
setPropertyAtChar(prop, char, value);
}
catch (e) {
console.error(e);
}
}
}
});
}
// Returns type predicate and true if node supports children
function supportsChildren(node) {
return node.type === "FRAME" || node.type === "COMPONENT" ||
node.type === "INSTANCE" || node.type === "BOOLEAN_OPERATION";
}
function isMixed(property) {
return property === figma.mixed && typeof property === "symbol";
}
// Checks if two objects are equivalent
function areEquivalent(a, b) {
if (a === b) {
return true;
}
if (!(a instanceof Object || b instanceof Object)) {
return false;
}
if (toType(a) !== toType(b)) {
return false;
}
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) {
return false;
}
if (a.length === b.length) {
for (let i = 0; i < a.length; i++) {
if (!areEquivalent(a[i], b[i])) {
return false;
}
}
}
return true;
}
// Create arrays of property names
let aProps = Object.getOwnPropertyNames(a);
let bProps = Object.getOwnPropertyNames(b);
// If number of properties is different,
// objects are not equivalent
if (aProps.length !== bProps.length) {
return false;
}
for (let i = 0; i < aProps.length; i++) {
let prop = aProps[i];
// Recursion: if values of the same property
// are not equal, objects are not equivalent
if (!areEquivalent(a[prop], b[prop])) {
return false;
}
}
// If we made it this far, objects
// are considered equivalent
return true;
}
// Returns type of an object
function toType(obj) {
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
}
// A list of all overrrideable properties in Figma
const allProperties = [
"visible",
"locked",
"opacity",
"blendMode",
"effects",
"effectStyleId",
// frame
"backgrounds",
"layoutGrids",
"clipsContent",
"guides",
"gridStyleId",
"backgroundStyleId",
// geometry
"fills",
"strokes",
"strokes",
"strokeAlign",
"strokeCap",
"strokeJoin",
"dashPattern",
"fillStyleId",
"strokeStyleId",
"cornerRadius",
"cornerSmoothing",
"exportSettings",
// component instance
"masterComponent",
// text
"autoRename",
"textAlignHorizontal",
"textAlignVertical",
"paragraphIndent",
"paragraphSpacing",
];
// Text properties that can be overriden on individual characters
const textProperties = [
"fills",
"fillStyleId",
"fontSize",
"fontName",
"textCase",
"textDecoration",
"letterSpacing",
"lineHeight",
"textStyleId",
];
// Methods are async, close plugin when they are resolved
main().then(msg => {
figma.closePlugin(msg);
});