selection.registerOnChange
Registers a callback that runs when a user selects (or deselects) content within a design.
To learn more, see Selection API.
Usage
import { selection } from "@canva/design";await selection.registerOnChange({scope: "plaintext",onChange: (event) => {console.log(event);},});
Scopes
Before an app that uses this method can be submitted for review, the following scopes must be enabled through the Developer Portal:
canva:design:content:read, if the app calls thereadmethodcanva:design:content:write, if the app calls thesavemethod
To learn more, see Configuring scopes.
Parameters
optionsobjectThe options for registering a selection callback.
options.scopestringThe type of content that should trigger a selection event.
The available options include:
"image""plaintext""richtext""video"
options.onChangefunctionThe callback that runs when a user selects or deselects the specified type of content.
options.onChange(event)objectThe selection event.
options.onChange(event.scope)stringThe type of content that's selected. This matches the value passed into the scope property.
options.onChange(event.count)numberThe number of distinct pieces of content that are selected.
options.onChange(event.read())functionReads the selected content.
Returns
A Promise that resolves with the selected content and a save method for persisting changes to the content.
options.onChange(event.read().contents)arrayThe selected content. Each distinct piece of content — for example, the content of two separate text elements — is a separate object. These objects have properties and methods for interacting with the content.
The available properties depend on the value of the scope property.
Images
If the scope is "image", each object has the following properties:
refstringAn identifier that points to an image asset in Canva's backend.
Plaintext
If the scope is "plaintext", each object has the following properties:
textstringThe text content, as plaintext.
Richtext
If the scope is "richtext", each object is a RichtextRange. This is the same object that's returned by the createRichtextRange method.
Videos
If the scope is "video", each object has the following properties:
refstringAn identifier that points to a video asset in Canva's backend.
options.onChange(event.read().save())functionSaves any changes made to the contents array and updates the user's design to reflect those changes.