This repository was archived by the owner on Jul 4, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcontroller.ts
More file actions
195 lines (159 loc) · 5.47 KB
/
Copy pathcontroller.ts
File metadata and controls
195 lines (159 loc) · 5.47 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
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
import {GenderType} from '../api/GenderType';
import {setCharacters} from '@figma-plugin/helpers';
import {ItemType} from '../api/ItemType';
figma.showUI(__html__, {visible: false});
// Receives messages from index.tsx
figma.ui.onmessage = (msg) => {
if (msg.type === 'launch-plugin') {
launchPlugin();
}
if (msg.type === 'fill-and-close') {
fillAndClose(msg.items, msg.idsShapes, msg.idsText, msg.images);
}
if (msg.type === 'failed') {
closePlugin(`⛔️ TinyFaces can't be contacted. Check your internet...`);
}
if (msg.type === 'close') {
closePlugin();
}
};
// Receives messages from manifest.json
function launchPlugin() {
switch (figma.command) {
case 'fill-ai':
fetchAndFillAI();
break;
case 'fill-male-ai':
fetchAndFillAI(GenderType.Male);
break;
case 'fill-female-ai':
fetchAndFillAI(GenderType.Female);
break;
case 'fill-non-binary-ai':
fetchAndFillAI();
break;
case 'fill-high-quality':
fetchAndFill(8);
break;
case 'fill-low-quality':
fetchAndFill(0);
break;
case 'fill-male':
fetchAndFill(0, GenderType.Male);
break;
case 'fill-female':
fetchAndFill(0, GenderType.Female);
break;
case 'fill-non-binary':
fetchAndFill(0);
break;
case 'license':
openUrl('https://tinyfac.es/license/non-commercial');
break;
case 'license-ai':
openUrl('https://tinyfac.es/dashboard');
break;
default:
}
}
function fetchAndFillAI(gender?: GenderType) {
const idsShapes = figma.currentPage.selection
.filter(
(node) =>
node.type === 'FRAME' ||
node.type === 'ELLIPSE' ||
node.type === 'POLYGON' ||
node.type === 'RECTANGLE' ||
node.type === 'STAR' ||
node.type === 'TEXT' ||
node.type === 'VECTOR'
)
.map((node) => node.id);
const idsText = figma.currentPage.selection.filter((node) => node.type === 'TEXT').map((node) => node.id);
if (idsShapes.length > 50 || idsText.length > 50) {
closePlugin(`🚨 You can't select more than 50 layers at a time right now.`);
}
if (idsShapes.length === 0 && idsText.length === 0) {
closePlugin('🚨 Select at least one or more Frame, Rectangle, Text, Ellipse, Polygon, Star or Vector layer(s)');
}
const limit = Math.max(idsText.length, idsShapes.length);
figma.ui.postMessage({
type: 'fetch-and-fill-ai',
message: {limit: limit, gender: gender, idsShapes: idsShapes, idsText: idsText},
});
}
// Will be deprecated soon
function fetchAndFill(quality: number, gender?: GenderType) {
const idsShapes = figma.currentPage.selection
.filter(
(node) =>
node.type === 'FRAME' ||
node.type === 'ELLIPSE' ||
node.type === 'POLYGON' ||
node.type === 'RECTANGLE' ||
node.type === 'STAR' ||
node.type === 'TEXT' ||
node.type === 'VECTOR'
)
.map((node) => node.id);
const idsText = figma.currentPage.selection.filter((node) => node.type === 'TEXT').map((node) => node.id);
if (idsShapes.length > 50 || idsText.length > 50) {
closePlugin(`🚨 You can't select more than 50 layers at a time right now.`);
}
if (idsShapes.length === 0 && idsText.length === 0) {
closePlugin('🚨 Select at least one or more Frame, Rectangle, Text, Ellipse, Polygon, Star or Vector layer(s)');
}
const limit = Math.max(idsText.length, idsShapes.length);
figma.ui.postMessage({
type: 'fetch-and-fill',
message: {quality: quality, limit: limit, gender: gender, idsShapes: idsShapes, idsText: idsText},
});
}
function fillAndClose(
items: Array<ItemType>,
idsShapes: Array<string>,
idsText: Array<string>,
images: Array<Uint8Array>
) {
// Fill Images (Sync)
idsShapes.slice(0, images.length).forEach((id, index) => {
fillWithData(images[index], id);
});
// Fill Text (Async)
const fillText = idsText
.slice(0, items.length)
.map((id, index) => fillWithText(`${items[index].first_name} ${items[index].last_name}`, id));
Promise.all(fillText).then(() => {
closePlugin();
});
}
async function fillWithText(text: string, targetId: string): Promise<boolean> {
const node = figma.currentPage.findOne((n) => n.id === targetId);
if (node.type === 'TEXT') {
const result = await setCharacters(node, text);
return result;
}
}
function openUrl(url: string) {
figma.ui.postMessage({
type: 'open-url',
message: {url: url},
});
}
function fillWithData(data: Uint8Array, targetId: string) {
const node = figma.currentPage.findOne((n) => n.id === targetId);
if (
node.type === 'FRAME' ||
node.type === 'ELLIPSE' ||
node.type === 'POLYGON' ||
node.type === 'RECTANGLE' ||
node.type === 'STAR' ||
node.type === 'VECTOR'
) {
const image = figma.createImage(data);
node.fills = [{type: 'IMAGE', imageHash: image.hash, scaleMode: 'FILL'}];
}
}
function closePlugin(message?: string) {
figma.closePlugin(message);
}