-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
185 lines (164 loc) · 4.74 KB
/
code.js
File metadata and controls
185 lines (164 loc) · 4.74 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
const handlerRegistry = require('./handlers/handlerRegistry');
const stickerHandler = require('./handlers/stickerHandler');
const stickerColumnHandler = require('./handlers/stickerColumnHandler');
const imagesHandler = require('./handlers/imagesHandler');
const tableHandler = require('./handlers/tablesHandler');
const sectionHandler = require('./handlers/sectionHandler');
const titleHandler = require('./handlers/titleHandler');
const boardDumper = require('./handlers/boardDumper');
// Register handlers
handlerRegistry.register('addSticker', stickerHandler.handle);
handlerRegistry.register('addStickerColumn', stickerColumnHandler.handle);
handlerRegistry.register('addImages', imagesHandler.handle);
handlerRegistry.register('addTable', tableHandler.handle);
handlerRegistry.register('addSection', sectionHandler.handle);
handlerRegistry.register('addTitle', titleHandler.handle);
handlerRegistry.register('boardDump', boardDumper.handle);
figma.showUI(__html__, { width: 420, height: 450 });
// Utility: Send message to UI
function sendToUI(type, data) {
figma.ui.postMessage({
type,
...data
});
}
// Utility: Log and notify
function log(message) {
console.log(message);
figma.notify(message);
sendToUI('log', {
message
});
}
// Find a node by title (used by handlers)
function findNodeByTitle(title) {
try {
// Search for sticky notes
const nodes = figma.currentPage.findAll(node => {
try {
return (
(node.type === "STICKY" || node.type === "SHAPE_WITH_TEXT") &&
node.name.toLowerCase().includes(title.toLowerCase())
);
} catch (e) {
return false;
}
});
if (nodes.length > 0) {
return nodes[0];
}
// Search for text nodes
const textNodes = figma.currentPage.findAll(node => {
try {
return (
node.type === "TEXT" &&
node.characters.toLowerCase().includes(title.toLowerCase())
);
} catch (e) {
return false;
}
});
if (textNodes.length > 0) {
return textNodes[0];
}
return null;
} catch (e) {
log(`Error finding node: ${e.message}`);
return null;
}
}
// Export utilities for handlers (use globalThis instead of global)
globalThis.figmaUtils = {
log,
sendToUI,
findNodeByTitle
};
// Dump current board to JSON
async function dumpBoard() {
try {
const boardData = {
name: figma.currentPage.name,
nodes: []
};
const allNodes = figma.currentPage.findAll();
for (const node of allNodes) {
const nodeData = {
id: node.id,
type: node.type,
name: node.name
};
// Add position if available
if (node.x !== undefined && node.y !== undefined) {
nodeData.position = {
x: node.x,
y: node.y
};
}
// Add size if available
if (node.width !== undefined && node.height !== undefined) {
nodeData.size = {
width: node.width,
height: node.height
};
}
// Add text content for text-based nodes
if (node.type === "TEXT" || node.type === "STICKY") {
try {
nodeData.text = node.characters || node.text?.characters || "";
} catch (e) {
nodeData.text = "";
}
}
boardData.nodes.push(nodeData);
}
sendToUI('board-dump', {
data: boardData
});
log('Board exported successfully');
} catch (e) {
log(`Error dumping board: ${e.message}`);
}
}
figma.ui.onmessage = async (msg) => {
try {
if (msg.type === 'process-request') {
// Route to appropriate handler
const requestType = msg.data.type;
const handler = handlerRegistry.get(requestType);
if (handler) {
log(`Processing ${requestType} request...`);
await handler(msg.data);
sendToUI('status', {
message: `${requestType} completed`,
status: 'active'
});
} else {
log(`Unknown request type: ${requestType}`);
sendToUI('status', {
message: `Unknown type: ${requestType}`,
status: 'error'
});
}
} else if (msg.type === 'dump-board') {
const handler = handlerRegistry.get('boardDump');
await handler({ action: 'dump' });
} else if (msg.type === 'load-board') {
const handler = handlerRegistry.get('boardDump');
await handler({
action: 'load',
boardData: msg.boardData,
clearExisting: msg.clearExisting
});
} else if (msg.type === 'start-polling') {
log('Polling started');
} else if (msg.type === 'stop-polling') {
log('Polling stopped');
}
} catch (e) {
log(`Error: ${e.message}`);
sendToUI('status', {
message: `Error: ${e.message}`,
status: 'error'
});
}
};