项目作者: passiomatic

项目描述 :
Use the Figma web API in Elm
高级语言: Elm
项目地址: git://github.com/passiomatic/elm-figma-api.git
创建时间: 2018-04-08T14:32:42Z
项目社区:https://github.com/passiomatic/elm-figma-api

开源协议:MIT License

下载


Figma API with Elm

This package aims to provide a typed, Elm-friendly access to the Figma web API.

The API currently supports view-level operations on Figma files. Given a file, you can inspect its Elm representation, read or post comments, or export a rendered image of any node subtree.

Live “Swatches” example

The example below fetches a given Figma file and collects all the found colors and gradients used as background or paint fills.

Try it now or view source

Get a document file

First, let’s create a authentication token and pass that to the getFile function, together with the file key we want to retrieve.

  1. import Http
  2. import Figma as F
  3. F.getFile
  4. ( F.personalToken "your-token" )
  5. "your-file-key"
  6. |> Http.send FileReceived

Then in the update function we can extract the FileReceived message payloadand store it in the model app:

  1. FileReceived result ->
  2. case result of
  3. Ok response ->
  4. ( { model | documentRoot = response.document }, Cmd.none )
  5. Err error ->
  6. let
  7. _ = Debug.log "Error while fetching file" error
  8. in
  9. ( model, Cmd.none )

Note: The key can be extracted from any Figma file URL: https://www.figma.com/file/:key/:title, or via the getFiles function.

Export a document node to PNG

Here we start a request to export the node with ID 1:6 into a PNG file.

  1. import Http
  2. import Figma as F
  3. F.exportNodesAsPng
  4. ( F.personalToken "your-token" )
  5. "your-file-key"
  6. [ "1:6" ]
  7. |> Http.send ExportFinished

Once finished the ExportFinished message will contain a list of ExportedImage, with the URL’s for the rendered images.

Changes from previous version

  • All top module functions now return a Http.Request value instead of Cmd, so you can chain calls together. See getFile documentation for an example.
  • Added BooleanGroup and BooleanOperation.
  • Added support for file versions.

Missing pieces

If you need any of these features please open an issue.