-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
181 lines (161 loc) · 6.02 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
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
/*
* 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.
*/
const {Artboard} = require("scenegraph");
const Viewport = require("viewport");
const DIALOG_CANCELED = "reasonCanceled";
var origViewport;
var candidates;
var filtered;
var selectedI;
// General behavior:
// - Type to filter artboards by name; top 5 hits are shown (dialogs can't resize yet)
// - Top item semi-highlighted to indicate pressing Enter will go to it
// - Arrow keys to select: pans to artboard immediately (then nothing more happens on Enter)
// - Editing filter text after using arrows to select tries to keep the same item selected if it's still in the top results
// - If only one result after filtering, select & go to it automatically
// - Closing dialog via Escape reverts viewport to original position
function escape(text) {
return text.replace(/&/g, "&").replace(/</g, "<");
}
function gatherCandidates(root) {
candidates = root.children.filter(node => node instanceof Artboard);
}
function rerenderList() {
var listItemsHTML = "";
filtered.forEach((node, i) => {
var cssClass = (i === selectedI)? "selected" : (i === 0 && selectedI === -1)? "onDeck" : "";
listItemsHTML += `<div class="${cssClass}">${escape(node.name)}</div>`;
});
document.getElementById("list").innerHTML = listItemsHTML;
}
function updateList(filterText) {
var selectedArtboard = filtered[selectedI]; // undefined if nothing selected
filterText = filterText.toLowerCase();
filtered = candidates.filter(node => node.name.toLowerCase().indexOf(filterText) !== -1);
filtered = filtered.slice(0, 5);
if (filtered.length === 1) {
selectedI = 0;
gotoArtboard(filtered[0]);
} else {
// If last selected artboard is still in filtered results, keep it selected if possible
selectedI = filtered.indexOf(selectedArtboard);
// (will be -1 if nothing was selected before, or if selected artboard no longer in filtered results)
}
rerenderList();
}
function quickNav(selection, root) {
filtered = [];
selectedI = -1;
var dialog = document.createElement("dialog");
dialog.innerHTML = `
<style>
.row {
display: flex;
align-items: center;
}
#list {
padding-top: 8px;
width: 300px;
}
#list div {
height: 35px;
padding: 9px 5px 2px 5px;
font-size: 12px;
}
#list div.selected {
background: #d8d8d8;
border-radius: 5px;
}
#list div.onDeck {
border: 1px solid #a0a0a0;
padding: 8px 4px 1px 4px; /* compensate for border */
border-radius: 5px;
}
</style>
<form method="dialog">
<h1>Artboard Quick Navigate</h1>
<hr>
<div class="row">
<input type="text" uxp-quiet="true" id="input" />
</div>
<div id="list"></div>
</form>`;
document.appendChild(dialog);
origViewport = Viewport.bounds;
gatherCandidates(root);
updateList("");
var input = document.getElementById("input");
input.addEventListener("input", function () {
updateList(input.value);
});
input.addEventListener("keydown", function (event) {
if (event.code === "ArrowDown") {
if (filtered.length && selectedI < filtered.length - 1) {
selectedI++;
gotoArtboard(filtered[selectedI]);
rerenderList();
}
} else if (event.code === "ArrowUp") {
if (filtered.length && selectedI > 0) {
selectedI--;
gotoArtboard(filtered[selectedI]);
rerenderList();
}
}
});
// Enter key automatically 'submits' the form
// Esc key automatically cancels
return dialog.showModal().then(function (reason) {
dialog.remove();
if (reason === DIALOG_CANCELED) {
Viewport.scrollToCenter(origViewport.x + origViewport.width / 2, origViewport.y + origViewport.height / 2);
} else if (filtered.length > 0 && selectedI === -1) {
gotoArtboard(filtered[0]);
}
});
}
function gotoArtboard(artboard) {
var bounds = artboard.globalBounds;
var viewport = Viewport.bounds;
// Include artboard title label in bounds we're centering
const LABEL_HT = 18 / Viewport.zoomFactor;
bounds.y -= LABEL_HT;
bounds.height += LABEL_HT;
if (bounds.width <= viewport.width && bounds.height <= viewport.height) {
// If entire bounds fits, center it
Viewport.scrollToCenter(
bounds.x + bounds.width / 2,
bounds.y + bounds.height / 2
);
} else {
// Otherwise, place UL of bounds in UL of viewport
Viewport.scrollToCenter(
bounds.x - 13 + viewport.width / 2,
bounds.y - 10 + viewport.height / 2
);
}
}
module.exports = {
commands: {
quickNav: quickNav
}
};