-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.ts
More file actions
112 lines (96 loc) · 2.94 KB
/
code.ts
File metadata and controls
112 lines (96 loc) · 2.94 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
// This shows the HTML page in "ui.html"
figma.showUI(__html__, { width: 300, height: 200 });
// When the plugin starts, check if there's a selection
checkSelection();
// Listen for selection changes
figma.on("selectionchange", () => {
checkSelection();
});
// Function to check the current selection
function checkSelection() {
const selection = figma.currentPage.selection;
if (selection.length === 0) {
figma.ui.postMessage({ type: "no-selection" });
} else {
figma.ui.postMessage({
type: "selection-ready",
count: selection.length
});
}
}
// Listen for messages from the UI
figma.ui.onmessage = async (msg) => {
if (msg.type === "update-components") {
await updateComponentsToLatest();
} else if (msg.type === "cancel") {
figma.closePlugin();
}
};
// The main function to update components to their latest versions
async function updateComponentsToLatest() {
const selection = figma.currentPage.selection;
if (selection.length === 0) {
figma.ui.postMessage({ type: "error", message: "No selection found" });
return;
}
// Track our progress
let totalComponents = 0;
let updatedComponents = 0;
let skippedComponents = 0;
let errors = 0;
// Process all nodes in the selection (including nested nodes)
async function processNode(node) {
// Skip if the node is not visible or locked
if (node.visible === false || node.locked === true) {
return;
}
// If the node is an instance of a component
if (node.type === "INSTANCE") {
totalComponents++;
try {
// Check if there's a newer version available
const mainComponent = node.mainComponent;
if (mainComponent && mainComponent.remote === true) {
const library = await figma.importComponentByKeyAsync(mainComponent.key);
if (library && library !== node.mainComponent) {
// Swap to the latest version
node.swapComponent(library);
updatedComponents++;
figma.ui.postMessage({
type: "progress",
updated: updatedComponents,
total: totalComponents
});
} else {
// Already at the latest version
skippedComponents++;
}
} else {
// Local component or no main component
skippedComponents++;
}
} catch (error) {
errors++;
console.error("Error updating component:", error);
}
}
// Process children if the node has them
if ("children" in node) {
for (const child of node.children) {
await processNode(child);
}
}
}
// Start processing from the top-level selected nodes
for (const node of selection) {
await processNode(node);
}
// Report the results
figma.ui.postMessage({
type: "complete",
total: totalComponents,
updated: updatedComponents,
skipped: skippedComponents,
errors: errors
});
}