Skip to content

Commit

Permalink
refactor: editor machine events
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhg committed Jan 28, 2025
1 parent 099279c commit 28fb834
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 229 deletions.
12 changes: 6 additions & 6 deletions packages/editor/src/editor/Editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export const PortableTextEditable = forwardRef<
// The selection is usually automatically emitted to change$ by the withPortableTextSelections plugin whenever there is a set_selection operation applied.
if (!slateEditor.operations.some((o) => o.type === 'set_selection')) {
editorActor.send({
type: 'selection',
type: 'notify.selection',
selection: normalizedSelection,
})
}
Expand Down Expand Up @@ -465,7 +465,7 @@ export const PortableTextEditable = forwardRef<
event.preventDefault()

// Resolve it as promise (can be either async promise or sync return value)
editorActor.send({type: 'loading'})
editorActor.send({type: 'notify.loading'})

Promise.resolve(onPasteResult)
.then((result) => {
Expand Down Expand Up @@ -494,7 +494,7 @@ export const PortableTextEditable = forwardRef<
return error
})
.finally(() => {
editorActor.send({type: 'done loading'})
editorActor.send({type: 'notify.done loading'})
})
} else if (event.nativeEvent.clipboardData) {
editorActor.send({
Expand Down Expand Up @@ -525,12 +525,12 @@ export const PortableTextEditable = forwardRef<
Transforms.select(slateEditor, Editor.start(slateEditor, []))
slateEditor.onChange()
}
editorActor.send({type: 'focused', event})
editorActor.send({type: 'notify.focused', event})
const newSelection = PortableTextEditor.getSelection(portableTextEditor)
// If the selection is the same, emit it explicitly here as there is no actual onChange event triggered.
if (selection === newSelection) {
editorActor.send({
type: 'selection',
type: 'notify.selection',
selection,
})
}
Expand Down Expand Up @@ -581,7 +581,7 @@ export const PortableTextEditable = forwardRef<
onBlur(event)
}
if (!event.isPropagationStopped()) {
editorActor.send({type: 'blurred', event})
editorActor.send({type: 'notify.blurred', event})
}
},
[editorActor, onBlur],
Expand Down
17 changes: 16 additions & 1 deletion packages/editor/src/editor/components/Synchronizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,22 @@ export function Synchronizer(props: SynchronizerProps) {

useEffect(() => {
const subscription = syncActorRef.on('*', (event) => {
props.editorActor.send(event)
switch (event.type) {
case 'invalid value':
props.editorActor.send({
...event,
type: 'notify.invalid value',
})
break
case 'value changed':
props.editorActor.send({
...event,
type: 'notify.value changed',
})
break
default:
props.editorActor.send(event)
}
})

return () => {
Expand Down
56 changes: 8 additions & 48 deletions packages/editor/src/editor/create-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import {
type EventObject,
type Snapshot,
} from 'xstate'
import type {Behavior, CustomBehaviorEvent} from '../behaviors/behavior.types'
import type {
Behavior,
CustomBehaviorEvent,
SyntheticBehaviorEvent,
} from '../behaviors/behavior.types'
import {coreConverters} from '../converters/converters.core'
import {compileType} from '../internal-utils/schema'
import type {PickFromUnion} from '../type-utils'
import type {EditableAPI} from '../types/editor'
import {createEditorSchema} from './create-editor-schema'
import {createSlateEditor, type SlateEditor} from './create-slate-editor'
Expand All @@ -23,7 +26,7 @@ import {
editorMachine,
type EditorActor,
type EditorEmittedEvent,
type InternalEditorEvent,
type ExternalEditorEvent,
} from './editor-machine'
import {getEditorSnapshot} from './editor-selector'
import type {EditorSnapshot} from './editor-snapshot'
Expand Down Expand Up @@ -60,51 +63,8 @@ export type EditorConfig = {
* @public
*/
export type EditorEvent =
| PickFromUnion<
InternalEditorEvent,
'type',
| 'annotation.add'
| 'annotation.remove'
| 'annotation.toggle'
| 'block.set'
| 'block.unset'
| 'blur'
| 'data transfer.set'
| 'decorator.add'
| 'decorator.remove'
| 'decorator.toggle'
| 'delete.block'
| 'delete.text'
| 'deserialization.failure'
| 'deserialization.success'
| 'focus'
| 'insert.block'
| 'insert.block object'
| 'insert.inline object'
| 'insert.span'
| 'insert.text block'
| 'list item.add'
| 'list item.remove'
| 'list item.toggle'
| 'move.block'
| 'move.block down'
| 'move.block up'
| 'select'
| 'select.next block'
| 'select.previous block'
| 'serialization.failure'
| 'serialization.success'
| 'style.add'
| 'style.remove'
| 'style.toggle'
| 'text block.set'
| 'text block.unset'
| 'patches'
| 'update behaviors'
| 'update key generator'
| 'update readOnly'
| 'update value'
>
| ExternalEditorEvent
| SyntheticBehaviorEvent
| CustomBehaviorEvent

/**
Expand Down
Loading

0 comments on commit 28fb834

Please sign in to comment.