-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode.js
More file actions
49 lines (49 loc) · 2.19 KB
/
code.js
File metadata and controls
49 lines (49 loc) · 2.19 KB
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
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
function downloadImage(url) {
return __awaiter(this, void 0, void 0, function* () {
figma.showUI(__html__, { visible: false });
figma.ui.postMessage(url);
const newBytes = yield new Promise((resolve, reject) => {
figma.ui.onmessage = value => resolve(value);
});
const newPaint = { scaleMode: 'FILL', type: 'IMAGE', imageHash: figma.createImage(newBytes).hash };
return newPaint;
});
}
function addAvatar(node) {
return __awaiter(this, void 0, void 0, function* () {
const url = 'https://api.codetabs.com/v1/proxy?quest=https://thispersondoesnotexist.com/image?=' + Math.random();
const avatar = figma.createEllipse();
avatar.resize(128, 128);
const paint = yield downloadImage(url);
avatar.fills = [paint];
if (node && (node.type === "GROUP" || node.type === "FRAME")) {
node.appendChild(avatar);
}
return avatar;
});
}
function createAvatars() {
return __awaiter(this, void 0, void 0, function* () {
if (figma.currentPage.selection.length) {
yield Promise.all(figma.currentPage.selection.map(selected => addAvatar(selected)));
}
else {
const avatar = yield addAvatar(null);
figma.currentPage.appendChild(avatar);
figma.currentPage.selection = [avatar];
figma.viewport.scrollAndZoomIntoView([avatar]);
}
});
}
createAvatars()
.catch(err => console.log('err', err))
.then(() => figma.closePlugin());