-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathmain.js
100 lines (82 loc) · 2.75 KB
/
main.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
const { alert, error } = require("./lib/dialogs.js");
// Fit to parent width & height
function fitToParent(selection) {
fit(selection)
}
// Fit to parent width
function fitToParentWidth(selection) {
fit(selection, 'width')
}
// Fit to parent height
function fitToParentHeight(selection) {
fit(selection, 'height')
}
// Resize and move
function fit(selection, command) {
// If no selection
if (0 === selection.items.length) {
alert("Nothing Selected",
"Please select one or more objects that are either inside a Group or on an Artboard.");
return false;
// If objects selected
} else {
// Iterate through the selection
selection.items.forEach(function (item) {
const parent = item.parent;
const distanceToMove = calculateDistance(item, parent);
let desiredWidth = parent.globalDrawBounds.width;
let desiredHeight = parent.globalDrawBounds.height;
let currentWidth = item.globalDrawBounds.width;
let currentHeight = item.globalDrawBounds.height;
// If there is no parent
if ('RootNode' === parent.constructor.name) {
alert(`No Parents`,
"To resize objects, they need to be inside a Group or on an Artboard.");
return false;
}
switch(command) {
case 'width': {
item.moveInParentCoordinates(-distanceToMove.x, 0);
resizeObject(item, desiredWidth, currentHeight);
break;
}
case 'height': {
item.moveInParentCoordinates(0, -distanceToMove.y);
resizeObject(item, currentWidth, desiredHeight);
break;
}
default: {
item.moveInParentCoordinates(-distanceToMove.x, -distanceToMove.y);
resizeObject(item, desiredWidth, desiredHeight);
break;
}
}
})
}
}
function resizeObject(item, w, h) {
// If it's not text, use the normal resize
if (item.constructor.name !== 'Text') {
item.resize(w, h);
// If it's text, convert to areaBox and set width/height
} else {
item.areaBox = {
width: w,
height: h,
}
}
}
function calculateDistance(item, parent) {
let distanceToMove = {
x: item.globalDrawBounds.x - parent.globalDrawBounds.x,
y: item.globalDrawBounds.y - parent.globalDrawBounds.y
}
return distanceToMove;
}
module.exports = {
commands: {
"FitToParent": fitToParent,
"FitToParentWidth": fitToParentWidth,
"FitToParentHeight": fitToParentHeight
}
};