-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.ts
More file actions
244 lines (210 loc) · 7.52 KB
/
code.ts
File metadata and controls
244 lines (210 loc) · 7.52 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Show the UI
figma.showUI(__html__, { width: 300, height: 500 });
// Handle messages from the UI
figma.ui.onmessage = async (msg) => {
if (msg.type === 'create-table') {
await createTable(msg.rows, msg.columns, msg.columnTypes);
} else if (msg.type === 'cancel') {
figma.closePlugin();
}
};
/**
* Creates a table with the specified number of rows and columns
* using the table-cell component with appropriate variants
*/
async function createTable(rows: number, columns: number, columnTypes: string[]): Promise<void> {
try {
// Find table-cell instances on the page
const instances = figma.currentPage.findAllWithCriteria({
types: ['INSTANCE']
});
console.log(`Found ${instances.length} instances on the page`);
// Try to find a table-cell instance
let tableCellInstance: InstanceNode | null = null;
for (const instance of instances) {
if (instance.name.toLowerCase() === "table-cell") {
tableCellInstance = instance;
console.log(`Found table-cell instance: ${instance.name}`);
break;
}
}
// If we didn't find a specific instance, show an error
if (!tableCellInstance) {
figma.notify("🚨 Please add a table-cell instance on the page first.", { error: true });
return;
}
// Check if the instance has the variant property
if (!tableCellInstance.componentProperties ||
!tableCellInstance.componentProperties["variant"]) {
figma.notify("🚨 The table-cell instance doesn't have the required 'variant' property.", { error: true });
return;
}
// Create a frame for the table
const table = figma.createFrame();
table.name = "table";
table.layoutMode = "VERTICAL";
table.primaryAxisSizingMode = "AUTO";
table.counterAxisSizingMode = "FIXED";
table.itemSpacing = 0;
table.fills = [];
table.cornerRadius = 0;
table.paddingLeft = 0;
table.paddingRight = 0;
table.paddingTop = 0;
table.paddingBottom = 0;
// Set a reasonable width for the table
table.resize(800, table.height);
// Create header row
const headerRow = figma.createFrame();
headerRow.name = "header";
headerRow.layoutMode = "HORIZONTAL";
headerRow.primaryAxisSizingMode = "FIXED";
headerRow.counterAxisSizingMode = "AUTO";
headerRow.itemSpacing = 0;
headerRow.fills = [];
headerRow.resize(table.width, headerRow.height);
// Add header cells based on column types
for (let i = 0; i < columns; i++) {
const cell = tableCellInstance.clone();
const columnType = i < columnTypes.length ? columnTypes[i] : "header";
// Set the variant based on column type
cell.setProperties({
"variant": columnType
});
// Rename the cell based on its type
if (columnType === "checkbox") {
cell.name = "check";
// Make checkbox layer visible
makeSpecificLayerVisible(cell, "checkbox");
} else if (columnType === "icon") {
cell.name = "icon";
// Make iconWrapper layer visible
makeSpecificLayerVisible(cell, "iconWrapper");
// Set the icon to more-horizontal
setIconToMoreHorizontal(cell);
} else {
cell.name = "cell"; // For header cells in the header row
}
// Set layout properties based on column type
if (columnType === "checkbox" || columnType === "icon") {
// Fixed width for checkbox and icon cells
cell.layoutAlign = "INHERIT";
cell.layoutGrow = 0;
} else {
// Fill for header and body cells
cell.layoutAlign = "STRETCH";
cell.layoutGrow = 1;
}
headerRow.appendChild(cell);
}
table.appendChild(headerRow);
// Create data rows
for (let i = 0; i < rows - 1; i++) {
const dataRow = figma.createFrame();
dataRow.name = `row ${i + 1}`;
dataRow.layoutMode = "HORIZONTAL";
dataRow.primaryAxisSizingMode = "FIXED";
dataRow.counterAxisSizingMode = "AUTO";
dataRow.itemSpacing = 0;
dataRow.fills = [];
dataRow.resize(table.width, dataRow.height);
// Add body cells based on column types
for (let j = 0; j < columns; j++) {
const cell = tableCellInstance.clone();
let cellType = "body";
// Match the column type
if (j < columnTypes.length) {
// For checkbox and icon columns, use the same variant in the body
if (columnTypes[j] === "checkbox" || columnTypes[j] === "icon") {
cellType = columnTypes[j];
}
}
// Set the variant
cell.setProperties({
"variant": cellType
});
// Rename the cell based on its type
if (cellType === "checkbox") {
cell.name = "check";
} else if (cellType === "icon") {
cell.name = "icon";
} else {
cell.name = "cell"; // For body cells
}
// Set layout properties based on cell type
if (cellType === "checkbox" || cellType === "icon") {
// Fixed width for checkbox and icon cells
cell.layoutAlign = "INHERIT";
cell.layoutGrow = 0;
} else {
// Fill for header and body cells
cell.layoutAlign = "STRETCH";
cell.layoutGrow = 1;
}
dataRow.appendChild(cell);
}
table.appendChild(dataRow);
}
// Center the table in the viewport
const centerX = figma.viewport.center.x;
const centerY = figma.viewport.center.y;
table.x = centerX - (table.width / 2);
table.y = centerY - (table.height / 2);
// Select the table
figma.currentPage.selection = [table];
figma.viewport.scrollAndZoomIntoView([table]);
figma.notify("Table created successfully!");
figma.closePlugin();
} catch (error) {
console.error("Error creating table:", error);
figma.notify(`Error creating table: ${error}`, { error: true });
}
}
/**
* Makes a specific layer visible by name within a node
*/
function makeSpecificLayerVisible(node: SceneNode, layerName: string): void {
if ('children' in node) {
for (const child of node.children) {
// Make the specific layer visible if it matches the name
if (child.name === layerName) {
child.visible = true;
}
// Continue searching in children
if ('children' in child) {
makeSpecificLayerVisible(child, layerName);
}
}
}
}
/**
* Sets the icon to more-horizontal in the button-icon component
*/
function setIconToMoreHorizontal(node: SceneNode): void {
if ('children' in node) {
for (const child of node.children) {
// Find the iconWrapper
if (child.name === "iconWrapper") {
// Navigate to button-icon
if ('children' in child) {
for (const buttonIcon of child.children) {
if (buttonIcon.name === "button-icon" && buttonIcon.type === "INSTANCE") {
// Check if the button-icon has an "icon" property
if (buttonIcon.componentProperties && 'icon' in buttonIcon.componentProperties) {
// Set the icon property to more-horizontal
buttonIcon.setProperties({
"icon": "more-horizontal"
});
return; // Exit after setting the property
}
}
}
}
}
// Continue searching in children
if ('children' in child) {
setIconToMoreHorizontal(child);
}
}
}
}