-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathmain.js
218 lines (193 loc) · 7.58 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
/*
* Flutter App Icon Generator
* Currently basic UI has been integrated.
* Will be fine tuning the UI in the later releases.
*
*/
const application = require("application");
const fs = require("uxp").storage.localFileSystem;
const SELECTION_IMAGE_WIDTH = 100;
const SELECTION_IMAGE_HEIGHT = 100;
const IOS_APP_ICONS_PATH_ARR = "ios/Runner/Assets.xcassets/AppIcon.appiconset";
const iOS_RESOLUTION_MAP = {
}
const ANDROID_RES_PATH_ARR = "android/app/src/main/res";
const ANDROID_BASE_UNIT = 48;
const ANDROID_RESOLUTION_MAP = {
//48x48 is the base and 4 times = 192x192 is the scale
// with 4x4 cell the editing should happen
//TODO: this is removed as it is not supported by Flutter Project created in Android Studio
// "mipmap-ldpi": (0.75 * ANDROID_BASE_UNIT) / SELECTION_IMAGE_WIDTH,
"mipmap-mdpi": (1.0 * ANDROID_BASE_UNIT) / SELECTION_IMAGE_WIDTH,
"mipmap-hdpi": (1.5 * ANDROID_BASE_UNIT) / SELECTION_IMAGE_WIDTH,
"mipmap-xhdpi": (2.0 * ANDROID_BASE_UNIT) / SELECTION_IMAGE_WIDTH,
"mipmap-xxhdpi": (3.0 * ANDROID_BASE_UNIT) / SELECTION_IMAGE_WIDTH,
"mipmap-xxxhdpi": (4.0 * ANDROID_BASE_UNIT) / SELECTION_IMAGE_WIDTH,
}
let flutter_folder_path = null;
async function myPluginCommand(selection) {
const folder = await fs.getFolder();
flutter_folder_path = folder.nativePath;
let androidResPath;
let iOSAppIconPath;
try {
androidResPath = await folder.getEntry(ANDROID_RES_PATH_ARR);
iOSAppIconPath = await folder.getEntry(IOS_APP_ICONS_PATH_ARR);
} catch (error) {
notAFlutterRoot.showModal();
return;
}
const width = selection.items[0].width;
const height = selection.items[0].height;
if (width != SELECTION_IMAGE_WIDTH || height != SELECTION_IMAGE_HEIGHT) {
errorDialog.showModal();
return;
}
for (let resolution in ANDROID_RESOLUTION_MAP) {
const mipmapFolder = await androidResPath.getEntry(resolution);
const mipmapFile = await mipmapFolder.createFile("ic_launcher.png", { overwrite: true });
let renditionSettings = [{
node: selection.items[0],
outputFile: mipmapFile,
type: application.RenditionType.PNG,
scale: ANDROID_RESOLUTION_MAP[resolution],
}];
await application.createRenditions(renditionSettings);
}
for (let resolution in iOS_RESOLUTION_MAP) {
const appIconFileName = await iOSAppIconPath.createFile(resolution, { overwrite: true });
let renditionSettings = [{
node: selection.items[0],
outputFile: appIconFileName,
type: application.RenditionType.PNG,
scale: iOS_RESOLUTION_MAP[resolution],
}];
await application.createRenditions(renditionSettings);
}
showSuccessMessage();
//TODO: the whole method needs to be enclosed with try-catch, and proper error messaging needs to be
// shown. Until, then the errors are left to Adobe XD plugin environment to be handled.
}
module.exports = {
commands: {
myPluginCommand: myPluginCommand,
}
};
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;
}
let dialog =
h("dialog",
h("form", { method: "dialog", style: { width: 400 } },
h("h1", "Simple Form"),
h("hr"),
h("p", "This plugin will vectorize your entire project. Are you sure you'd like to continue?"),
h("label",
h("span", "Input Type Text"),
h("input", { id: "folder_input_id" })
),
h("label",
h("span", "Text Area rows=6, Bug: Should be 6 rows high."),
h("textarea", { style: { height: 100 } })
),
h("label",
h("span", "Select"),
h("select",
...["A", "B", "C", "D", "E", "F", "G"].map(name => h("option", `Option ${name}`))
)
),
h("label", { style: { flexDirection: "row", alignItems: "center" } },
h("input", { type: "checkbox" }),
h("span", "Input Type Checkbox?")
),
h("label",
h("span", "Input Type Range"),
h("input", { type: "range" })
),
h("footer",
h("button", { uxpVariant: "primary", onclick(e) { dialog.close() } }, "Cancel"),
h("button", {
uxpVariant: "cta", async onclick(e) {
const folder = await fs.getFolder();
document.getElementById("folder_input_id").value = folder.nativePath;
}
}, "Submit")
)
)
)
document.body.appendChild(dialog);
let errorDialog =
h("dialog",
h("form", { method: "dialog" },
h("label",
h("span", "Select 100x100 Sized Object"),
),
h("footer",
h("button", { uxpVariant: "primary", onclick(e) { errorDialog.close() } }, "OK"),
),
)
);
document.body.appendChild(errorDialog);
let notAFlutterRoot =
h("dialog",
h("form", { method: "dialog" },
h("label",
h("span", "Not a Flutter App Directory"),
),
h("footer",
h("button", { uxpVariant: "primary", onclick(e) { notAFlutterRoot.close() } }, "OK"),
),
)
);
document.body.appendChild(notAFlutterRoot);
function showSuccessMessage() {
let successMessage =
h("dialog",
h("form", { method: "dialog", style: { width: 500 } },
h("h4", { style: { padding: "8px" } }, "Generated Succesfully"),
h("p", `iOS: ${flutter_folder_path}/${IOS_APP_ICONS_PATH_ARR}`),
h("p", `Android: ${flutter_folder_path}/${ANDROID_RES_PATH_ARR}`),
h("footer",
h("button", { uxpVariant: "primary", onclick(e) { successMessage.close() } }, "OK"),
),
)
);
document.body.appendChild(successMessage);
successMessage.showModal();
}