-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathmain.js
420 lines (357 loc) · 17.2 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* Copyright 2018 François Le Lay <[email protected]>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const {Text, Rectangle, Color} = require("scenegraph") ;
const commands = require("commands") ;
const h = require("./h") ;
const fs = require("uxp").storage.localFileSystem ;
const { alert, error } = require("./lib/dialogs.js");
let dialog,
pluginDataFolder,
settingsFile,
settings,
startTimeValue,
endTimeValue,
colorValue,
fontSizeValue,
fontAspectRatioValue,
fontFamilyValue ;
async function doSettings(){
pluginDataFolder = await fs.getDataFolder() ;
try {
settingsFile = await pluginDataFolder.getEntry("timer-settings.txt") ;
settings = await settingsFile.read() ;
} catch( e ) {
console.log(e) ;
}
if(settings === undefined) {
[startTimeValue, endTimeValue, colorValue, fontSizeValue, fontAspectRatioValue, fontFamilyValue] =
["23:17", "07:44", "#000000", "30", "0.66", "Arial"] ;
} else {
var settingsObj = JSON.parse(settings) ;
[startTimeValue, endTimeValue, colorValue, fontSizeValue, fontAspectRatioValue, fontFamilyValue] =
[settingsObj.startTime, settingsObj.endTime, settingsObj.color, settingsObj.fontSize, settingsObj.fontAspectRatio, settingsObj.fontFamily] ;
}
}
async function inconsistentTimestamps(ts, te) {
await error("Inconsistent Timestamps ",
ts + " < " + te,
"This plugin allows you to create a timelapse of a countdown between two timestamps. In order to function correctly, it requires the start time to be greater that the end time.");
}
async function incorrectArtboardSelection() {
await error("Incorrect number of artboards selected",
"Please select exactly 2 artboards in Design mode before executing this plugin.",
"Steps you can take:",
"* Make sure you have 2 artboards or more in your document. If not, create blank ones.",
"* Left-click on the name of a first artboard.",
"* While holding the SHIFT key, left-click on the \nname of a second artboard.",
"* Launch this plugin from the Plugins menu.");
}
async function incorrectTimeFormat(t){
await error("Wrong time format " + t,
"Accepted format XX:YY where:",
"* XX between 00 and 99",
"* YY between 00 and 59",
"* Make sure to use a column separator between XX and YY components");
}
async function getDialog(selection) {
if (dialog != null) {
dialog = null ;
}
if (dialog == null) {
if( selection.items.length < 2 ){
await incorrectArtboardSelection() ;
} else {
async function onsubmit() {
let values = {
startTime: startTime.value,
endTime: endTime.value,
color: color.value,
fontSize: fontSize.value,
fontAspectRatio: fontAspectRatio.value,
fontFamily: fontFamily.value
}
const JSONValues = JSON.stringify(values) ;
let newFile = await pluginDataFolder.createEntry("timer-settings.txt", {overwrite: true}) ;
await newFile.write(JSONValues) ;
let timeFormat = /^\d\d:[0-5]\d$/ ;
let ts_a = timeFormat.exec(startTime.value) ;
let ts_b = timeFormat.exec(endTime.value) ;
if( ts_a == null ) {
dialog.close() ;
await incorrectTimeFormat(startTime.value) ;
} else if( ts_b == null ) {
dialog.close();
await incorrectTimeFormat(endTime.value) ;
} else if( endTime.value > startTime.value ) {
dialog.close();
await inconsistentTimestamps(startTime.value, endTime.value) ;
} else {
await createMaskedTextStrips( selection,
startTime.value,
endTime.value,
color.value,
Number(fontSize.value),
Number(fontAspectRatio.value),
fontFamily.value ) ;
// dialog is automatically closed after submit unless you call e.preventDefault()
dialog.close();
}
}
let startTime, endTime, color, fontSize, fontAspectRatio, fontFamily ;
dialog =
h("dialog",
h("form", { method:"dialog", style: { width: 320 }, onsubmit },
h("div", { class: "row" },
h("label",
h("span", "Start Time (mm:ss)"),
startTime = h("input", { value: startTimeValue })
),
h("label",
h("span", "End Time (mm:ss)"),
endTime = h("input", { value: endTimeValue })
)
),
h("div", { class: "row" },
h("label",
h("span", "Font Color hex #"),
color = h("input", { value: colorValue })
)
),
h("div", { class: "row" },
h("label",
h("span", "Font size (px)"),
fontSize = h("input", { value: fontSizeValue })
)
),
h("div", { class: "row" },
h("label",
h("span", "Horizontal Spacing Ratio (> 0.50)"),
fontAspectRatio = h("input", { value: fontAspectRatioValue })
)
),
h("div", { class: "row" },
h("label",
h("span", "Font family"),
fontFamily = h("input", { value: fontFamilyValue })
)
),
h("div", { class: "row" },
h("label",
h("span", "Once your timer elements are added to your artboards, all you have to do " +
"is to switch to Prototype mode and create a few seconds long (e.g. 5s), Ease in-out," +
" auto-animate transition triggered by the action of your choice. Then press play and enjoy the timelapse.")
)
),
h("footer",
h("button", { uxpVariant: "primary", onclick(e) { dialog.close("Cancelled") } }, "Cancel"),
h("button", { uxpVariant: "cta", type:"submit", onclick(e) { onsubmit(); e.preventDefault() } }, "Create Timer Elements")
)
)
);
return dialog;
}
}
}
async function createMaskedTextStrips( selection,
startTime,
endTime,
color,
fontSize,
fontAspectRatio,
fontFamily) {
const x = 20 ;
const y = 50 ;
const start = startTime ;
const end = endTime ;
const startMinuteTens = start[0] ;
const startMinuteUnits = start[1] ;
const startSecondTens = start[3] ;
const startSecondUnits = start[4] ;
const startMinute = Number( startMinuteTens + startMinuteUnits ) ;
const startSecond = Number( startSecondTens + startSecondUnits) ;
const endMinuteTens = end[0] ;
const endMinuteUnits = end[1] ;
const endSecondTens = end[3] ;
const endSecondUnits = end[4] ;
const endMinute = Number( endMinuteTens + endMinuteUnits ) ;
const endSecond = Number( endSecondTens + endSecondUnits ) ;
const minuteSpan = startMinute - endMinute ;
const minuteTensSpan = Number(startMinuteTens) - Number(endMinuteTens) ;
const startMinuteTensStrip = new Text() ;
const startMinuteUnitsStrip = new Text() ;
const endMinuteTensStrip = new Text() ;
const endMinuteUnitsStrip = new Text() ;
const columnStart = new Text() ;
const columnEnd = new Text() ;
const startSecondTensStrip = new Text() ;
const startSecondUnitsStrip = new Text() ;
const endSecondTensStrip = new Text() ;
const endSecondUnitsStrip = new Text() ;
startMinuteTensStrip.fontFamily = fontFamily ;
startMinuteUnitsStrip.fontFamily = fontFamily ;
endMinuteTensStrip.fontFamily = fontFamily ;
endMinuteUnitsStrip.fontFamily = fontFamily ;
columnStart.fontFamily = fontFamily ;
columnEnd.fontFamily = fontFamily ;
startSecondTensStrip.fontFamily = fontFamily ;
endSecondTensStrip.fontFamily = fontFamily ;
startSecondUnitsStrip.fontFamily = fontFamily ;
endSecondUnitsStrip.fontFamily = fontFamily ;
// Minutes tens
startMinuteTensStrip.text = new String("") ;
var j = Number(endMinuteTens) ;
while( j < Number(startMinuteTens)) {
startMinuteTensStrip.text += new String(j) ;
startMinuteTensStrip.text += "\n" ;
j += 1 ;
}
startMinuteTensStrip.text += startMinuteTens ;
endMinuteTensStrip.text = startMinuteTensStrip.text ;
// Minutes units
startMinuteUnitsStrip.text = new String("") ;
var j = endMinute ;
while( j < startMinute ) {
startMinuteUnitsStrip.text += new String(j%10) ;
startMinuteUnitsStrip.text += "\n" ;
j += 1 ;
}
startMinuteUnitsStrip.text += startMinuteUnits ;
endMinuteUnitsStrip.text = startMinuteUnitsStrip.text ;
// Seconds tens
startSecondTensStrip.text = new String("") ;
var cycles = 6 - Number(endSecondTens) + Number(startSecondTens) + (minuteSpan-1)*6 ;
for( var j=0; j< cycles; j++) {
startSecondTensStrip.text += new String( (Number(endSecondTens)+j) % 6 ) ;
startSecondTensStrip.text += "\n" ;
}
startSecondTensStrip.text += startSecondTens ;
endSecondTensStrip.text = startSecondTensStrip.text ;
// Seconds units
startSecondUnitsStrip.text = new String("") ;
var fastCycles = 10 - Number(endSecondUnits) + Number(startSecondUnits) + (cycles-1)*10 ;
for( var j=0; j< fastCycles; j++) {
startSecondUnitsStrip.text += new String( (Number(endSecondUnits)+j) % 10 ) ;
startSecondUnitsStrip.text += "\n" ;
}
startSecondUnitsStrip.text += startSecondUnits ;
endSecondUnitsStrip.text = startSecondUnitsStrip.text ;
// column
columnStart.text = ":" ;
columnEnd.text = ":" ;
// set more text properties
let lineSpacing = Math.round(fontSize * 1.1) ;
startMinuteTensStrip.fill = new Color(color) ;
startMinuteTensStrip.fontSize = fontSize ;
startMinuteTensStrip.lineSpacing = lineSpacing ;
startMinuteUnitsStrip.fill = new Color(color) ;
startMinuteUnitsStrip.fontSize = fontSize ;
startMinuteUnitsStrip.lineSpacing = lineSpacing ;
endMinuteTensStrip.fill = new Color(color) ;
endMinuteTensStrip.fontSize = fontSize ;
endMinuteTensStrip.lineSpacing = lineSpacing ;
endMinuteUnitsStrip.fill = new Color(color) ;
endMinuteUnitsStrip.fontSize = fontSize ;
endMinuteUnitsStrip.lineSpacing = lineSpacing ;
columnStart.fill = new Color(color) ;
columnStart.fontSize = fontSize ;
columnStart.lineSpacing = lineSpacing ;
columnEnd.fill = new Color(color) ;
columnEnd.fontSize = fontSize ;
columnEnd.lineSpacing = lineSpacing ;
startSecondTensStrip.fill = new Color(color) ;
startSecondTensStrip.fontSize = fontSize ;
startSecondTensStrip.lineSpacing = lineSpacing ;
endSecondTensStrip.fill = new Color(color) ;
endSecondTensStrip.fontSize = fontSize ;
endSecondTensStrip.lineSpacing = lineSpacing ;
startSecondUnitsStrip.fill = new Color(color) ;
startSecondUnitsStrip.fontSize = fontSize ;
startSecondUnitsStrip.lineSpacing = lineSpacing ;
endSecondUnitsStrip.fill = new Color(color) ;
endSecondUnitsStrip.fontSize = fontSize ;
endSecondUnitsStrip.lineSpacing = lineSpacing ;
let hspace = Math.round(fontAspectRatio*fontSize) ;
// add to artboards
selection.items[0].addChild(startMinuteTensStrip) ;
selection.items[0].addChild(startMinuteUnitsStrip) ;
selection.items[0].addChild(columnStart) ;
selection.items[0].addChild(startSecondTensStrip) ;
selection.items[0].addChild(startSecondUnitsStrip) ;
selection.items[1].addChild(endMinuteTensStrip) ;
selection.items[1].addChild(endMinuteUnitsStrip) ;
selection.items[1].addChild(columnEnd) ;
selection.items[1].addChild(endSecondTensStrip) ;
selection.items[1].addChild(endSecondUnitsStrip) ;
// reposition start time strips
startMinuteTensStrip.height = minuteTensSpan * lineSpacing - 1 ;
startMinuteTensStrip.moveInParentCoordinates(x, lineSpacing - startMinuteTensStrip.height);
startMinuteUnitsStrip.height = minuteSpan * lineSpacing - 1 ;
startMinuteUnitsStrip.moveInParentCoordinates(x+hspace, lineSpacing - startMinuteUnitsStrip.height) ;
columnStart.moveInParentCoordinates(x+hspace*2, lineSpacing) ;
startSecondTensStrip.height = cycles * lineSpacing - 1 ;
startSecondTensStrip.moveInParentCoordinates(x+hspace*2.5, lineSpacing - startSecondTensStrip.height) ;
startSecondUnitsStrip.height = fastCycles * lineSpacing - 1 ;
startSecondUnitsStrip.moveInParentCoordinates(x+hspace*3.5, lineSpacing - startSecondUnitsStrip.height) ;
// reposition end time strips
endMinuteTensStrip.moveInParentCoordinates(x, lineSpacing) ;
endMinuteUnitsStrip.moveInParentCoordinates(x+hspace, lineSpacing) ;
columnEnd.moveInParentCoordinates(x+hspace*2, lineSpacing) ;
endSecondTensStrip.moveInParentCoordinates(x+hspace*2.5, lineSpacing) ;
endSecondUnitsStrip.moveInParentCoordinates(x+hspace*3.5, lineSpacing) ;
// add and rename masks
// IMPORTANT: we do this because all groups should be given the same name
// on all artboards or auto-animate will fail and perform a very disappointing
// dissolve transition instead.
const rectStart = new Rectangle() ;
rectStart.width = hspace*4.5 ;
rectStart.height = lineSpacing ;
rectStart.name = "Mask" ;
selection.items[0].addChild(rectStart) ;
rectStart.moveInParentCoordinates(x,2*(lineSpacing-fontSize)) ;
const rectEnd = new Rectangle() ;
rectEnd.width = hspace*4.5 ;
rectEnd.height = lineSpacing ;
rectEnd.name = "Mask" ;
selection.items[1].addChild(rectEnd) ;
rectEnd.moveInParentCoordinates(x,2*(lineSpacing-fontSize)) ;
selection.items = [ startMinuteTensStrip,
startMinuteUnitsStrip,
columnStart,
startSecondTensStrip,
startSecondUnitsStrip,
rectStart ] ;
commands.createMaskGroup() ;
let startMaskedGroup = selection.items[0] ;
startMaskedGroup.name = "CounterMask" ;
selection.items = [ endMinuteTensStrip,
endMinuteUnitsStrip,
columnEnd,
endSecondTensStrip,
endSecondUnitsStrip,
rectEnd ] ;
commands.createMaskGroup() ;
let endMaskedGroup = selection.items[0] ;
endMaskedGroup.name = "CounterMask" ;
}
module.exports = {
commands: {
makeCountdownTimer : async function (selection, documentRoot) {
await doSettings() ;
dialog = await getDialog(selection) ;
await document.body.appendChild(dialog).showModal() ;
}
}
};