Skip to content

Commit

Permalink
docs: document raise action
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhg committed Jan 3, 2025
1 parent cb87912 commit a6d2f6f
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions apps/docs/src/content/docs/reference/behavior-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,56 @@ Additional actions
- delete.block
- delete.text
- effect
- raise
- select.previous block
- select.next block
- style.add
- style.remove
- text block.set
- text block.unset

## Raising Synthetic Events

Synthetic events can be raised using the `raise` action:

```tsx
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:

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

When raising and event rather than performing the action directly, the event is sent back into the editor, allowing other behaviors to respond:

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

However, be careful as this can can lead to infinite loops:

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

0 comments on commit a6d2f6f

Please sign in to comment.