-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
244 lines (212 loc) · 5.61 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const application = require("application")
const fs = require("uxp").storage.localFileSystem
const removeTheNotApproved = (unsanitized) => {
return unsanitized.replace(/[\\:*?"<>|#]/g, '')
}
const dashesForSlashes = (unsanitized) => {
return unsanitized.replace(/\//g, '-')
}
/**
* Removes characters which are not allowed in file names and inserts dashes (-) for slashes.
*
* Removed: \:*?"<>|#
* Dashed: /
*/
const sanitizeName = (unsanitized) => {
return removeTheNotApproved(dashesForSlashes(unsanitized))
}
async function exportAt(selection, scale, format) {
const renditionSettings = []
const folder = await fs.getFolder()
const ext = format.toLowerCase()
await Promise.all(selection.items.map(async (item) => {
try {
const base = sanitizeName(item.name)
const fileName = scale === 1
? `${base}.${ext}`
: `${base}@${scale}x.${ext}`
const file = await folder.createFile(fileName, { overwrite: true })
renditionSettings.push({
node: item,
outputFile: file,
type: application.RenditionType[format],
scale: scale,
// quality only matters for JPG
quality: format === 'JPG'
? 100
: undefined,
})
} catch (e) {
console.log(e)
}
}))
application.createRenditions(renditionSettings)
// .then((results) => {})
.catch((error) => {
console.log(error)
})
}
function exportDialog() {
/**
* Height and width need to be set up front. Can't them change later
*
* On Windows the size of dialog is constrained by the size of the window
*/
const dialog = document.createElement("dialog")
const selectId = 'select'
dialog.innerHTML = `
<style type="text/css">
.formatChoice {
display: flex;
justify-content: flex-start;
align-items: center;
}
</style>
<form method="dialog">
<div class="formatChoice"><span>Exporting as:</span>
<select
id="${selectId}"
>
<!-- set to PNG by default by adding "selected" attribute -->
<option
value="PNG"
selected
>PNG</option>
<option
value="JPG"
>JPG</option>
</select>
</div>
<div>Which scale do you want to export at?</div>
<footer>
<button
id="cancel"
type="reset"
uxp-variant="secondary"
uxp-quiet="true"
>Cancel</button>
<button
id="btn1x"
type="button"
uxp-variant="primary"
>1x</button>
<button
id="btn2x"
type="button"
uxp-variant="primary"
>2x</button>
<button
id="btn3x"
type="button"
uxp-variant="primary"
autofocus="autofocus"
>3x</button>
</footer>
</form>
`
let response = {
scale: undefined,
format: undefined,
cancelled: false,
}
// set to PNG by default
response.format = 'PNG'
const closeOptions = {
'#cancel': { cancelled: true },
'#btn1x': {
scale: 1,
cancelled: false,
},
'#btn2x': {
scale: 2,
cancelled: false,
},
'#btn3x': {
scale: 3,
cancelled: false,
},
}
// Listening to the 'close' event is the only way I can set the dialog
// response when ENTER is pressed (otherwise it's just an empty string)
dialog.addEventListener('close', (evt) => {
dialog.close(response)
})
Object.keys(closeOptions).forEach((key) => {
// Clicking on a button will prepare the correct response value and then
// directly close the dialog
dialog.querySelector(key).addEventListener('click', () => {
Object.assign(response, closeOptions[key])
dialog.close()
})
// Focusing on a button (like when TABbing through the buttons) will prepare
// the correct response value when closing the dialog
dialog.querySelector(key).addEventListener('focus', (evt) => {
Object.assign(response, closeOptions[key])
})
})
// The only downside to the current approach is that the ESC key doesn't work
// as expected. This event listener fixes that.
dialog.addEventListener('keydown', (evt) => {
// capture if ESC key is pressed and set the appropriate response value
if (evt.keyCode === 27) {
response.cancelled = true
}
})
const selectEl = dialog.querySelector(selectId)
// <select value="…"/> does not show the value as selected. Instead, get a reference to
// the element and call setAttribute("value", …) or use the selected attribute on the
// option tags.
// - https://adobexdplatform.com/plugin-docs/known-issues.html
selectEl.addEventListener('change', (evt) => {
selectEl.setAttribute('value', evt.target.value)
Object.assign(response, { format: evt.target.value })
})
document.appendChild(dialog)
return dialog
}
function noSelectionDialog() {
const dialog = document.createElement("dialog")
dialog.innerHTML = `
<form method="dialog">
<h2>⚠️ Not so fast</h2>
<p>Don't forget to select something to export :)</p>
<footer>
<button
id="ok"
type="submit"
uxp-variant="cta"
>OK, I will select something</button>
</footer>
</form>
`
document.appendChild(dialog)
return dialog
}
async function showDialog(selection) {
if (!selection.items.length) {
const dialog = noSelectionDialog()
await dialog.showModal()
dialog.remove()
return
}
const dialog = exportDialog()
// `showModal` returns a Promise, that's why we await. If you're waiting for
// dialog responses you need to make an async function so XD knows not to
// close the document.
const response = await dialog.showModal()
if (
!response.cancelled &&
typeof response.scale === 'number' &&
typeof response.format === 'string'
) {
exportAt(selection, response.scale, response.format)
}
// If the dialog is not explicitly removed it will stay in RAM but won't be
// visible to the user.
dialog.remove()
}
module.exports = {
commands: {
"exportAtX": showDialog,
},
}