-
-
Notifications
You must be signed in to change notification settings - Fork 154
/
main.js
790 lines (686 loc) · 24.7 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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
// Adobe Modules required for plugin
const application = require("application");
const uxp = require("uxp");
const scenegraph = require("scenegraph");
const ImageFill = require("scenegraph").ImageFill;
const Color = require("scenegraph").Color;
const commands = require("commands");
// Utilities
const base64Convert = require("./utilities/base64").base64Convert;
const randomString = require("./utilities/base64").randomString;
const extractAllText = require("./utilities/extractAllText").extractAllText;
const xhrBinary = require("./utilities/xhrBinary").xhrBinary;
const base64ArrayBuffer = require("./utilities/arrayBuffer").base64ArrayBuffer;
// Export module with commandId identifier for plugin
module.exports = {
commands: {
menuCommand: angleModal,
blank: blankAngle
}
};
/* ---------------------------------------------
Global Variables
--------------------------------------------- */
// User selected shapes
let userSelection = {};
// Dropdown selections
let pixelDensity = "dpr_auto";
let imageQuality = "q_auto";
let orientationFill = "a_auto";
/* ---------------------------------------------
Accessory Angle Functionality
--------------------------------------------- */
async function blankAngle(selection) {
let selectedItem = selection.items[0];
// Check is selected item is a Group
// Throw error
if (
selectedItem instanceof scenegraph.SymbolInstance ||
selectedItem instanceof scenegraph.Group ||
selectedItem instanceof scenegraph.Artboard
) {
let groupError = `Oops! Angle does not currently support ${
selectedItem instanceof scenegraph.SymbolInstance
? "Symbol"
: selectedItem instanceof scenegraph.Group
? "Group"
: "Artboard"
} selection. Please select your shape directly.`;
const groupErrorDialog = createErrorDialog(groupError);
try {
const ret = await groupErrorDialog.showModal();
// Display the return value of the modal
if (ret) {
console.log(`${ret}`);
}
} catch (err) {
// Esc pressed or error
console.log(`Error Dialog dismissed with ESC or error: ${err}`);
}
}
if (selectedItem === undefined) {
let lockError =
"Oops! Your selection is locked or undefined. Please unlock to apply without error.";
const lockErrorDialog = createErrorDialog(lockError);
try {
const ret = await lockErrorDialog.showModal();
// Display the return value of the modal
if (ret) {
console.log(`${ret}`);
}
} catch (err) {
// Esc pressed or error
console.log(`Error Dialog dismissed with ESC or error: ${err}`);
}
}
let selectedItemParent = selection.items[0].parent;
let isMaskGroup = false;
// Check if selected parent is a Masked Group
// Ungroup
if (
selectedItemParent instanceof scenegraph.Group &&
selectedItemParent.mask
) {
isMaskGroup = true;
commands.ungroup();
}
selectedItem.fill = new Color("white");
// Group back with Mask if Mask Group
if (isMaskGroup === true) {
commands.createMaskGroup();
}
return;
}
function createErrorDialog(error) {
document.body.innerHTML = `
<style>
.container {
width: 500px;
}
.logo {
background-image: url("./assets/logo-2.png");
background-repeat: no-repeat;
background-size: cover;
width: 50px;
height: 50px;
position: relative;
margin: 8px auto;
}
.box {
margin: 8px;
}
.error {
text-align: left;
font-size: 14px;
font-weight: 500;
margin-bottom: 12px;
}
#exit {
margin: 0 auto;
}
</style>
<dialog id="errorDialog">
<form class="container">
<div class="logo"></div>
<div class="box">
<div class="error">${error}</div>
</div>
<footer>
<button id="exit">Exit</button>
</footer>
</form>
</dialog>
`;
// Dialog to be used for closing
const errorDialog = document.getElementById("errorDialog");
// Exit > Close Dialog
document.getElementById("exit").addEventListener("click", () => {
errorDialog.close("exited error modal");
});
return errorDialog;
}
/* ---------------------------------------------
STEPS:
1. Show Modal
2. Applying Base64 image on a selected shape
--------------------------------------------- */
/*--- ENTRY POINT OF PLUGIN ---*/
// Step 1. Show modal to be able to select Artboard, Pixel Density, and Quality
// Selection and root node of scenegraph passed to dialog
async function angleModal(selection, rootnode) {
// Save selection in entry point of app for later use
userSelection = selection.items;
console.log(`\n/*--- User Selected Items ---*/ \n${userSelection}\n`);
const dialog = createDialog(selection, rootnode);
try {
const ret = await dialog.showModal();
// Display the return value of the modal
if (ret) {
console.log(`${ret}`);
}
} catch (err) {
// Esc pressed or error
console.log(`Dialog dismissed with ESC or error: ${err}`);
}
}
// Step 2: Get Base64 image and apply it on a selected shape
async function applyAngleFunction(selectedArtboard) {
// Check if Group or Symbol
// Throw errors for now, support this later!
for (let i = 0; i < userSelection.length; i++) {
// Check is selected item is a Group
// Throw error
if (userSelection[i] instanceof scenegraph.SymbolInstance) {
throw new Error(
"Angle does not currently support Symbol selection, please select your shape directly."
);
}
// Check is selected item is a Group
// Throw error
if (userSelection[i] instanceof scenegraph.Group) {
throw new Error(
"Angle does not currently support Group selection, please select your shape directly."
);
}
}
// Get Base64 image from temporary storage for user selected Artboard
// Upload image to Cloudinary API for later retrieval
const imageData = await getBase64(selectedArtboard);
const imageName = await uploadFile(imageData);
const promisesToAwait = [];
for (let i = 0; i < userSelection.length; i++) {
promisesToAwait.push(angleFill(imageName, i));
}
const responses = await Promise.all(promisesToAwait);
return responses;
}
/* ---------------------------------------------
Necessary Plugin Functions
--------------------------------------------- */
async function angleFill(imageName, itemNumber) {
// Initializing user selected item+parent
let selectedItem = userSelection[itemNumber];
let selectedItemParent = userSelection[itemNumber].parent;
let isMaskGroup = false;
// Check if selected parent is a Masked Group
// Ungroup
if (
selectedItemParent instanceof scenegraph.Group &&
selectedItemParent.mask
) {
isMaskGroup = true;
commands.ungroup();
}
// Apply Bitmap with distortion
// Calculation of coordinates from Path for image distortion
const pathBounds = selectedItem.pathData;
const pathBoundsArray = pathBounds.split(" ");
let pathPoints = {
topLeftX: pathBoundsArray[1],
topLeftY: pathBoundsArray[2],
topRightX: pathBoundsArray[4],
topRightY: pathBoundsArray[5],
bottomRightX: pathBoundsArray[7],
bottomRightY: pathBoundsArray[8],
bottomLeftX: pathBoundsArray[10],
bottomLeftY: pathBoundsArray[11]
};
if (orientationFill === "rotate-left") {
pathPoints = {
topLeftX: pathBoundsArray[10],
topLeftY: pathBoundsArray[11],
topRightX: pathBoundsArray[1],
topRightY: pathBoundsArray[2],
bottomRightX: pathBoundsArray[4],
bottomRightY: pathBoundsArray[5],
bottomLeftX: pathBoundsArray[7],
bottomLeftY: pathBoundsArray[8]
};
} else if (orientationFill === "rotate-right") {
pathPoints = {
topLeftX: pathBoundsArray[4],
topLeftY: pathBoundsArray[5],
topRightX: pathBoundsArray[7],
topRightY: pathBoundsArray[8],
bottomRightX: pathBoundsArray[10],
bottomRightY: pathBoundsArray[11],
bottomLeftX: pathBoundsArray[1],
bottomLeftY: pathBoundsArray[2]
};
} else if (orientationFill === "flip") {
pathPoints = {
topLeftX: pathBoundsArray[7],
topLeftY: pathBoundsArray[8],
topRightX: pathBoundsArray[10],
topRightY: pathBoundsArray[11],
bottomRightX: pathBoundsArray[1],
bottomRightY: pathBoundsArray[2],
bottomLeftX: pathBoundsArray[4],
bottomLeftY: pathBoundsArray[5]
};
}
// Image URL from Cloudinary API with width, height, path coordinates, and Image Name
const imageURL =
"https://res.cloudinary.com/design-code/image/upload/e_distort:" +
pathPoints.topLeftX +
":" +
pathPoints.topLeftY +
":" +
pathPoints.topRightX +
":" +
pathPoints.topRightY +
":" +
pathPoints.bottomRightX +
":" +
pathPoints.bottomRightY +
":" +
pathPoints.bottomLeftX +
":" +
pathPoints.bottomLeftY +
"/" +
"e_trim" +
"/" +
pixelDensity +
"/" +
imageQuality +
"/" +
imageName +
".png";
console.log(`Image URL: ${imageURL}`);
// Receive Cloudinary API response with selected coordinates if skewed
// Perform image distortion with coordinates from path
// Apply distorted Bitmap
try {
const photoObj = await xhrBinary(imageURL);
const photoObjBase64 = await base64ArrayBuffer(photoObj);
applyBitmap(selectedItem, photoObjBase64);
} catch (error) {
console.log(`Error: ${error.message}`);
throw new Error(error);
}
// Group back with Mask if Mask Group
if (isMaskGroup === true) {
commands.createMaskGroup();
}
}
// Function to Apply the Bitmap, depending on the user selected Item
function applyBitmap(selectedItem, base64) {
const imageFill = new ImageFill(`data:image/png;base64,${base64}`);
selectedItem.fill = imageFill;
}
// Function to upload file to Cloudinary
async function uploadFile(file) {
console.log("starting upload to cloudinary...");
const uploadUrl = "https://api.cloudinary.com/v1_1/design-code/image/upload";
let imageName = Math.floor(Math.random() * Math.floor(100000000));
console.log(`Uploaded Image Name: ${imageName}`);
var imageNamePath = fetch(uploadUrl, {
method: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8",
"X-Requested-With": "XMLHttpRequest"
},
body: JSON.stringify({
upload_preset: "anglify",
file: "data:image/png;base64," + file,
public_id: "" + imageName
})
})
.then(function(response) {
console.log(`Upload Response: ${JSON.stringify(response)}`);
return imageName;
})
.catch(error => console.log(`Upload error: ${error}`));
return imageNamePath;
}
// Function to export PNG rendition to temporary storage and convert to Base64
// If selected shape is a Path, distort image to fit 4 coordinates of that shape
async function getBase64(selectedArtboard) {
const imageBase64FromNode = async function(node) {
const tempFolder = await uxp.storage.localFileSystem.getTemporaryFolder();
const shotFile = await tempFolder.createEntry(`${randomString()}.png`, {
overwrite: true
});
return new Promise(function(resolve, reject) {
application
.createRenditions([
{
node: node,
outputFile: shotFile,
type: application.RenditionType.PNG,
scale: 2
}
])
.then(async function(files) {
const buffer = await files[0].outputFile.read({
format: uxp.storage.formats.binary
});
let bytes = new Uint8Array(buffer);
resolve(base64Convert(bytes));
})
.catch(function(error) {
reject(`Error converting to base64: ${error}`);
});
});
};
return imageBase64FromNode(selectedArtboard).then(base64 => {
return base64;
});
}
// Function to create modal with input fields to select Artboard, Pixel Density, and Quality
const $ = sel => document.querySelector(sel);
// Function to create Modal dialog
function createDialog(selection, rootnode) {
document.body.innerHTML = `
<style>
.container {
width: 500px;
}
.logo {
background-image: url("./assets/logo-2.png");
background-repeat: no-repeat;
background-size: cover;
width: 50px;
height: 50px;
position: relative;
margin: 8px auto;
}
.title {
text-align: center;
font-size: 28px;
font-weight: 700;
color: black;
margin: 8px;
}
.box {
margin: 8px;
}
.header {
text-align: center;
font-size: 14px;
margin-bottom: 24px;
}
.table {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: space-around;
margin-bottom: 24px;
}
.artboard {
width: 150px;
}
.label {
margin-left: 4px;
}
#artboard-select {
min-width: 150px;
}
#pixel-select {
max-width: 75px;
}
#quality-select {
max-width: 75px;
}
#orientation-select {
max-width: 100px;
}
.items-selected {
margin-top: 12px;
margin-bottom: 8px;
font-size: 12px;
font-weight: bold;
text-align: center;
}
#loader {
display: none;
text-align: center;
font-size: 14px;
font-weight: bold;
position: relative;
margin: 0 auto;
margin-top: 50px;
margin-bottom: 8px;
}
.loader {
background-image: url("./assets/loading.gif");
background-repeat: no-repeat;
background-size: cover;
width: 65px;
height: 65px;
position: absolute;
top: 36px;
left: 6px;
}
#error {
display: none;
color: #E1467C;
text-align: left;
font-size: 12px;
font-weight: 500;
position: relative;
margin: 0 auto;
margin-top: 12px;
margin-bottom: 8px;
width: 90%;
}
.footer {
margin-top: 8px;
display: flex;
flex-direction: row;
justify-content: center;
}
.tooltip {
display: flex;
flex-direction: row;
justify-content: center;
margin-top: 16px;
margin-bottom: -12px;
}
</style>
<dialog id="dialog">
<form id="container-form" class="container">
<a href="https://designcode.io/angle-xd/"><div class="logo"></div></a>
<h1 class="title">Apply to Mockup</h1>
<div class="box">
<div class="header">Choose an Artboard to apply onto your selected shape(s):</div>
<div class="table">
<div class="artboard">
<span class="label" id="artboard-span">Artboard</span>
<select id="artboard-select">
</select>
</div>
<div>
<span class="label" id="pixel-span">Pixel Density</span>
<select id="pixel-select">
<option value="1">1x</option>
<option value="2">2x</option>
<option value="3">3x</option>
<option value="4">4x</option>
</select>
</div>
<div>
<span class="label" id="quality-span">Quality</span>
<select id="quality-select">
<option value="Best">Best</option>
<option value="Better">Better</option>
<option value="Good">Good</option>
<option value="Average">Average</option>
</select>
</div>
<div>
<span class="label" id="orientation-span">Orientation</span>
<select id="orientation-select">
<option value="Default">Default</option>
<option value="Rotate-Left">Rotate Left</option>
<option value="Rotate-Right">Rotate Right</option>
<option value="Flip">Flip</option>
</select>
</div>
</div>
<p class="items-selected" id="items-selected"> ${
selection.items.length === 0
? "No items selected. Select some shapes to apply Angle!"
: selection.items.length === 1
? "1 item selected!"
: selection.items.length + " items selected!"
} </p>
<div id="loader">
<div class="loader"></div>
</div>
<div id="error"></div>
</div>
<div class="footer" id="footer">
<button uxp-variant="primary" id="cancel">Cancel</button>
<button uxp-variant="cta" type="submit" id="apply">Apply</button>
</div>
<div class="tooltip" id="tooltip">
<a href="https://designcode.io/angle-xd-instructions/"><button id="instructions" uxp-quiet="true">Instructions</button></a>
<a href="https://designcode.io/angle-xd/"><button id="mockups" uxp-quiet="true">Buy Mockups</button></a>
<a href="https://designcode.io/privacy/"><button id="privacy-policy" uxp-quiet="true">Privacy Policy</button></a>
</div>
</form>
</dialog>
`;
// Setting the options for Artboard select
let artboardSelectValue = "";
rootnode.children.forEach((node, index) => {
if (node instanceof scenegraph.Artboard) {
const artboardObj = node.toString();
const artboardName = extractAllText(artboardObj).toString();
const select = document.getElementById("artboard-select");
let option = document.createElement("option");
option.value = artboardName;
option.textContent = artboardName;
option.value = artboardName;
if (index === 0) {
artboardSelectValue = option.value;
}
select.appendChild(option);
}
});
// Setting default select values for all select fields
document.getElementById("artboard-select").value = artboardSelectValue;
document.getElementById("pixel-select").value = "3";
document.getElementById("quality-select").value = "Best";
document.getElementById("orientation-select").value = "Default";
// Dialog to be used for closing
const dialog = document.getElementById("dialog");
// Cancel > Close Dialog
document.getElementById("cancel").addEventListener("click", () => {
dialog.close("cancelled");
});
// If no items selected
// Disable everything and prompt user to start over
if (selection.items.length <= 0) {
document.getElementById("apply").style.display = "none";
document.getElementById("cancel").style.margin = "0 auto";
document.getElementById("cancel").innerHTML = "Close and try again";
document.getElementById("artboard-select").disabled = true;
document.getElementById("pixel-select").disabled = true;
document.getElementById("orientation-select").disabled = true;
document.getElementById("quality-select").disabled = true;
}
function triggerApplyButton() {
// Set pixel density from the select
let pixelSelect = document.getElementById("pixel-select");
let pixelSelectedOption =
pixelSelect.options[pixelSelect.selectedIndex].textContent;
console.log("optionSelected: (final) " + pixelSelectedOption);
let pNumber = "3.0";
if (pixelSelectedOption === "4x") {
pNumber = "4.0";
} else if (pixelSelectedOption === "3x") {
pNumber = "3.0";
} else if (pixelSelectedOption === "2x") {
pNumber = "2.0";
} else if (pixelSelectedOption === "1x") {
pNumber = "1.0";
}
pixelDensity = `dpr_${pNumber}`;
// Set image quality from the select
let qualitySelect = document.getElementById("quality-select");
let qualitySelectedOption =
qualitySelect.options[qualitySelect.selectedIndex].textContent;
console.log("optionSelected: (final) " + qualitySelectedOption);
let qValue = "best";
if (qualitySelectedOption === "Best") {
qValue = "best";
} else if (qualitySelectedOption === "Better") {
qValue = "good";
} else if (qualitySelectedOption === "Good") {
qValue = "eco";
} else if (qualitySelectedOption === "Average") {
qValue = "low";
}
imageQuality = `q_auto:${qValue}`;
// Set image quality from the select
let orientationSelect = document.getElementById("orientation-select");
let orientationSelectOption =
orientationSelect.options[orientationSelect.selectedIndex].textContent;
console.log("optionSelected: (final) " + orientationSelectOption);
let oValue = "auto";
if (orientationSelectOption === "Default") {
oValue = "auto";
} else if (orientationSelectOption === "Rotate Left") {
oValue = "rotate-left";
} else if (orientationSelectOption === "Rotate Right") {
oValue = "rotate-right";
} else if (orientationSelectOption === "Flip") {
oValue = "flip";
}
orientationFill = oValue;
// Displaying working loader and disable buttons
const loaderText = document.createTextNode("Working...");
if (selection.items.length > 0) {
document.getElementById("apply").style.display = "none";
document.getElementById("cancel").style.display = "none";
document.getElementById("instructions").style.display = "none";
document.getElementById("privacy-policy").style.display = "none";
document.getElementById("mockups").style.display = "none";
document.getElementById("items-selected").style.display = "none";
document.getElementById("artboard-select").disabled = true;
document.getElementById("pixel-select").disabled = true;
document.getElementById("quality-select").disabled = true;
document.getElementById("orientation-select").disabled = true;
document.getElementById("loader").style.display = "block";
document.getElementById("loader").appendChild(loaderText);
// Matching the user selected artboard from dropdown respective artboard that on canvas
const select = document.getElementById("artboard-select");
const selectedOption = select.options[select.selectedIndex]
? select.options[select.selectedIndex].textContent
: "";
let selectedArtboard = {};
rootnode.children.forEach(node => {
const nodeObj = node.toString();
const nodeName = extractAllText(nodeObj).toString();
if (nodeName === selectedOption) {
selectedArtboard = node;
}
});
// Apply Angle Function > close dialog
applyAngleFunction(selectedArtboard)
.then(() => {
dialog.close("Angle applied!");
})
.catch(async error => {
const errorText = document.createTextNode(error);
document.getElementById("loader").style.display = "none";
document.getElementById("error").style.display = "block";
document.getElementById("error").appendChild(errorText);
document.getElementById("instructions").style.display = "block";
document.getElementById("instructions").style.marginTop = "32px";
document.getElementById("cancel").disabled = false;
document.getElementById("cancel").style.display = "block";
document.getElementById("cancel").style.margin = "0 auto";
document.getElementById("cancel").innerHTML = "Close and try again";
});
}
}
// Apply Angle Button Actions
document.getElementById("container-form").onsubmit = event => {
event.preventDefault();
triggerApplyButton();
};
return dialog;
}