-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathmain.js
160 lines (131 loc) · 4.04 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
/*
* Automatically insert page numbers
*
*/
const { Artboard, Rectangle, Ellipse, Text, Color } = require("scenegraph");
const { alert } = require("./lib/dialogs.js");
function defineDialogue() {
let dialog = document.createElement("dialog");
let application = require("application");
// Set page caption
let pageNumberCaption = "Page";
if (application.systemLocale === "de_DE") {
pageNumberCaption = "Seite";
}
// Set font size
let pageNumberFontSize = 20;
dialog.innerHTML = `
<style>
form {
width: 360px;
}
.h1 {
align-items: center;
justify-content: space-between;
display: flex;
flex-direction: row;
}
.icon {
border-radius: 4px;
width: 24px;
height: 24px;
overflow: hidden;
}
.row {
align-items: center;
}
</style>
<form method="dialog">
<h1 class="h1">
<span>Insert Page Numbers</span>
<img class="icon" src="./assets/icon.png" />
</h1>
<hr />
<p>You can change the default settings such as the caption and it's font-size before inserting the page numbers.</p>
<label>
<span>Caption</span>
<input type="text" placeholder="e.g., Page" value="${pageNumberCaption}" id="pageCaption"/>
</label>
<label>
<span>Font Size</span>
<input type="number" value="${pageNumberFontSize}" id="fontSize"/>
</label>
<label class="row">
<input type="checkbox" id="onFirstPage"/>
<span>Print Number on First Page</span>
</label>
<footer>
<button uxp-variant="primary" id="cancel">Cancel</button>
<button type="submit" uxp-variant="cta" uxp-selected="true">Insert Numbers</button>
</footer>
</form>
`;
// Append to body
document.body.appendChild(dialog);
// Register cancel callback
const cancelButton = document.querySelector("#cancel");
cancelButton.addEventListener("click", () => dialog.close("reasonCanceled"));
return dialog;
}
async function insertPageNoCommand(selection, root) {
// Check if there are any artboards in the present document
if (root.children.filter(node => node instanceof Artboard).length === 0) {
await alert("Insert Page Numbers",
"There are no artboards present in your current document. Please insert at least one, so this plugin can work correctly.");
return;
}
// Dialogue definition
let settingsDialogue = defineDialogue();
// Register additional callbacks
const onFirstPageCheckbox = document.querySelector("#onFirstPage");
let onFirstPage = false;
onFirstPageCheckbox.addEventListener("change", () => onFirstPage = onFirstPageCheckbox.checked);
// Show dialogue
let dialogueResult = await settingsDialogue.showModal();
// Check if dialogue was cancelled
if (dialogueResult === "reasonCanceled") return;
// Get dialogue values
const fontSize = Number(document.querySelector("#fontSize").value);
let numberCaption = document.querySelector("#pageCaption").value;
// Add a space after the caption if there is one
if (numberCaption.trim().length > 0) {
numberCaption += " ";
}
// Get artboards
let sortedList = root.children.filter(node => node instanceof Artboard);
// Sort list
sortedList = sortedList.sort((a, b) => {
if (a.translation.y < b.translation.y) {
return -1;
} else if (a.translation.y > b.translation.y) {
return 1
} else if (a.translation.y === b.translation.y) {
if (a.translation.x < b.translation.x) {
return -1;
} else {
return 1;
}
}
});
// Loop through sorted list
sortedList.forEach((node, index) => {
if (node instanceof Artboard) {
// Make sure we only print the page number on the first page, if we're allowed to
if (index === 0 && onFirstPage || index > 0) {
let artboard = node;
const textNode = new Text();
textNode.textAlign = "right";
textNode.text = numberCaption + (index + 1).toString();
textNode.fontSize = fontSize;
textNode.fill = new Color("black");
artboard.addChild(textNode);
textNode.moveInParentCoordinates(artboard.globalBounds.width - 30, artboard.globalBounds.height - 30);
}
}
})
}
module.exports = {
commands: {
insertPageNoCommand: insertPageNoCommand
}
};