figma / plugin-typings

Typings for the Figma Plugin API
MIT License
195 stars 45 forks source link

How to access "document" in code.ts? #158

Closed ChrisChan888 closed 2 years ago

ChrisChan888 commented 2 years ago

Document is undefined in code.ts. I can't createElement('a') for download some styles data I get from figma API. How to access "document" in code.ts? So user can download the json generate by the plugin? function downloadObjectAsJson(exportObj, exportName){ var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj)); var downloadAnchorNode = document.createElement('a'); downloadAnchorNode.setAttribute("href", dataStr); downloadAnchorNode.setAttribute("download", exportName + ".json"); document.body.appendChild(downloadAnchorNode); // required for firefox downloadAnchorNode.click(); downloadAnchorNode.remove(); }

glebsexy commented 2 years ago

A better place to ask this is probably on the forum https://forum.figma.com/ or Discord https://discord.com/invite/figma

You can't do that within the plugin backend, it needs to be done on the UI side (iframe).

ChrisChan888 commented 2 years ago

We can use figma.ui.postMessage and send data to ui.html, then execute downloadObjectAsJson function.