-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
71 lines (71 loc) · 3.23 KB
/
code.js
File metadata and controls
71 lines (71 loc) · 3.23 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
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
console.clear();
figma.showUI(__html__, { width: 400, height: 300 });
console.log('Plugin started!');
figma.on('selectionchange', () => {
const selectedSections = figma.currentPage.selection.filter(node => node.type === 'SECTION');
figma.ui.postMessage({ type: 'selection-count', count: selectedSections.length });
});
figma.ui.onmessage = (msg) => __awaiter(void 0, void 0, void 0, function* () {
if (msg.type === 'create-json') {
try {
const selectedSections = figma.currentPage.selection.filter(node => node.type === 'SECTION');
if (selectedSections.length === 0) {
figma.notify('Please select at least one section.');
return;
}
const exportData = {
file: figma.root.name,
sections: {}
};
const processSection = (section) => {
const sectionData = {
sticky_notes: {},
text_fields: {},
nested_sections: {}
};
if ('children' in section) {
for (const child of section.children) {
if (child.type === 'STICKY') {
sectionData.sticky_notes["sticky " + child.id] = child.text.characters;
}
else if (child.type === 'TEXT') {
sectionData.text_fields["text_field " + child.id] = child.characters;
}
else if (child.type === 'SECTION') {
sectionData.nested_sections[child.name] = processSection(child);
}
}
}
return sectionData;
};
for (const section of selectedSections) {
exportData.sections[section.name] = processSection(section);
}
// Convert the export data to a JSON string
const jsonString = JSON.stringify(exportData, null, 2);
// Send the JSON data to the UI
figma.ui.postMessage({ type: 'json-data', data: jsonString });
}
catch (error) {
figma.notify('An error occurred while exporting to JSON.');
console.error('Export error:', error);
}
}
if (msg.type === 'close-plugin') {
figma.closePlugin();
}
if (msg.type === 'copy-to-clipboard') {
figma.notify('JSON data copied to clipboard!');
figma.ui.postMessage({ type: 'copyToClipboard', data: msg.data });
}
});