Skip to content

Behavior API

Reference docs for the behavior API.

defineBehavior

Options Object

  • on (string): Internal editor event
  • guard (function or boolean): function accepts context and event, returns boolean
  • actions (array): function accepts context and event, returns array of actions

Example

const noLowerCaseA = defineBehavior({
on: 'insert.text',
guard: ({event, context}) => event.text === 'a',
actions: [({event, context}) => [{type: 'insert.text', text: 'A'}]],
})

Behavior Event types

Native event types

  • copy
  • paste
  • key.down
  • key.up

Synthetic event types

  • annotation.add
  • annotation.remove
  • blur
  • decorator.toggle
  • delete.backward
  • delete.forward
  • focus
  • insert.block object
  • insert.inline object
  • insert.break
  • insert.sort break
  • insert.text
  • list item.toggle
  • select
  • style.toggle

Behavior Actions

Actions as part of synthetic events

  • annotation.add
  • annotation.remove
  • blur
  • decorator.toggle
  • delete.backward
  • delete.forward
  • focus
  • insert.block object
  • insert.inline object
  • insert.break
  • insert.sort break
  • insert.text
  • list item.toggle
  • select
  • style.toggle

Additional actions

  • annotation.toggle
  • decorator.add
  • decorator.remove
  • insert.span
  • insert.text block
  • list item.add
  • list item.remove
  • move.block
  • move.block down
  • move.block up
  • noop
  • delete.block
  • delete.text
  • effect
  • raise
  • select.previous block
  • select.next block
  • style.add
  • style.remove
  • text block.set
  • text block.unset

raise

The raise action type is used to trigger synthetic events.

Properties:

  • type: raise
  • event: Behavior event object
const raisedUppercaseA = defineBehavior({
on: 'insert.text',
guard: ({event, context}) => event.text === 'a',
actions: [
({event, context}) => [
{type: 'raise', event: {type: 'insert.text', text: 'A'}},
],
],
})

The raise action also has a handy shorthand function. It accepts a behavior event object.

const raisedUppercaseA = defineBehavior({
on: 'insert.text',
guard: ({event, context}) => event.text === 'a',
actions: [({event, context}) => [raise({type: 'insert.text', text: 'A'})]],
})