/
main.js
152 lines (133 loc) · 4.36 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
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
/**
* Steve Kwak
*
* How to use the plugin
* Grouping:
* 1. Group the items including the item you want to duplicate as well as all the items you want to fit the duplicated to
* 2. Once grouping is done, select the original item and then use one of the following three commands.
*
* Non-grouping (Artboard):
* 1. Select an item that you want to duplicate and fit to the Artboart (make sure the item is inside or touching the Artboard)
* 2. Use one of the following three commands to fit the Artboard
*
* - "Fit Item": Fit the selected item to the artboard (rotation will be moved back to the original rotation).
* - "Rotate Clock Fit Item": Rotate the item clockwise and fit the artboard.
* - "Rotate Counter Clock Fit Item": Rotate the item counter clockwise and fit the artboard.
*
*/
var Artboard = require("scenegraph").Artboard;
var Group = require("scenegraph").Group;
var commands = require("commands");
function fitItem(selection) {
fitIt(selection, "original");
}
function rotatateClockFitItem(selection) {
fitIt(selection, "right");
}
function rotateCounterClockFitItem(selection) {
fitIt(selection, "left");
}
// Main function
function fitIt(selection, direction) {
if (selection.hasArtwork) {
var selectedItem = selection.items[0];
var itemsToFit = getItemsToFit(selection);
itemsToFit.forEach(function (itemToFit) {
// Duplicate the selected item
commands.duplicate();
// Rotate the selection as requested
rotate(selection.items[0], direction, selection.items[0].localCenterPoint);
// Resize to fit the target object
resize(selection.items[0], itemToFit.globalBounds);
// Place the item in the center of the target object
if (itemToFit.parent instanceof Group) {
selection.items[0].placeInParentCoordinates(selection.items[0].localCenterPoint, getCenterPoint(itemToFit));
} else {
selection.items[0].placeInParentCoordinates(selection.items[0].localCenterPoint, itemToFit.localCenterPoint);
}
// Bring the item to the front
commands.bringToFront()
})
}
}
// Helper functions
function getItemsToFit(selection) {
var selectedItem = selection.items[0];
var parent = selectedItem.parent;
var itemsToFit = [];
// First use case where items are grouped
if (parent instanceof Group) {
parent.children.forEach(function (childNode, i) {
if (!childNode.selected) {
itemsToFit.push(childNode);
}
});
} else {
// Second use case where items are not grouped (Fitting the Artboard)
while (true) {
if (parent instanceof Artboard) {
itemsToFit.push(parent);
break;
} else {
parent = parent.parent;
}
}
}
return itemsToFit;
}
function rotate(node, direction, centerPoint) {
// Rotate the duplicated item based on the specified direction
if (direction == "original") {
if (node.rotation == -90) {
node.rotateAround(90, centerPoint);
} else if (node.rotation == 90) {
node.rotateAround(-90, centerPoint);
} else if (node.rotation == 180) {
node.rotateAround(-180, centerPoint);
} else if (node.rotation == -180) {
node.rotateAround(180, centerPoint);
}
} else if (direction == "left") {
if (node.rotation == 0) {
node.rotateAround(-90, centerPoint);
} else if (node.rotation == 90) {
node.rotateAround(-180, centerPoint);
} else if (node.rotation == 180) {
node.rotateAround(-270, centerPoint);
} else if (node.rotation == -180) {
node.rotateAround(-270, centerPoint);
}
} else if (direction == "right") {
if (node.rotation == 0) {
node.rotateAround(90, centerPoint);
} else if (node.rotation == -90) {
node.rotateAround(180, centerPoint);
} else if (node.rotation == 180) {
node.rotateAround(-90, centerPoint);
} else if (node.rotation == -180) {
node.rotateAround(270, centerPoint);
}
}
}
function resize(node, globalBounds) {
// Resize to fit the artboard
if (node.rotation == 0 || node.rotation == -180 || node.rotation == 180) {
node.resize(globalBounds.width, globalBounds.height);
} else if (node.rotation == 90 || node.rotation == -90) {
node.resize(globalBounds.height, globalBounds.width);
}
}
function getCenterPoint(node) {
console.log(node.parent)
return {
x: node.boundsInParent.x + node.boundsInParent.width / 2,
y: node.boundsInParent.y + node.boundsInParent.height / 2
}
}
module.exports = {
commands: {
fitItem: fitItem,
rotatateClockFitItem: rotatateClockFitItem,
rotateCounterClockFitItem: rotateCounterClockFitItem
}
};