-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathmain.js
147 lines (137 loc) · 5.27 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
/**
* Selection to Artboard Plugin
*/
var scenegraph = require('scenegraph');
var commands = require('commands');
var nrArt = 0;
/**
* Shorthand for creating Elements.
* @param {*} tag The tag name of the element.
* @param {*} [props] Optional props.
* @param {*} children Child elements or strings
*/
function h(tag, props, ...children) {
let element = document.createElement(tag);
if (props) {
if (props.nodeType || typeof props !== "object") {
children.unshift(props);
}
else {
for (let name in props) {
let value = props[name];
if (name == "style") {
Object.assign(element.style, value);
}
else {
element.setAttribute(name, value);
element[name] = value;
}
}
}
}
for (let child of children) {
element.appendChild(typeof child === "object" ? child : document.createTextNode(child));
}
return element;
}
let dialog;
function getDialog() {
if (dialog == null) {
dialog =
h("dialog",
h("form", { style: { width: 360 } },
h("h1", "Selection to Artboard Plugin"),
h("hr", ""),
h("p", ""),
h("p", "Please select one or more layers."),
h("p", ""),
h("footer",
h("button", { uxpVariant:"cta", type:"submit", onclick() { dialog.close() } }, "OK")
)
)
)
}
return dialog;
}
// Global coordinates of Top Left Corner of Selection
function findTopLeftCornerOfSelection(userSelection) {
var minX = userSelection[0].globalBounds.x,
minY = userSelection[0].globalBounds.y;
userSelection.forEach(function(node) {
minX = node.globalBounds.x < minX ? node.globalBounds.x : minX;
minY = node.globalBounds.y < minY ? node.globalBounds.y : minY;
});
return {x: minX, y: minY};
}
// Global coordinates of Bottom Right Corner of Selection
function findBottomRightCornerOfSelection(userSelection) {
var maxX = userSelection[0].globalBounds.x + userSelection[0].globalBounds.width,
maxY = userSelection[0].globalBounds.y + userSelection[0].globalBounds.height;
userSelection.forEach(function(node) {
maxX = node.globalBounds.x + node.globalBounds.width > maxX ? node.globalBounds.x + node.globalBounds.width : maxX;
maxY = node.globalBounds.y + node.globalBounds.height > maxY ? node.globalBounds.y + node.globalBounds.height : maxY;
});
return {x: maxX, y: maxY};
}
// Check if any elements of the Selection are positioned on Artboard
function checkParentArtboard(selection, root) {
return selection.items.reduce((acc, node) => {
while (!(node instanceof scenegraph.Artboard) && node != root) {
node = node.parent;
}
return acc || (node != root);
}, false);
}
function execution (selection, root) {
if (selection.items.length == 0 || selection.items[0] instanceof scenegraph.Artboard) {
document.body.appendChild(getDialog()).showModal();
return;
}
var topLeft = findTopLeftCornerOfSelection(selection.items),
bottomRight = findBottomRightCornerOfSelection(selection.items);
// The selection will be copied on a new custom sized Artboard
if (checkParentArtboard(selection, root)) {
var userSelection = selection.items;
var artboard = new scenegraph.Artboard();
artboard.fill = new scenegraph.Color('white');
artboard.width = 375;
artboard.height = 667;
nrArt++;
// Artboard must be placed lowest in the Z order
root.addChildBefore(artboard, root.children.at(0));
selection.items = [artboard];
commands.duplicate();
var newArtboard = selection.items[0],
newArtboardBounds = newArtboard.globalBounds;
newArtboard.name = 'Artboard - ' + nrArt;
artboard.removeFromParent();
selection.items = userSelection;
// The selection is duplicated
commands.duplicate();
// Each duplicated element is positioned on the new Artboard
selection.items.forEach(function(node) {
node.moveInParentCoordinates(newArtboardBounds.x - topLeft.x, newArtboardBounds.y - topLeft.y);
});
// The new Artboard is resized to fit the selection
newArtboard.resize(bottomRight.x - topLeft.x, bottomRight.y - topLeft.y);
} else {
// All the selected elements are placed on Pasteboard
// A new Artboard will be placed under the selection
var artboard = new scenegraph.Artboard();
artboard.fill = new scenegraph.Color('white');
artboard.width = 375;
artboard.height = 667;
nrArt++;
artboard.name = 'Artboard - ' + nrArt;
// Artboard must be placed lowest in the Z order
root.addChildBefore(artboard, root.children.at(0));
var artboardTopLeft = {x: artboard.localBounds.x, y: artboard.localBounds.y};
artboard.placeInParentCoordinates(artboardTopLeft, topLeft);
artboard.resize(bottomRight.x - topLeft.x, bottomRight.y - topLeft.y);
}
}
module.exports = {
commands: {
execution
}
};