Created
September 17, 2021 15:38
-
-
Save darknoon/663741d92140dd92190fc8b4c725c63e to your computer and use it in GitHub Desktop.
Is this the best way to generate a Zip file in Scripter?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const colors = figma.getLocalPaintStyles() | |
.map(c => { | |
const {name, paints} = c | |
const paint = paints[0] | |
if (isSolidPaint(paint)) { | |
const {opacity, color} = paint | |
const {r,g,b} = color | |
return {name, color: {red: r, green: g, blue: b, alpha: opacity} } | |
} else { | |
return undefined | |
} | |
}).filter(c => c) | |
//print(colors) | |
const zipper = createWorker(async w => { | |
const JSZip_ = await w.import("jszip@3.7.1/dist/jszip.min.js") | |
const JSZip = JSZip_() | |
const colors = await w.recv() | |
const zip = JSZip(); | |
const info = { | |
author : "darknoon.figma.ExportXCAssets", | |
version : 1, | |
}; | |
// TODO: parse namespaces | |
const folderContents = { | |
info, | |
properties: { | |
"provides-namespace" : true | |
} | |
}; | |
zip.folder("Histogram") | |
.file("Contents.json", JSON.stringify(folderContents)) | |
for (let c of colors) { | |
const {name, color} = c | |
const folder = zip.folder(`${name}.colorset`) | |
const contents = { | |
colors: [ | |
// Add other variants here | |
{ | |
color : { | |
"color-space" : "srgb", | |
components : color, | |
}, | |
idiom : "universal" | |
} | |
], | |
info | |
}; | |
folder.file("Contents.json", JSON.stringify(contents)) | |
} | |
const result = await zip.generateAsync({type : "uint8array"}) | |
w.send(result) | |
}) | |
zipper.send(colors) | |
const zip = await zipper.recv(); | |
//print("Result: ", zip) | |
//print("Opening window…") | |
let downloadWindow = createWindow({width:300, height:300}, async w => { | |
const data = await w.recv(); | |
if (true) { | |
const blob = new w.Blob([data], {type: "application/zip"}) | |
const blobURL = w.URL.createObjectURL(blob) | |
const button = w.createElement("a") as WebDOM.HTMLAnchorElement; | |
button.href = blobURL | |
button.download = "Assets.zip" | |
// Show the Blob URL… clicking doesn't trigger a download tho | |
button.textContent = "Option-click to download" | |
button.setAttribute("download", "Assets.zip") | |
//button.click() | |
w.document.body.appendChild(button) | |
} else { | |
// This API seems to not be available in Scripter | |
const f = new File([data], "Assets.zip") | |
w.window.saveAs(f, "Assets.zip") | |
} | |
}) | |
downloadWindow.send(zip) | |
await downloadWindow | |
print("done") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment