-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
197 lines (162 loc) · 6.37 KB
/
code.js
File metadata and controls
197 lines (162 loc) · 6.37 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
196
197
figma.showUI(__html__, { width: 300, height: 500 });
let selectedNodes = [];
function findTextNodes(node) {
const textNodes = [];
if (node.type === 'TEXT') {
textNodes.push(node);
} else if ('children' in node) {
for (const child of node.children) {
textNodes.push(...findTextNodes(child));
}
}
return textNodes;
}
function updateSelection() {
const selection = figma.currentPage.selection;
console.log('Selection changed, length:', selection.length);
if (selection.length === 0) {
selectedNodes = [];
figma.ui.postMessage({
type: 'show-selection',
selectionType: 'none'
});
return;
}
if (selection.length === 1) {
const node = selection[0];
console.log('Single node selected, type:', node.type);
if (node.type === 'TEXT') {
// Single text selected
selectedNodes = [node];
console.log('Text node selected:', node.characters);
figma.ui.postMessage({
type: 'show-selection',
selectionType: 'text',
text: node.characters,
textCount: 1,
allTexts: [node.characters]
});
} else if (node.type === 'FRAME' || node.type === 'GROUP' || node.type === 'COMPONENT' || node.type === 'INSTANCE' || 'children' in node) {
// Frame or group selected - find all text nodes
const textNodes = findTextNodes(node);
selectedNodes = textNodes;
console.log('Container selected, found text nodes:', textNodes.length);
if (textNodes.length > 0) {
const allTexts = textNodes.map(n => n.characters);
console.log('All texts found:', allTexts);
figma.ui.postMessage({
type: 'show-selection',
selectionType: 'frame',
text: allTexts[0],
textCount: textNodes.length,
frameName: node.name,
allTexts: allTexts
});
} else {
selectedNodes = [];
figma.ui.postMessage({
type: 'show-selection',
selectionType: 'empty',
frameName: node.name
});
}
} else {
selectedNodes = [];
figma.ui.postMessage({
type: 'show-selection',
selectionType: 'other'
});
}
} else {
// Multiple nodes selected
selectedNodes = [];
let allTextNodes = [];
for (const node of selection) {
if (node.type === 'TEXT') {
allTextNodes.push(node);
} else if ('children' in node) {
allTextNodes.push(...findTextNodes(node));
}
}
if (allTextNodes.length > 0) {
selectedNodes = allTextNodes;
const allTexts = allTextNodes.map(n => n.characters);
console.log('Multiple selection with text nodes:', allTextNodes.length);
figma.ui.postMessage({
type: 'show-selection',
selectionType: 'frame',
text: allTexts[0],
textCount: allTextNodes.length,
frameName: `${selection.length} items`,
allTexts: allTexts
});
} else {
figma.ui.postMessage({
type: 'show-selection',
selectionType: 'empty'
});
}
}
}
// Listen for selection changes
figma.on('selectionchange', updateSelection);
// Initialize with current selection
updateSelection();
// Handle messages from UI
figma.ui.onmessage = async (msg) => {
console.log('Received message:', msg.type);
if (selectedNodes.length === 0) {
figma.notify('Please select a text layer or frame containing text');
return;
}
if (msg.type === 'apply-translations' && msg.translations) {
console.log('Applying translations:', msg.translations.length);
let success = 0;
let errors = 0;
for (let i = 0; i < selectedNodes.length && i < msg.translations.length; i++) {
try {
const node = selectedNodes[i];
const translation = msg.translations[i];
console.log(`Applying translation ${i + 1}:`, translation);
// Load the font before changing text
await figma.loadFontAsync(node.fontName);
// Apply the translation
node.characters = translation;
success++;
} catch (error) {
console.error(`Error applying translation ${i + 1}:`, error);
errors++;
}
}
if (success > 0) {
figma.notify(`Applied ${success} translation(s)${errors > 0 ? ` (${errors} failed)` : ''}`);
} else {
figma.notify('Failed to apply translations. Check font loading.');
}
}
if (msg.type === 'reset-texts' && msg.originalTexts) {
console.log('Resetting texts:', msg.originalTexts.length);
let success = 0;
let errors = 0;
for (let i = 0; i < selectedNodes.length && i < msg.originalTexts.length; i++) {
try {
const node = selectedNodes[i];
const originalText = msg.originalTexts[i];
console.log(`Resetting text ${i + 1}:`, originalText);
// Load the font before changing text
await figma.loadFontAsync(node.fontName);
// Reset to original text
node.characters = originalText;
success++;
} catch (error) {
console.error(`Error resetting text ${i + 1}:`, error);
errors++;
}
}
if (success > 0) {
figma.notify(`Reset ${success} text layer(s)${errors > 0 ? ` (${errors} failed)` : ''}`);
} else {
figma.notify('Failed to reset texts. Check font loading.');
}
}
};