-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
171 lines (171 loc) · 7.57 KB
/
Copy pathcode.js
File metadata and controls
171 lines (171 loc) · 7.57 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
// code.ts — v12: frameIndex from frame name, section filter support
figma.showUI(__html__, { width: 560, height: 820, themeColors: true });
function isIOSScreenshot(name) { return /^\d+_APP_/.test(name); }
const ANDROID_SECTIONS = {
"ANDROID PHONE": { folder: "phoneScreenshots", scale: 3 },
"ANDROID TAB 7 INCH": { folder: "sevenInchScreenshots", scale: 3 },
"ANDROID TAB 10 INCH": { folder: "tenInchScreenshots", scale: 2 },
"ANDROID TV": { folder: "tvScreenshots", scale: 2 },
"FEATUREGRAPHIC": { folder: "__feature__", scale: 1 },
};
function norm(s) { return s.trim().toUpperCase().replace(/\s+/g, " "); }
// Извлекает числовой префикс из имени фрейма: "3_en-US" → 3, "featureGraphic" → null
function extractFrameIndex(frameName) {
const m = frameName.match(/^(\d+)/);
return m ? parseInt(m[1]) : null;
}
function getIOSFrames(page) {
return page.children
.filter(n => (n.type === "FRAME" || n.type === "COMPONENT" || n.type === "INSTANCE") && isIOSScreenshot(n.name))
.map(f => ({ name: f.name, width: Math.round(f.width), height: Math.round(f.height) }));
}
function getAndroidFrames(page) {
const frames = [], sections = [];
for (const child of page.children) {
if (child.type === "SECTION" && ANDROID_SECTIONS[norm(child.name)]) {
sections.push(child.name);
for (const sub of child.children) {
if (sub.type === "FRAME" || sub.type === "COMPONENT" || sub.type === "INSTANCE")
frames.push({ name: sub.name, width: Math.round(sub.width), height: Math.round(sub.height), section: child.name });
}
}
}
return { frames, sections };
}
function sendDocInfo() {
figma.ui.postMessage({ type: "doc-info", docName: figma.root.name });
}
function sendPagesInfo(storeType) {
const pages = figma.root.children.map(p => {
const all = p.children.length;
if (storeType === "playmarket") {
const { frames, sections } = getAndroidFrames(p);
return { id: p.id, name: p.name, frames, sections, allFramesCount: all, skippedCount: all - frames.length };
}
const frames = getIOSFrames(p);
return { id: p.id, name: p.name, frames, allFramesCount: all, skippedCount: all - frames.length };
});
figma.ui.postMessage({ type: "pages-info", pages });
}
let stopRequested = false;
async function exportFrames(pageIds, storeType, scaleMap, defaultScale, sectionFilter // null = все секции, иначе только указанные
) {
stopRequested = false;
const toExport = figma.root.children.filter(p => pageIds.includes(p.id));
let total = 0;
for (const p of toExport) {
if (storeType === "playmarket") {
const { frames } = getAndroidFrames(p);
total += sectionFilter
? frames.filter(f => f.section && sectionFilter.includes(norm(f.section))).length
: frames.length;
}
else {
total += getIOSFrames(p).length;
}
}
figma.ui.postMessage({ type: "export-start", total });
let done = 0;
for (const page of toExport) {
if (stopRequested)
break;
figma.currentPage = page;
if (storeType === "playmarket") {
for (const child of page.children) {
if (stopRequested)
break;
if (child.type !== "SECTION")
continue;
const sectionNorm = norm(child.name);
const cfg = ANDROID_SECTIONS[sectionNorm];
if (!cfg)
continue;
// Пропускаем секцию если задан фильтр и её нет в нём
if (sectionFilter && !sectionFilter.includes(sectionNorm))
continue;
for (const frame of child.children) {
if (stopRequested)
break;
if (frame.type !== "FRAME" && frame.type !== "COMPONENT" && frame.type !== "INSTANCE")
continue;
try {
const bytes = await frame.exportAsync({ format: "PNG", constraint: { type: "SCALE", value: cfg.scale } });
done++;
// Извлекаем индекс из имени фрейма (напр. "3_en-US" → 3)
const frameIndex = extractFrameIndex(frame.name);
figma.ui.postMessage({
type: "frame-exported",
pageName: page.name,
frameName: frame.name,
frameIndex, // число или null для featureGraphic
width: Math.round(frame.width),
height: Math.round(frame.height),
scale: cfg.scale,
section: child.name,
folder: cfg.folder,
bytes: Array.from(bytes),
current: done,
total
});
}
catch (e) {
done++;
figma.ui.postMessage({ type: "log", level: "error", text: `Fail: ${frame.name}: ${e}` });
}
}
}
}
else {
const frames = page.children.filter(n => (n.type === "FRAME" || n.type === "COMPONENT" || n.type === "INSTANCE") && isIOSScreenshot(n.name));
for (const frame of frames) {
if (stopRequested)
break;
try {
let scale = defaultScale;
if (scaleMap) {
const u = frame.name.toUpperCase();
for (const [k, v] of Object.entries(scaleMap)) {
if (u.includes(k)) {
scale = v;
break;
}
}
}
const bytes = await frame.exportAsync({ format: "PNG", constraint: { type: "SCALE", value: scale } });
done++;
figma.ui.postMessage({
type: "frame-exported",
pageName: page.name,
frameName: frame.name,
frameIndex: null,
width: Math.round(frame.width),
height: Math.round(frame.height),
scale,
bytes: Array.from(bytes),
current: done,
total
});
}
catch (e) {
done++;
figma.ui.postMessage({ type: "log", level: "error", text: `Fail: ${frame.name}: ${e}` });
}
}
}
}
figma.ui.postMessage({ type: "export-done", exported: done, total, stopped: stopRequested });
}
figma.ui.onmessage = msg => {
if (msg.type === "get-doc-info")
sendDocInfo();
if (msg.type === "get-pages")
sendPagesInfo(msg.store || "appstore");
if (msg.type === "export-frames")
exportFrames(msg.pages || [], msg.store || "appstore", msg.scaleMap || null, msg.scale || 2, msg.sectionFilter || null);
if (msg.type === "stop-export")
stopRequested = true;
if (msg.type === "cancel")
figma.closePlugin();
};
sendDocInfo();
sendPagesInfo("appstore");