-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathmain.js
65 lines (53 loc) · 1.57 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
const commands = require('commands');
const axios = require('./lib/axios');
const { ImageFill, Color } = require("scenegraph");
const endPoint = "https://kennykrosky.com/image-data-uri?image=https://via.placeholder.com/";
const { alert, error } = require('./lib/dialogs.js');
const runImages = async (selection) => {
let types = ["Rectangle", "Ellipse", "Polygon"];
if(selection.items.length >= 1 && selection.items[0].constructor.name != "Artboard") {
for(let i = 0; i < selection.items.length; i++){
let selected = selection.items[i];
let { localBounds } = selection.items[i];
if(types.includes(selected.constructor.name)) {
let height = Math.round(localBounds.height);
let width = Math.round(localBounds.width);
let getDate = await fetchImage(height, width);
let fillImage = await new ImageFill(getDate);
selected.fill = fillImage;
}
}
} else {
alertUserNoObj();
}
}
const fetchImage = (height, width) => {
return axios(`${endPoint}${width}x${height}.png/?text=${width}x${height}`)
.then(response => {
return response.data;
})
.catch((err) => {
console.log(err);
noConnection();
})
}
const alertUserNoObj = async () => {
await error(
"Placeholder Image error",
"Please select one or more of the following: ",
"* Rectangle",
"* Ellipse",
"* Polygon"
);
}
const noConnection = async () => {
await error(
"Placeholder Image error",
"Please connect to the internet and try again ",
);
}
module.exports = {
commands: {
doImage: runImages
}
}