-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
145 lines (125 loc) · 3.51 KB
/
code.js
File metadata and controls
145 lines (125 loc) · 3.51 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
// Figma plugin asosiy kodi — batch processing qo'llab-quvvatlanadi
figma.showUI(__html__, { width: 400, height: 400 });
// Batch navbati
let nodeQueue = [];
let queueIndex = 0;
// Navbatdagi keyingi node ni UI ga yuborish
async function sendNextImage() {
// Navbat tugagan — hammasi qayta ishlandi
if (queueIndex >= nodeQueue.length) {
const count = nodeQueue.length;
nodeQueue = [];
queueIndex = 0;
figma.ui.postMessage({ type: "batch-done", count });
return;
}
const node = nodeQueue[queueIndex];
// Node o'chirilgan bo'lsa — o'tkazib yuborish
if (!node || node.removed) {
queueIndex++;
await sendNextImage();
return;
}
const imageFill = node.fills.find((f) => f.type === "IMAGE" && f.imageHash);
if (!imageFill) {
queueIndex++;
await sendNextImage();
return;
}
try {
const image = figma.getImageByHash(imageFill.imageHash);
if (!image) {
queueIndex++;
await sendNextImage();
return;
}
// Get image bytes and send to UI
const bytes = await image.getBytesAsync();
figma.ui.postMessage({
type: "image-data",
bytes,
nodeId: node.id,
current: queueIndex + 1,
total: nodeQueue.length,
});
} catch (err) {
figma.ui.postMessage({
type: "error",
message: "Failed to read image: " + (err.message || "Unknown error"),
});
nodeQueue = [];
queueIndex = 0;
}
}
// Messages from UI
figma.ui.onmessage = async (msg) => {
// ── open-url: open external link via Figma ──
if (msg.type === "open-url") {
figma.openExternal(msg.url);
return;
}
// ── get-image: queue selected nodes and start processing ──
if (msg.type === "get-image") {
const selection = figma.currentPage.selection;
if (selection.length === 0) {
figma.ui.postMessage({
type: "error",
message: "Please select an image first",
});
return;
}
// Filter nodes that have an IMAGE fill
const imageNodes = selection.filter(
(node) =>
"fills" in node &&
node.fills &&
node.fills.some((f) => f.type === "IMAGE" && f.imageHash),
);
if (imageNodes.length === 0) {
figma.ui.postMessage({
type: "error",
message: "No image fill found in selection",
});
return;
}
nodeQueue = imageNodes;
queueIndex = 0;
await sendNextImage();
return;
}
// ── apply-result: fonsiz rasmni tegishli node ga qo'llash ──
if (msg.type === "apply-result") {
const node = nodeQueue[queueIndex];
if (!node || node.removed) {
queueIndex++;
await sendNextImage();
return;
}
try {
const newImage = figma.createImage(msg.bytes);
const oldFill = node.fills.find((f) => f.type === "IMAGE" && f.imageHash);
// Avvalgi fill larni JSON orqali klonlash (Figma proxy muammosini oldini olish)
const hiddenFills = JSON.parse(JSON.stringify([...node.fills])).map(
function (f) {
return Object.assign({}, f, { visible: false });
},
);
// Yangi fonsiz rasmni ko'rinadigan qilib qo'shish
node.fills = [
...hiddenFills,
{
type: "IMAGE",
imageHash: newImage.hash,
scaleMode: oldFill ? oldFill.scaleMode : "FILL",
visible: true,
},
];
queueIndex++;
await sendNextImage();
} catch (err) {
figma.notify("Error: " + (err.message || "Unknown"), { error: true });
nodeQueue = [];
queueIndex = 0;
}
}
};