I've recently joined Sketch to work on the plugin API.
The Actions API was originally designed as a way to trigger actions in Sketch as well as a way to listen for changes. There have been some issues with it so we are working on a replacement that just focuses on events i.e. when something happens in Sketch, plugins will be notified. We are currently working on notifying plugins when changes to the document occur and we would like to get your feedback on it.
In your plugin you will have a onDocumentChanged
function where you can get access to changeSet
. You can query this to get the layers/properties that have changed or the layers that have been created or deleted. Unlike actions, objects will be wrapped JavaScript objects rather than native Sketch objects. The function will be executed at a reasonable interval i.e. if you drag a layer, it will be called when you have finished dragging the layer rather than continuously as you drag the layer. We plan on supporting most of the properties currently in the JavaScript API. For example if you move or resize a layer the changed property will be frame
or if you change the color of the first fill, the property will be style.fills[0].color
.
Here is an example of what the code might look like:
function onDocumentChanged(context) {
const changeSet = context.eventContext.changeSet
/*
Get the layers/properties that have changed. Optionally filtered by properties and layers
changeSet.getChangedLayers(options)
options: Object, optional
properties: array of strings
layers: array of Layers or strings (layer IDs)
Returns:
[
{
layer: Layer,
properties: {
frame: {
prevValue: Rectangle,
value: Rectangle
}
}
}
]
*/
// Examples
let changedLayers = changeSet.getChangedLayers()
changedLayers = changeSet.getChangedLayers({ properties: ['frame'] })
changedLayers = changeSet.getChangedLayers({ properties: ['frame', 'hidden'], layers: [layer1] })
changedLayers = changeSet.getChangedLayers({ properties: ['frame'], layers: [layer1.id, layer2.id] })
/*
Get the layers that have been created or deleted
changeSet.createdLayers
changeSet.deletedLayers
Returns an array of Layers
*/
// Examples
let createdLayers = changeSet.createdLayers
let deletedLayers = changeSet.deletedLayers
}
You will also be able to do the same for other objects like text and layer styles:
let changedSharedStyles = changeSet.getChangedTextStyles()
let createdSharedStyles = changeSet.createdTextStyles
let deletedSharedStyles = changeSet.deletedTextStyles
Let me know what you think and also let me know if think there is a use-case you had in mind that the above doesn't cover.