Behavior Recipes
Below are some common behavior examples. You can also find a list of core behaviors on GitHub.
To add these to your editor, first import defineBehavior
and optionally the coreBehaviors
.
Then, register the behavior with the EditorProvider
. Spread the core behaviors if you want to include them.
Read more about using behaviors and building your own with these guides:
Log inserted text
Send and effect
type action along with an effect
to perform side effects.
Auto-close parenthesis
You can write behaviors to auto-close common paired characters. This example closes parenthesis, and then moves the cursor in between the two characters. This logic can expand to cover more sets.
Emoji Picker
Using a factory to create behaviors, a state machine, and custom components results in an emoji picker that triggers when you insert :
.
Test it out in the Playground.
You can leverage the behavior by importing createEmojiPickerBehaviors
from @portabletext/editor/behaviors
. See the editor source above for a usage example.
Raise a synthetic event
Sometimes you want to trigger a synthetic event from within an action. This sends the event back to the editor, where the editor treats it like any other event.
The raise
action also has a shorthand function: