-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
90 lines (73 loc) · 2.5 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
/*
* Adobe XD Plugin to visually move back and forth through the artboard stack, zooming each to fill screen
*/
const Artboard = require("scenegraph").Artboard;
const viewport = require("viewport");
const commands = require("commands");
const { alert, error } = require("./lib/dialogs.js");
var currentArtboard, currentIndex, targetBoard, allArtboards;
var cachedArtboard = [];
async function showAlert(targ) {
/* we'll display a dialog here */
await alert('No Artboards Found...', '"Go to '+targ+' artboard," you say? \n\nThere are no artboards here. \n\nThank you for your attempt, though. Please do this again when you have artboards.');
}
function init(selection,root) {
allArtboards = root.children.filter(node=>{
if (node instanceof Artboard) {
return node;
}
});
// Start from selected artboard, if available:
let selectedArtboard = selection.focusedArtboard;
if (selectedArtboard && cachedArtboard && cachedArtboard.toString() != selection.focusedArtboard.toString() ) {
currentArtboard = selectedArtboard;
}
// Allow currentArtboard to fall back to the first artboard in project if still undefined:
currentArtboard = currentArtboard || allArtboards[0];
// store current selection so we don't use it to refocus Artboard if user is going through several boards.
cachedArtboard = selection.focusedArtboard;
// update currentArtboard and currentIndex to last set
allArtboards.forEach((node,idx)=>{
if (currentArtboard == node) {
currentIndex = idx;
}
});
}
function prevArtboard(selection,root) {
init(selection,root);
// end early when no artboards found
if (!allArtboards.length) {
showAlert('next');
return;
}
if (currentIndex + 1 >= allArtboards.length) {
targetBoard = allArtboards[0];
}
else {
targetBoard = allArtboards[currentIndex + 1];
}
viewport.zoomToRect(targetBoard);
currentArtboard = targetBoard;
}
function nextArtboard(selection,root) {
init(selection,root);
// end early when no artboards found
if (!allArtboards.length) {
showAlert('previous');
return;
}
if (currentIndex - 1 < 0) {
targetBoard = allArtboards[allArtboards.length - 1];
}
else {
targetBoard = allArtboards[currentIndex - 1];
}
viewport.zoomToRect(targetBoard);
currentArtboard = targetBoard;
}
module.exports = {
commands: {
nextArtboard,
prevArtboard
}
};