Warm tip: This article is reproduced from serverfault.com, please click

Figma Plugin: callback on file updated

发布于 2020-11-15 21:36:15

I found the callback 'on' interesting but limiting https://www.figma.com/plugin-docs/api/properties/figma-on/#docsNav

I there a way to trigger an event once the file has been updated?

Questioner
Giardiv
Viewed
0
Gleb Sabirzyanov 2020-11-28 08:07:21

There is no way to do this at the moment. The only type of update you can get is if selection changes or current page changes. Here is an example from the docs:

figma.on("selectionchange", () => { console.log("changed") })

The method commonly used by plugins to watch for changes on nodes is polling: simply creating an interval or timer and checking if one of properties changed from the previously saved state.

let interval = setInterval(checkNodes, 500) // check every 300ms

const node = figma.currentPage.selection[0] // first node in selection
let nodeWidth = node.width // store node properties to watch

function checkNodes() {
  if (nodeWidth !== node.width) {
    // width changed
  }
  nodeWidth = node.width
}