Skip to content

Commit

Permalink
fix(useEditor): allow configure of initial readOnly and remove Editor…
Browse files Browse the repository at this point in the history
….readOnly
  • Loading branch information
christianhg committed Nov 20, 2024
1 parent 2c2a737 commit edcee2c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
7 changes: 6 additions & 1 deletion apps/playground/src/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function Editor(props: {editorRef: EditorActorRef}) {
props.editorRef,
(s) => s.context.keyGenerator,
)
const [readOnly, setReadOnly] = useState(false)
const editor = useEditor({
behaviors: [
...coreBehaviors,
Expand Down Expand Up @@ -85,6 +86,7 @@ export function Editor(props: {editorRef: EditorActorRef}) {
}),
],
keyGenerator,
readOnly,
schemaDefinition,
})
const patchesReceived = useSelector(props.editorRef, (s) =>
Expand Down Expand Up @@ -114,6 +116,9 @@ export function Editor(props: {editorRef: EditorActorRef}) {
if (event.type === 'done loading') {
setLoading(false)
}
if (event.type === 'readOnly toggled') {
setReadOnly(event.readOnly)
}
})

return () => {
Expand Down Expand Up @@ -197,7 +202,7 @@ export function Editor(props: {editorRef: EditorActorRef}) {
<Tooltip>Remove editor</Tooltip>
</TooltipTrigger>
<Switch
isSelected={editor.readOnly}
isSelected={readOnly}
onChange={() => {
editor.send({type: 'toggle readOnly'})
}}
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/editor/PortableTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ export function RouteEventsToChanges(props: {
case 'annotation.toggle':
case 'focus':
case 'patches':
case 'readOnly toggled':
break
default:
handleChange(event)
Expand Down
9 changes: 9 additions & 0 deletions packages/editor/src/editor/behavior/behavior.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,12 @@ export type PickFromUnion<
TTagKey extends keyof TUnion,
TPickedTags extends TUnion[TTagKey],
> = TUnion extends Record<TTagKey, TPickedTags> ? TUnion : never

/**
* @alpha
*/
export type OmitFromUnion<
TUnion,
TTagKey extends keyof TUnion,
TOmittedTags extends TUnion[TTagKey],
> = TUnion extends Record<TTagKey, TOmittedTags> ? never : TUnion
15 changes: 12 additions & 3 deletions packages/editor/src/editor/editor-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type {
BehaviorActionIntend,
BehaviorContext,
BehaviorEvent,
OmitFromUnion,
PickFromUnion,
} from './behavior/behavior.types'

Expand Down Expand Up @@ -95,7 +96,7 @@ export type InternalEditorEvent =
type: 'update maxBlocks'
maxBlocks: number | undefined
}
| InternalEditorEmittedEvent
| OmitFromUnion<InternalEditorEmittedEvent, 'type', 'readOnly toggled'>

/**
* @internal
Expand Down Expand Up @@ -129,6 +130,7 @@ export type InternalEditorEmittedEvent =
| {type: 'focused'; event: FocusEvent<HTMLDivElement, Element>}
| {type: 'loading'}
| {type: 'done loading'}
| {type: 'readOnly toggled'; readOnly: boolean}
| PickFromUnion<
BehaviorEvent,
'type',
Expand All @@ -154,6 +156,7 @@ export const editorMachine = setup({
input: {} as {
behaviors?: Array<Behavior>
keyGenerator: () => string
readOnly?: boolean
schema: PortableTextMemberSchemaTypes
value?: Array<PortableTextBlock>
},
Expand Down Expand Up @@ -296,7 +299,7 @@ export const editorMachine = setup({
keyGenerator: input.keyGenerator,
pendingEvents: [],
schema: input.schema,
readOnly: false,
readOnly: input.readOnly ?? false,
maxBlocks: undefined,
value: input.value,
}),
Expand Down Expand Up @@ -332,7 +335,13 @@ export const editorMachine = setup({
'update schema': {actions: 'assign schema'},
'update value': {actions: assign({value: ({event}) => event.value})},
'toggle readOnly': {
actions: assign({readOnly: ({context}) => !context.readOnly}),
actions: [
assign({readOnly: ({context}) => !context.readOnly}),
emit(({context}) => ({
type: 'readOnly toggled',
readOnly: context.readOnly,
})),
],
},
'update maxBlocks': {
actions: assign({maxBlocks: ({event}) => event.maxBlocks}),
Expand Down
7 changes: 3 additions & 4 deletions packages/editor/src/editor/use-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
ArraySchemaType,
PortableTextBlock,
} from '@sanity/types'
import {useActorRef, useSelector} from '@xstate/react'
import {useActorRef} from '@xstate/react'
import {getPortableTextMemberSchemaTypes} from '../utils/getPortableTextMemberSchemaTypes'
import {compileType} from '../utils/schema'
import type {Behavior, PickFromUnion} from './behavior/behavior.types'
Expand All @@ -22,6 +22,7 @@ import {defaultKeyGenerator} from './key-generator'
export type EditorConfig = {
behaviors?: Array<Behavior>
keyGenerator?: () => string
readOnly?: boolean
initialValue?: Array<PortableTextBlock>
} & (
| {
Expand Down Expand Up @@ -54,7 +55,6 @@ export type EditorEvent = PickFromUnion<
export type Editor = {
send: (event: EditorEvent) => void
on: EditorActor['on']
readOnly: boolean
_internal: {
editorActor: EditorActor
slateEditor: SlateEditor
Expand All @@ -69,6 +69,7 @@ export function useEditor(config: EditorConfig): Editor {
input: {
behaviors: config.behaviors,
keyGenerator: config.keyGenerator ?? defaultKeyGenerator,
readOnly: config.readOnly,
schema: config.schemaDefinition
? compileSchemaDefinition(config.schemaDefinition)
: getPortableTextMemberSchemaTypes(
Expand All @@ -80,14 +81,12 @@ export function useEditor(config: EditorConfig): Editor {
},
})
const slateEditor = createSlateEditor({editorActor})
const readOnly = useSelector(editorActor, (s) => s.context.readOnly)

return {
send: (event) => {
editorActor.send(event)
},
on: (event, listener) => editorActor.on(event, listener),
readOnly,
_internal: {
editorActor,
slateEditor,
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export {
type BehaviorContext,
type BehaviorEvent,
type BehaviorGuard,
type OmitFromUnion,
type PickFromUnion,
} from './editor/behavior/behavior.types'
export type {SlateEditor} from './editor/create-slate-editor'
Expand Down

0 comments on commit edcee2c

Please sign in to comment.