@portabletext/editor 1.49.8 → 1.49.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.cjs +4190 -4086
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +4 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +4180 -4076
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/editor/Editable.tsx +1 -0
- package/src/editor/PortableTextEditor.tsx +68 -33
- package/src/editor/__tests__/PortableTextEditor.test.tsx +1 -0
- package/src/editor/create-editor.ts +104 -80
- package/src/editor/create-slate-editor.tsx +4 -21
- package/src/editor/editor-provider.tsx +43 -23
- package/src/editor/mutation-machine.ts +3 -0
- package/src/editor/sync-machine.ts +3 -0
- package/src/internal-utils/applyPatch.ts +21 -4
- package/src/internal-utils/stop-actor.ts +43 -0
- package/src/internal-utils/use-constant.ts +15 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copy/pasted from https://github.com/statelyai/xstate/blob/main/packages/xstate-react/src/stopRootWithRehydration.ts
|
|
3
|
+
* and renamed to `stopActor`
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {AnyActorRef, Snapshot} from 'xstate'
|
|
7
|
+
|
|
8
|
+
const forEachActor = (
|
|
9
|
+
actorRef: AnyActorRef,
|
|
10
|
+
callback: (ref: AnyActorRef) => void,
|
|
11
|
+
) => {
|
|
12
|
+
callback(actorRef)
|
|
13
|
+
const children = actorRef.getSnapshot().children
|
|
14
|
+
if (children) {
|
|
15
|
+
Object.values(children).forEach((child) => {
|
|
16
|
+
forEachActor(child as AnyActorRef, callback)
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function stopActor(actorRef: AnyActorRef) {
|
|
22
|
+
// persist snapshot here in a custom way allows us to persist inline actors and to preserve actor references
|
|
23
|
+
// we do it to avoid setState in useEffect when the effect gets "reconnected"
|
|
24
|
+
// this currently only happens in Strict Effects but it simulates the Offscreen aka Activity API
|
|
25
|
+
// it also just allows us to end up with a somewhat more predictable behavior for the users
|
|
26
|
+
const persistedSnapshots: Array<[AnyActorRef, Snapshot<unknown>]> = []
|
|
27
|
+
forEachActor(actorRef, (ref) => {
|
|
28
|
+
persistedSnapshots.push([ref, ref.getSnapshot()])
|
|
29
|
+
// muting observers allow us to avoid `useSelector` from being notified about the stopped snapshot
|
|
30
|
+
// React reconnects its subscribers (from the useSyncExternalStore) on its own
|
|
31
|
+
// and userland subscibers should basically always do the same anyway
|
|
32
|
+
// as each subscription should have its own cleanup logic and that should be called each such reconnect
|
|
33
|
+
;(ref as any).observers = new Set()
|
|
34
|
+
})
|
|
35
|
+
const systemSnapshot = actorRef.system.getSnapshot?.()
|
|
36
|
+
|
|
37
|
+
actorRef.stop()
|
|
38
|
+
;(actorRef.system as any)._snapshot = systemSnapshot
|
|
39
|
+
persistedSnapshots.forEach(([ref, snapshot]) => {
|
|
40
|
+
;(ref as any)._processingStatus = 0
|
|
41
|
+
;(ref as any)._snapshot = snapshot
|
|
42
|
+
})
|
|
43
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
|
|
3
|
+
type ConstantRef<TConstant> = {constant: TConstant}
|
|
4
|
+
|
|
5
|
+
export default function useConstant<TConstant>(
|
|
6
|
+
factory: () => TConstant,
|
|
7
|
+
): TConstant {
|
|
8
|
+
const ref = React.useRef<ConstantRef<TConstant>>(null)
|
|
9
|
+
|
|
10
|
+
if (!ref.current) {
|
|
11
|
+
ref.current = {constant: factory()}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return ref.current.constant
|
|
15
|
+
}
|