-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
479 lines (477 loc) · 20.4 KB
/
index.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
(() => {
const defines = {};
const entry = [null];
function define(name, dependencies, factory) {
defines[name] = { dependencies, factory };
entry[0] = name;
}
define("require", ["exports"], (exports) => {
Object.defineProperty(exports, "__cjsModule", { value: true });
Object.defineProperty(exports, "default", { value: (name) => resolve(name) });
});
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
define("src/modules/createStyle", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = (elevation, styleId = null, styleName = 'Elevation') => {
// get or create new style
const style = figma.getStyleById(styleId) || figma.createEffectStyle();
// set style name
style.name = styleName;
// replace if missing
if (styleName === null || styleName.trim() === '') {
style.name = 'Elevation';
}
// set effects
const effects = elevation.map(layer => {
const { name } = layer, effects = __rest(layer
// return effects only
, ["name"]);
// return effects only
return effects;
});
style.effects = effects;
return style;
};
});
define("src/modules/containerStore", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContainerData = exports.setContainerData = exports.storeKeys = void 0;
exports.storeKeys = {
ELEVATION_SETTNGS: 'elevationSettings'
};
const isValidContainer = (container) => {
if (container !== undefined && container !== null) {
return true;
}
return false;
};
const setContainerData = (container, key, data) => {
if (isValidContainer(container)) {
if (typeof data === 'object') {
container.setPluginData(key, JSON.stringify(data));
}
if (typeof data === 'number' || typeof data === 'string') {
container.setPluginData(key, data);
}
}
};
exports.setContainerData = setContainerData;
const getContainerData = (container, key) => {
if (isValidContainer(container)) {
try {
const jsonString = JSON.parse(container.getPluginData(key));
return jsonString;
}
catch (e) {
}
return container.getPluginData(key);
}
return null;
};
exports.getContainerData = getContainerData;
});
define("src/modules/createPreviewElement", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const SETTINGS = {
WIDTH: 320,
HEIGHT: 120,
RADIUS: 5,
NAME: 'Elevation'
};
exports.default = (index, elevationLayers) => {
// remove name from layers
const effects = elevationLayers.map(layer => {
const { name } = layer, effects = __rest(layer
// retuzrn effects only
, ["name"]);
// retuzrn effects only
return effects;
});
// create element
const element = figma.createRectangle();
// set name
element.name = `${SETTINGS.NAME} ${index}`;
// set size
element.resizeWithoutConstraints(SETTINGS.WIDTH, SETTINGS.HEIGHT);
// set radius
element.cornerRadius = SETTINGS.RADIUS;
// set fill to white
element.fills = [{ type: 'SOLID', color: { r: 1, g: 1, b: 1 } }];
// set elevation
element.effects = effects;
// return
return element;
};
});
define("src/modules/parseValue", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const placeholders = {
INDEX: '#'
};
exports.default = (value, index) => {
// replace placeholders in string
if (typeof value === 'string') {
value = value
// replace comma
.replace(',', '.')
// replace space
.replace(' ', '')
// replace 0 at beginning
.replace(/^0+/i, '')
// replace 0 after char
.replace(/(\+|\*|\/|-|#)(0)(.)/i, '$1')
// replace placeholder with index
.replace(placeholders.INDEX, index);
// replace leading zeros
}
// eval and parse int
try {
value = parseFloat(eval(value));
if (!isNaN(value)) {
return value;
}
}
catch (e) {
console.error(e);
}
return 0;
};
});
define("src/modules/minMax", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = (number, min, max) => {
// return min if number smaller
if (number < min) {
return min;
}
// return max if number bigger
if (number > max) {
return max;
}
// return number if between
return number;
};
});
define("src/modules/hexToRgba", ["require", "exports", "src/modules/minMax"], function (require, exports, minMax_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
minMax_1 = __importDefault(minMax_1);
exports.default = (hex, opacity) => {
// extract rgb from hex
const [, r, g, b] = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
// return rgb
return {
r: parseInt(r, 16) / 255,
g: parseInt(g, 16) / 255,
b: parseInt(b, 16) / 255,
a: minMax_1.default(parseInt(opacity, 10) / 100, 0, 1)
};
};
});
define("src/modules/createElevationLayer", ["require", "exports", "src/modules/parseValue", "src/modules/hexToRgba"], function (require, exports, parseValue_1, hexToRgba_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
parseValue_1 = __importDefault(parseValue_1);
hexToRgba_1 = __importDefault(hexToRgba_1);
const allowedEffectType = ['DROP_SHADOW', 'INNER_SHADOW'];
exports.default = (index, layer) => {
return {
// define elevation
name: layer.name,
type: allowedEffectType.includes(layer.type) ? layer.type : 'DROP_SHADOW',
color: hexToRgba_1.default(layer.color, parseValue_1.default(layer.opacity, index)),
offset: {
x: parseValue_1.default(layer.x, index),
y: parseValue_1.default(layer.y, index)
},
spread: parseValue_1.default(layer.spread, index),
radius: parseValue_1.default(layer.radius, index),
// defaults
blendMode: 'NORMAL',
visible: true
};
};
});
define("src/modules/updateElevation", ["require", "exports", "src/modules/createStyle", "src/modules/containerStore", "src/modules/createPreviewElement", "src/modules/createElevationLayer"], function (require, exports, createStyle_1, containerStore_1, createPreviewElement_1, createElevationLayer_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
createStyle_1 = __importDefault(createStyle_1);
createPreviewElement_1 = __importDefault(createPreviewElement_1);
createElevationLayer_1 = __importDefault(createElevationLayer_1);
exports.default = (figma, container, data) => {
const focusNodes = [];
// remove children nodes
data.steps = parseInt(data.steps);
container.children.forEach(child => child.remove());
// get styles & id
const containerData = containerStore_1.getContainerData(container, containerStore_1.storeKeys.ELEVATION_SETTNGS);
data.containerId = containerData.containerId;
data.styles = containerData.styles || [];
// add updated children nodes
for (let i = 0; i < data.steps; i++) {
// get elevation
const elevation = [...data.elevationLayer].map(layer => {
return createElevationLayer_1.default(i, layer);
});
// elevation name
const elevationName = elevationStyleName(i, data.styleName);
// create elements
const previewElements = createPreviewElement_1.default(i, elevation);
// append to container
container.appendChild(previewElements);
focusNodes.push(previewElements);
// create styles
if (data.createStyles === true) {
const style = createStyle_1.default(elevation, data.styles[i] || null, elevationName);
data.styles[i] = style.id;
}
// sync styles is off
else if (data.styles.length > 0) {
data.styles.forEach(styleId => {
figma.getStyleById(styleId).remove();
});
// unset styles
data.styles = [];
}
}
// zoom to container if new
figma.viewport.scrollAndZoomIntoView(focusNodes);
// elevation settings
containerStore_1.setContainerData(container, containerStore_1.storeKeys.ELEVATION_SETTNGS, data);
// append & select
figma.currentPage.selection = [container];
};
const elevationStyleName = (i, styleName) => {
if (styleName !== undefined) {
const number = String(i).padStart(2, '0');
if (styleName.indexOf('#') > -1) {
return styleName.replace('##', number).replace('#', String(i));
}
else {
return `${styleName} ${number}`;
}
}
return null;
};
});
define("src/modules/fixDuplicate", ["require", "exports", "src/modules/containerStore"], function (require, exports, containerStore_2) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = currentContainer => {
const containerData = containerStore_2.getContainerData(currentContainer, containerStore_2.storeKeys.ELEVATION_SETTNGS);
// remove styles if container was duplicated
if (containerData.containerId !== null && containerData.containerId !== currentContainer.id) {
containerData.styles = [];
containerData.createStyles = false;
}
// add container id
containerData.containerId = currentContainer.id;
// update container data
containerStore_2.setContainerData(currentContainer, containerStore_2.storeKeys.ELEVATION_SETTNGS, containerData);
};
});
define("src/modules/getCurrentContainer", ["require", "exports", "src/modules/containerStore", "src/modules/fixDuplicate"], function (require, exports, containerStore_3, fixDuplicate_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
fixDuplicate_1 = __importDefault(fixDuplicate_1);
exports.default = (figma) => {
// get current selection
const currentSelection = figma.currentPage.selection[0];
// return if frame
if (currentSelection !== undefined && currentSelection.type === 'FRAME' && containerStore_3.getContainerData(currentSelection, containerStore_3.storeKeys.ELEVATION_SETTNGS)) {
// deal with duplicates
fixDuplicate_1.default(currentSelection);
// return current container
return currentSelection;
}
//
return null;
};
});
define("src/modules/refreshUi", ["require", "exports", "src/modules/containerStore"], function (require, exports, containerStore_4) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const SETTINGS = {
LAYER_SIZE: 40,
BASE_SIZE: 562,
UI_WIDTH: 360,
EMPTY_STATE_WIDTH: 360,
EMPTY_STATE_HEIGHT: 160
};
exports.default = (figma, container) => {
// show the html ui
figma.showUI(__html__, {
width: SETTINGS.EMPTY_STATE_WIDTH,
height: SETTINGS.EMPTY_STATE_HEIGHT
});
// if selected container
if (container !== null) {
const elevationProperties = containerStore_4.getContainerData(container, containerStore_4.storeKeys.ELEVATION_SETTNGS);
// calc height for UI window
const UI_HEIGHT = 700;
// update UI size
figma.ui.resize(SETTINGS.UI_WIDTH, UI_HEIGHT);
// send data to UI
figma.ui.postMessage(JSON.stringify({
type: 'updateProperties',
properties: elevationProperties
}));
}
// if no container is selected
else {
figma.ui.postMessage(JSON.stringify({
type: 'emptyState'
}));
}
};
});
define("src/modules/createContainer", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const SETTINGS = {
NAME: 'Elevation Scale',
LAYOUT_MODE: 'VERTICAL',
SPACING: 20,
PADDING: 20
};
exports.default = (figma) => {
const container = figma.createFrame();
container.name = SETTINGS.NAME;
container.layoutMode = SETTINGS.LAYOUT_MODE;
container.primaryAxisSizingMode = 'AUTO';
container.counterAxisSizingMode = 'AUTO';
container.itemSpacing = SETTINGS.SPACING;
container.paddingTop = SETTINGS.PADDING;
container.paddingRight = SETTINGS.PADDING;
container.paddingBottom = SETTINGS.PADDING;
container.paddingLeft = SETTINGS.PADDING;
// return container
return container;
};
});
define("src/modules/defaults", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ELEVATION_DEFAULTS = void 0;
exports.ELEVATION_DEFAULTS = {
name: 'Elevation',
containerId: null,
steps: 5,
createStyles: false,
elevationLayer: [{
type: 'DROP_SHADOW',
color: '000000',
opacity: '10+#',
x: 0,
y: '0.5*#',
spread: '2*#',
radius: '#'
}],
styles: []
};
});
define("src/modules/addNewContainer", ["require", "exports", "src/modules/createContainer", "src/modules/containerStore", "src/modules/defaults"], function (require, exports, createContainer_1, containerStore_5, defaults_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
createContainer_1 = __importDefault(createContainer_1);
exports.default = (figma) => {
// create new container
const container = createContainer_1.default(figma);
// append to current page
figma.currentPage.appendChild(container);
// add default data
containerStore_5.setContainerData(container, containerStore_5.storeKeys.ELEVATION_SETTNGS, defaults_1.ELEVATION_DEFAULTS);
// select new container
return container;
};
});
define("src/index", ["require", "exports", "src/modules/updateElevation", "src/modules/getCurrentContainer", "src/modules/refreshUi", "src/modules/addNewContainer", "src/modules/defaults"], function (require, exports, updateElevation_1, getCurrentContainer_1, refreshUi_1, addNewContainer_1, defaults_2) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
updateElevation_1 = __importDefault(updateElevation_1);
getCurrentContainer_1 = __importDefault(getCurrentContainer_1);
refreshUi_1 = __importDefault(refreshUi_1);
addNewContainer_1 = __importDefault(addNewContainer_1);
/**
* This is were the execution of the plugin starts
*/
refreshUi_1.default(figma, getCurrentContainer_1.default(figma));
// run code on commands from UI
figma.ui.onmessage = msg => {
// create a new scale
if (msg.type === 'createScale') {
// create new container
const newContainer = addNewContainer_1.default(figma);
// update container
updateElevation_1.default(figma, newContainer, Object.assign({ type: 'updateScale' }, defaults_2.ELEVATION_DEFAULTS));
}
// update an exsisting scale
if (msg.type === 'updateScale') {
updateElevation_1.default(figma, getCurrentContainer_1.default(figma), msg);
}
};
// update ui if selection changes
figma.on('selectionchange', () => {
refreshUi_1.default(figma, getCurrentContainer_1.default(figma));
});
});
'marker:resolver';
function get_define(name) {
if (defines[name]) {
return defines[name];
}
else if (defines[name + '/index']) {
return defines[name + '/index'];
}
else {
const dependencies = ['exports'];
const factory = (exports) => {
try {
Object.defineProperty(exports, "__cjsModule", { value: true });
Object.defineProperty(exports, "default", { value: require(name) });
}
catch (_a) {
throw Error(['module "', name, '" not found.'].join(''));
}
};
return { dependencies, factory };
}
}
const instances = {};
function resolve(name) {
if (instances[name]) {
return instances[name];
}
if (name === 'exports') {
return {};
}
const define = get_define(name);
instances[name] = {};
const dependencies = define.dependencies.map(name => resolve(name));
define.factory(...dependencies);
const exports = dependencies[define.dependencies.indexOf('exports')];
instances[name] = (exports['__cjsModule']) ? exports.default : exports;
return instances[name];
}
if (entry[0] !== null) {
return resolve(entry[0]);
}
})();