MCPcopy Index your code
hub / github.com/figma/plugin-samples / processCollection

Function processCollection

variables-import-export/code.js:136–164  ·  view source on GitHub ↗
({ name, modes, variableIds })

Source from the content-addressed store, hash-verified

134}
135
136async function processCollection({ name, modes, variableIds }) {
137 const files = [];
138 for (const mode of modes) {
139 const file = { fileName: `${name}.${mode.name}.tokens.json`, body: {} };
140 for (const variableId of variableIds) {
141 const { name, resolvedType, valuesByMode } =
142 await figma.variables.getVariableByIdAsync(variableId);
143 const value = valuesByMode[mode.modeId];
144 if (value !== undefined && ["COLOR", "FLOAT"].includes(resolvedType)) {
145 let obj = file.body;
146 name.split("/").forEach((groupName) => {
147 obj[groupName] = obj[groupName] || {};
148 obj = obj[groupName];
149 });
150 obj.$type = resolvedType === "COLOR" ? "color" : "number";
151 if (value.type === "VARIABLE_ALIAS") {
152 const currentVar = await figma.variables.getVariableByIdAsync(
153 value.id
154 );
155 obj.$value = `{${currentVar.name.replace(/\//g, ".")}}`;
156 } else {
157 obj.$value = resolvedType === "COLOR" ? rgbToHex(value) : value;
158 }
159 }
160 }
161 files.push(file);
162 }
163 return files;
164}
165
166figma.ui.onmessage = async (e) => {
167 console.log("code received message", e);

Callers 1

exportToJSONFunction · 0.85

Calls 1

rgbToHexFunction · 0.70

Tested by

no test coverage detected