-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathmain.js
162 lines (147 loc) · 5.08 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
// Requiring API's
const application = require("application");
const fs = require("uxp").storage.localFileSystem;
const ImageFill = require("scenegraph").ImageFill;
const command = require("commands");
const { error } = require("./lib/dialogs.js");
// Helper function for dialog modal
/**
* Shorthand for creating Elements.
* @param {*} tag The tag name of the element.
* @param {*} [props] Optional props.
* @param {*} children Child elements or strings
*/
function h(tag, props, ...children) {
let element = document.createElement(tag);
if (props) {
if (props.nodeType || typeof props !== "object") {
children.unshift(props);
}
else {
for (let name in props) {
let value = props[name];
if (name == "style") {
Object.assign(element.style, value);
}
else {
element.setAttribute(name, value);
element[name] = value;
}
}
}
}
for (let child of children) {
element.appendChild(typeof child === "object" ? child : document.createTextNode(child));
}
return element;
}
// Iterating two times for making rendered image into selection.
for(let i=0;i<2;i++)
{
// Main Handler Function
async function mainFunction(selection)
{
// Getting temporary folder
const tempFolder = await fs.getTemporaryFolder();
// Creating temporary file into temporary folder
const file = await tempFolder.createFile('rendition.jpg', { overwrite: true });
// Creating renditions
const renditions = [{
node: selection.items[0],
outputFile: file,
// Can change the image file type
type: "jpg",
scale: 1.0,
// Can change the quality of image file
quality:60
}];
// User should select atleast one node.
if (selection.items.length > 0)
{
// Creating rendition
await application.createRenditions(renditions)
.then(results => {
console.log("Image Created");
})
.catch(error => {
console.log("File creation error");
})
// Image filling to the selection try
try{
if(file.name === "rendition.jpg")
{
let fill = new ImageFill(file);
selection.items[0].fill = fill;
console.log("Image Filled");
}
else{
showError();
}
}
catch(Error)
{
console.log("Image not compressed and filled")
showError();
}
// Try block for checking if there image entry
// If exists deletes the temp image entry
try
{
const imageentry = await tempFolder.getEntry("rendition.jpg");
if(imageentry.name == "rendition.jpg")
{
try{
await imageentry.delete();
console.log("Deleted temporary storage!");
showdialog();
}
catch(Error)
{
showError();
}
}
else
{
showError();
}
}
catch(Error)
{
showError();
}
}
else
{
showError();
}
}
// Function for showing dialog message
function showdialog()
{
let dialog;
dialog = h("dialog",
h("form", { method: "dialog", style: { width: 'auto', height: 'auto'} },
h("header",
h("img", { src: "images/sucess.png", width: 24 , height: 24 } ),
h("h1","Optimized")),
// h("footer",
h("button", { uxpVariant: "cta", onclick(e) { dialog.close() } },"OK")
)
)
// )
document.body.appendChild(dialog);
dialog.showModal();
}
// Asynchronous Function to show error
async function showError()
{
await error("Select Image",
"Please select an image file to perform compression");
}
// Exporting modules via JSON.
module.exports = {
commands: {
myPluginCommand: mainFunction
}
};
}