Repository navigation
Expand file tree
/
Copy pathcode.js
More file actions
104 lines (91 loc) · 3.48 KB
/
Copy pathcode.js
File metadata and controls
104 lines (91 loc) · 3.48 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
figma.showUI(__html__, { width: 360, height: 580, themeColors: true });
// Helper: recursively find all text nodes in a node tree
function findTextNodes(node) {
const texts = [];
if (node.type === "TEXT") {
texts.push(node);
}
if ("children" in node) {
for (const child of node.children) {
texts.push(...findTextNodes(child));
}
}
return texts;
}
// Load saved settings and send to UI on startup
async function loadSettings() {
const provider = await figma.clientStorage.getAsync("te_provider") || "openai";
const apiKey = await figma.clientStorage.getAsync("te_apikey") || "";
const model = await figma.clientStorage.getAsync("te_model") || "";
figma.ui.postMessage({ type: "load-settings", provider, apiKey, model });
}
loadSettings();
// When UI sends a message
console.log("Plugin code.js loaded");
figma.ui.onmessage = async (msg) => {
console.log("code.js received message:", JSON.stringify(msg));
if (msg.type === "save-setting") {
await figma.clientStorage.setAsync(msg.key, msg.value);
return;
}
if (msg.type === "get-selection") {
const selection = figma.currentPage.selection;
if (selection.length !== 1) {
figma.ui.postMessage({ type: "selection-info", data: null, error: "Lutfen tek bir frame/grup secin." });
return;
}
const node = selection[0];
const textNodes = findTextNodes(node);
const texts = textNodes.map((t, i) => ({ index: i, name: t.name, characters: t.characters }));
figma.ui.postMessage({ type: "selection-info", data: { name: node.name, texts } });
}
if (msg.type === "generate") {
const selection = figma.currentPage.selection;
if (selection.length !== 1) {
figma.notify("Lutfen tek bir frame/grup secin.");
return;
}
const sourceNode = selection[0];
const replacements = msg.replacements; // array of { texts: [{index, newText}] } per variant
for (let v = 0; v < replacements.length; v++) {
const variant = replacements[v];
// Clone the source node
const clone = sourceNode.clone();
// Position clone to the right of the source (or previous clone)
clone.x = sourceNode.x + (sourceNode.width + 40) * (v + 1);
clone.y = sourceNode.y;
// Find text nodes in the clone
const cloneTextNodes = findTextNodes(clone);
// Replace texts
for (const rep of variant.texts) {
const textNode = cloneTextNodes[rep.index];
if (textNode) {
try {
const fontsToLoad = textNode.getRangeAllFontNames(0, textNode.characters.length);
for (const font of fontsToLoad) {
await figma.loadFontAsync(font);
}
textNode.characters = rep.newText;
} catch (e) {
console.log("Font yukleme hatasi, fallback deneniyor:", e.message);
try {
// Fallback: set entire node to its first font, then change text
const firstFont = textNode.getRangeAllFontNames(0, 1)[0];
await figma.loadFontAsync(firstFont);
textNode.fontName = firstFont;
textNode.characters = rep.newText;
} catch (e2) {
console.log("Fallback de basarisiz, text degistirilemedi:", textNode.name);
}
}
}
}
// Append to the same parent
if (sourceNode.parent) {
sourceNode.parent.appendChild(clone);
}
}
figma.notify(`${replacements.length} kopya olusturuldu!`);
figma.ui.postMessage({ type: "generate-done" });
}
};