Skip to content

Commit

Permalink
fix(behavior): make EditorSnapshot.selection nullable
Browse files Browse the repository at this point in the history
This is a bit annoying, but more correct. And the `selection` is already
nullable in the selectors.
  • Loading branch information
christianhg committed Dec 9, 2024
1 parent d65459e commit a69f570
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 49 deletions.
4 changes: 2 additions & 2 deletions packages/editor/src/behaviors/behavior.core.lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const clearListOnBackspace = defineBehavior({

const atTheBeginningOfBLock =
focusTextBlock.node.children[0]._key === focusSpan.node._key &&
context.selection.focus.offset === 0
context.selection?.focus.offset === 0

if (atTheBeginningOfBLock && focusTextBlock.node.level === 1) {
return {focusTextBlock}
Expand Down Expand Up @@ -57,7 +57,7 @@ const unindentListOnBackspace = defineBehavior({

const atTheBeginningOfBLock =
focusTextBlock.node.children[0]._key === focusSpan.node._key &&
context.selection.focus.offset === 0
context.selection?.focus.offset === 0

if (
atTheBeginningOfBLock &&
Expand Down
8 changes: 4 additions & 4 deletions packages/editor/src/behaviors/behavior.markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
'children',
{_key: focusSpan.node._key},
],
offset: context.selection.focus.offset,
offset: context.selection?.focus.offset ?? 0,
},
})

Expand Down Expand Up @@ -245,7 +245,7 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
'children',
{_key: focusSpan.node._key},
],
offset: context.selection.focus.offset,
offset: context.selection?.focus.offset ?? 0,
},
})

Expand Down Expand Up @@ -319,7 +319,7 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {

const atTheBeginningOfBLock =
focusTextBlock.node.children[0]._key === focusSpan.node._key &&
context.selection.focus.offset === 0
context.selection?.focus.offset === 0

const defaultStyle = config.defaultStyle?.(context)

Expand Down Expand Up @@ -368,7 +368,7 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
'children',
{_key: focusSpan.node._key},
],
offset: context.selection.focus.offset,
offset: context.selection?.focus.offset ?? 0,
},
})

Expand Down
23 changes: 3 additions & 20 deletions packages/editor/src/editor/editor-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export const editorMachine = setup({
schema: EditorSchema
readOnly: boolean
maxBlocks: number | undefined
selection: NonNullable<EditorSelection> | undefined
selection: EditorSelection
value: Array<PortableTextBlock> | undefined
},
events: {} as InternalEditorEvent,
Expand Down Expand Up @@ -275,23 +275,6 @@ export const editorMachine = setup({
context.schema,
)

if (!selection) {
console.warn(
`Unable to handle event ${event.type} due to missing selection`,
)

if (!defaultAction) {
return
}

enqueue.raise({
type: 'behavior action intends',
editor: event.editor,
actionIntends: [defaultAction],
})
return
}

const editorContext = {
keyGenerator: context.keyGenerator,
schema: context.schema,
Expand Down Expand Up @@ -361,7 +344,7 @@ export const editorMachine = setup({
keyGenerator: input.keyGenerator,
pendingEvents: [],
schema: input.schema,
selection: undefined,
selection: null,
readOnly: input.readOnly ?? false,
maxBlocks: input.maxBlocks,
value: input.value,
Expand Down Expand Up @@ -410,7 +393,7 @@ export const editorMachine = setup({
'error': {actions: emit(({event}) => event)},
'selection': {
actions: [
assign({selection: ({event}) => event.selection ?? undefined}),
assign({selection: ({event}) => event.selection}),
emit(({event}) => event),
],
},
Expand Down
14 changes: 1 addition & 13 deletions packages/editor/src/editor/editor-selector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {useSelector} from '@xstate/react'
import type {EditorSelection} from '../types/editor'
import type {Editor} from './create-editor'
import type {EditorSnapshot} from './editor-snapshot'
import {getValue} from './get-value'
Expand All @@ -11,18 +10,7 @@ function defaultCompare<T>(a: T, b: T) {
/**
* @alpha
*/
export type EditorSelectorSnapshot = {
context: Omit<EditorSnapshot['context'], 'selection'> & {
selection?: NonNullable<EditorSelection>
}
}

/**
* @alpha
*/
export type EditorSelector<TSelected> = (
snapshot: EditorSelectorSnapshot,
) => TSelected
export type EditorSelector<TSelected> = (snapshot: EditorSnapshot) => TSelected

/**
* @alpha
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/editor/editor-snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {EditorSchema} from './define-schema'
export type EditorContext = {
keyGenerator: () => string
schema: EditorSchema
selection: NonNullable<EditorSelection>
selection: EditorSelection
value: Array<PortableTextBlock>
}

Expand Down
6 changes: 1 addition & 5 deletions packages/editor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ export {
useEditor,
type EditorProviderProps,
} from './editor/editor-provider'
export {
useEditorSelector,
type EditorSelector,
type EditorSelectorSnapshot,
} from './editor/editor-selector'
export {useEditorSelector, type EditorSelector} from './editor/editor-selector'
export type {EditorContext, EditorSnapshot} from './editor/editor-snapshot'
export {usePortableTextEditor} from './editor/hooks/usePortableTextEditor'
export {usePortableTextEditorSelection} from './editor/hooks/usePortableTextEditorSelection'
Expand Down
5 changes: 1 addition & 4 deletions packages/editor/src/selectors/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
export type {
EditorSelector,
EditorSelectorSnapshot,
} from '../editor/editor-selector'
export type {EditorSelector} from '../editor/editor-selector'
export type {EditorContext, EditorSnapshot} from '../editor/editor-snapshot'
export type {
EditorSelection,
Expand Down

0 comments on commit a69f570

Please sign in to comment.