-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathmain.js
221 lines (194 loc) · 5.97 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
/*
* uiLogos for Adobe XD by @realvjy
* Visit https://uilogos.co/xd-plugin
* Created date: Dec 2018
* Updated date: 12 Jan 2018
* Version 0.3.0
*/
const scenegraph = require("scenegraph");
const fs = require("uxp").storage.localFileSystem;
const { ImageFill } = require("scenegraph");
const diag = require('./dialog.js');
var selectedShapes = null;
// shuffle logos/images
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex ;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
async function generateLogos(type, color) {
const pluginFolder = await fs.getPluginFolder();
const pluginEntries = await pluginFolder.getEntries();
const resourceFolder = await getFolderData(pluginEntries, 'resources');
const logosFolder = await getFolderData(resourceFolder, 'logos');
let logoTypeFolder;
if (type == 'full-logo') {
logoTypeFolder = await getFolderData(logosFolder, 'full-logo');
}
if (type == 'mark') {
logoTypeFolder = await getFolderData(logosFolder, 'mark');
}
if (type == 'flags') {
logoTypeFolder = await getFolderData(logosFolder, 'flags');
}
// Get logos array
const allLogos = await getFolderData(logoTypeFolder, color);
return allLogos;
}
// Get all logos
async function getLogos(selection, type, color) {
if (selection.items.length > 0) {
try {
await generateLogos(type, color)
.then(result => {
if (result.length > 0) {
return fillLogos(selection, result);
}
});
} catch (e) {
diag.showDialog('#alertDialog',e);
}
} else {
diag.showDialog('#infoDialog', 'Please select rectangele shapes');
}
}
// Get Rectangle and oval Shapes
async function fillLogos(selection, allLogos) {
shuffle(allLogos);
var imageCount = allLogos.length;
if (imageCount != 0 && imageCount >= selection.items.length) {
for (var i = 0; i < selection.items.length; i++) {
if (selection.items[i] instanceof scenegraph.Rectangle) {
try {
var logoObj = await getLogo(allLogos[i]);
fillSelectionWithLogo(selection.items[i], logoObj);
} catch (e) {
console.log(e);
} finally {
diag.showDialog('#alertDialog',selection.items.length+' shapes filled with logo or flag');
}
} else {
diag.showDialog('#infoDialog', 'Please select shapes');
}
}
} else {
diag.showDialog('#alertDialog', selection.items.length+' shapes! Please select upto '+allLogos.length+' Shapes');
}
}
// Fill selected shape with image/logo
function fillSelectionWithLogo(selectedPath, image) {
const imageFill = new ImageFill(image);
let newBounds = getFrameSize(selectedPath, imageFill);
selectedPath.height = newBounds.height;
selectedPath.width = newBounds.width;
selectedPath.x = newBounds.x;
selectedPath.y = newBounds.y;
selectedPath.strokeEnabled = false;
selectedPath.name = image.name.split('.').slice(0, -1).join('.');
selectedPath.fill = imageFill;
}
// Get logo for selected shape
function getLogo(selectedLogo) {
return new Promise((resolve, reject) => {
if (selectedLogo) {
try {
const logo = selectedLogo;
resolve(logo);
} catch (e) {
reject('could not load logo')
}
} else {
reject('had an error')
}
});
}
// Return Folder and Files
async function getFolderData(entries, folderName) {
var folder = entries.filter(entry => entry.isFolder && entry.name == folderName);
if (folder.length == 0) {
console.log('The folder ' + folderName + ' doesn\'t exist');
return undefined;
} else {
var files = await folder[0].getEntries();
return files;
}
}
// Frame Size for image repace
function getFrameSize(selectedPath, image){
// Default dimentions
var newX = 0;
var newY = 0;
var newWidth = 100;
var newHeight = 100;
var coordiBounds = selectedPath.boundsInParent;
// Decide the output frame dimension for reference
if (selectedPath instanceof scenegraph.Rectangle) {
newX = coordiBounds.x;
newY = coordiBounds.y;
newWidth = coordiBounds.width;
newHeight = coordiBounds.height;
}
// Decide the height and width
var ratio = image.naturalWidth/image.naturalHeight;
var newHeight = newHeight;
var newWidth = image.naturalWidth/ratio;
// Check for Portrait Logo
if(newWidth > coordiBounds.width) {
newHeight = coordiBounds.height;
newWidth = newHeight*ratio;
}
// Decide location center align with shape
var newX = coordiBounds.x + (coordiBounds.width - newWidth)/2;
var newY = coordiBounds.y + (coordiBounds.height - newHeight)/2;
var newBounds = {x: newX, y: newY, height: newHeight, width: newWidth};
return newBounds;
}
module.exports = {
commands: {
getColorLogotype: async function (selection) {
try {
await getLogos(selection, 'full-logo', 'color');
} catch (error) {
console.log(error)
}
},
getBlackLogotype: async function (selection) {
try {
await getLogos(selection, 'full-logo', 'black');
} catch (error) {
console.log(error)
}
},
getColorLogomark: async function (selection) {
try {
await getLogos(selection, 'mark', 'color');
} catch (error) {
console.log(error)
}
},
getBlackLogomark: async function (selection) {
try {
await getLogos(selection, 'mark', 'black');
} catch (error) {
console.log(error)
}
},
// Method for flag 12 Jan
getCountryFlag: async function (selection) {
try{
await getLogos(selection, 'flags', 'color');
} catch (error) {
console.log(error)
}
}
}
}