Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: editor machine events #741

Merged
merged 4 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/editor/src/editor/Editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
export const PortableTextEditable = forwardRef<
Omit<HTMLDivElement, 'as' | 'onPaste' | 'onBeforeInput'>,
PortableTextEditableProps
>(function PortableTextEditable(props, forwardedRef) {

Check warning on line 136 in packages/editor/src/editor/Editable.tsx

View workflow job for this annotation

GitHub Actions / check-lint

[ReactCompilerBailout] Support value blocks (conditional, logical, optional chaining, etc) within a try/catch statement (@:637:8)
const {
hotkeys,
onBlur,
Expand Down Expand Up @@ -295,7 +295,7 @@
// 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 @@
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 @@
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 @@
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 @@
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
Loading