-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
142 lines (122 loc) · 5.25 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
/*
* Copyright (c) 2018 Peter Flynn
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*jshint esnext: true */
/*globals module, require, exports, console, document */
const sg = require("scenegraph");
function showDialog(content, buttons) {
var dialog = document.createElement("dialog");
dialog.innerHTML = `
<style>
.space-below { margin-bottom: 12px; }
</style>
<form method="dialog">
<h1>TrimIt</h1>
<hr>
${content}
<footer>${buttons}</footer>
</form>`;
document.appendChild(dialog);
return dialog.showModal().then(function () {
dialog.remove();
});
}
function trimArtboard(artboard) {
var minX, minY, maxX, maxY;
artboard.children.forEach(function (node) {
var bounds = node.boundsInParent; // might be a tad faster to work in terms of globalBounds, but whatever
if (bounds.x < minX || minX === undefined) { minX = bounds.x; }
if (bounds.y < minY || minY === undefined) { minY = bounds.y; }
// This is actually 1px past the max, which is why we don't need to +1 when computing the new artboard width below
if (bounds.x + bounds.width > maxX || maxX === undefined) { maxX = bounds.x + bounds.width; }
if (bounds.y + bounds.height > maxY || maxY === undefined) { maxY = bounds.y + bounds.height; }
});
if (!maxX || !maxY) {
// If artboard is empty (0 children, or only empty groups), leave it untouched
return;
}
// Upper-left crop
artboard.moveInParentCoordinates(minX, minY);
artboard.children.forEach(function (node) {
node.moveInParentCoordinates(-minX, -minY);
});
// Bottom-right crop
artboard.width = maxX - minX;
artboard.height = maxY - minY;
}
function trimText(node) {
// Converge on perfect height by performing the following with progressively smaller increments:
// - If clippedByArea, expand until not
// - If not clippedByArea, shrink until is
var style = node.styleRanges[0];
var increment = style.lineSpacing || style.fontSize;
if (!node.clippedByArea) { increment = -increment; }
var height = node.areaBox.height;
for (; Math.abs(increment) >= 1; increment = -Math.trunc(increment / 2)) {
var origValue = node.clippedByArea;
while (node.clippedByArea === origValue) {
height += increment;
console.log(height);
node.resize(node.areaBox.width, height);
}
}
if (node.clippedByArea) {
node.resize(node.areaBox.width, height + 1);
}
}
function trimRepeatGrid(rgrid) {
// Proper snapped width is cellSize.width*N + rgrid.paddingX*(N-1) -- for some integer N
// So N = (width + paddingX) / (cellSize.width + paddingX)
var cellSize = rgrid.cellSize;
var paddingX = rgrid.paddingX;
var paddingY = rgrid.paddingY;
var cols = Math.round((rgrid.localBounds.width + paddingX) / (cellSize.width + paddingX));
var rows = Math.round((rgrid.localBounds.height + paddingY) / (cellSize.height + paddingY));
rgrid.resize(cols*cellSize.width + (cols-1)*paddingX, rows*cellSize.height + (rows-1)*paddingY);
}
function trimSelection(selection, root) {
var didSomething = false;
selection.items.forEach(function (node) {
if (node instanceof sg.Artboard) {
didSomething = true;
trimArtboard(node);
} else if (node instanceof sg.Text && node.areaBox) {
didSomething = true;
trimText(node);
} else if (node instanceof sg.RepeatGrid) {
didSomething = true;
trimRepeatGrid(node);
}
});
if (!didSomething) {
showDialog(`<div class="space-below">Select one or more items to trim:</div>
<ul>
<li>• Artboard – snap to size of contents, eliminating any margin</li>
<li>• Repeat Grid – snap to nearest whole number of grid cells</li>
<li>• Area Text – snap height to fit text perfectly with no clipping</li>
</ul>`,
`<button id="ok" type="submit" uxp-variant="cta">OK</button>`);
// (note: due to UXP bug, can't dismiss this dialog via Enter key...)
}
}
exports.commands = {
trimSelection: trimSelection
};