Skip to content

Commit

Permalink
chore(playground): use store instead of patches$
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhg committed Sep 25, 2024
1 parent 3fbbdc4 commit 5d0007a
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions apps/playground/src/editor.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
BlockDecoratorRenderProps,
BlockStyleRenderProps,
Patch,
createEditorStore,
PortableTextEditable,
PortableTextEditor,
RenderAnnotationFunction,
Expand All @@ -12,13 +12,11 @@ import {
RenderPlaceholderFunction,
RenderStyleFunction,
} from '@portabletext/editor'
import {PortableTextBlock} from '@sanity/types'
import {useSelector} from '@xstate/react'
import {CopyIcon, ImageIcon, TrashIcon} from 'lucide-react'
import {useEffect, useMemo, useState} from 'react'
import {useEffect, useState} from 'react'
import {TooltipTrigger} from 'react-aria-components'
import {reverse} from 'remeda'
import {Subject} from 'rxjs'
import {Button} from './components/button'
import {ErrorBoundary} from './components/error-boundary'
import {ErrorScreen} from './components/error-screen'
Expand All @@ -42,6 +40,7 @@ import {SelectionPreview} from './selection-preview'
import {wait} from './wait'

export function Editor(props: {editorRef: EditorActorRef}) {
const [editorStore] = useState(() => createEditorStore())
const showingPatchesPreview = useSelector(props.editorRef, (s) =>
s.matches({'patches preview': 'shown'}),
)
Expand All @@ -56,17 +55,11 @@ export function Editor(props: {editorRef: EditorActorRef}) {
const patchesReceived = useSelector(props.editorRef, (s) =>
reverse(s.context.patchesReceived),
)
const patches$ = useMemo(
() =>
new Subject<{
patches: Array<Patch>
snapshot: Array<PortableTextBlock> | undefined
}>(),
[],
)

useEffect(() => {
const subscription = props.editorRef.on('patches', (event) => {
patches$.next({
editorStore.send({
type: 'patches',
patches: event.patches,
snapshot: event.snapshot,
})
Expand All @@ -75,7 +68,8 @@ export function Editor(props: {editorRef: EditorActorRef}) {
return () => {
subscription.unsubscribe()
}
}, [patches$, props.editorRef])
}, [props.editorRef, editorStore])

const [loading, setLoading] = useState(false)

return (
Expand All @@ -90,7 +84,7 @@ export function Editor(props: {editorRef: EditorActorRef}) {
>
<PortableTextEditor
value={value}
patches$={patches$}
store={editorStore}
onChange={(change) => {
if (change.type === 'mutation') {
props.editorRef.send(change)
Expand Down

0 comments on commit 5d0007a

Please sign in to comment.