@portabletext/editor 1.6.0 → 1.7.0

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.
Files changed (37) hide show
  1. package/README.md +5 -0
  2. package/lib/index.d.mts +3317 -3488
  3. package/lib/index.d.ts +3317 -3488
  4. package/lib/index.esm.js +4337 -4089
  5. package/lib/index.esm.js.map +1 -1
  6. package/lib/index.js +4325 -4077
  7. package/lib/index.js.map +1 -1
  8. package/lib/index.mjs +4337 -4089
  9. package/lib/index.mjs.map +1 -1
  10. package/package.json +10 -11
  11. package/src/editor/Editable.tsx +26 -28
  12. package/src/editor/PortableTextEditor.tsx +90 -66
  13. package/src/editor/behavior/behavior.action.insert-break.ts +12 -2
  14. package/src/editor/behavior/behavior.actions.ts +51 -11
  15. package/src/editor/behavior/behavior.types.ts +23 -0
  16. package/src/editor/components/Synchronizer.tsx +11 -4
  17. package/src/editor/create-slate-editor.tsx +67 -0
  18. package/src/editor/editor-machine.ts +58 -8
  19. package/src/editor/key-generator.ts +30 -1
  20. package/src/editor/plugins/create-with-event-listeners.ts +62 -1
  21. package/src/editor/plugins/createWithEditableAPI.ts +800 -728
  22. package/src/editor/plugins/createWithMaxBlocks.ts +7 -2
  23. package/src/editor/plugins/createWithPatches.ts +4 -4
  24. package/src/editor/plugins/createWithPlaceholderBlock.ts +8 -3
  25. package/src/editor/plugins/createWithPortableTextMarkModel.ts +3 -4
  26. package/src/editor/plugins/createWithUndoRedo.ts +6 -7
  27. package/src/editor/plugins/createWithUtils.ts +2 -8
  28. package/src/editor/plugins/{index.ts → with-plugins.ts} +22 -79
  29. package/src/editor/use-editor.ts +46 -14
  30. package/src/editor/withSyncRangeDecorations.ts +20 -0
  31. package/src/index.ts +9 -1
  32. package/src/types/editor.ts +0 -1
  33. package/src/types/options.ts +1 -3
  34. package/src/utils/__tests__/operationToPatches.test.ts +7 -14
  35. package/src/utils/__tests__/patchToOperations.test.ts +4 -7
  36. package/src/editor/components/SlateContainer.tsx +0 -79
  37. package/src/editor/hooks/usePortableTextReadOnly.ts +0 -20
@@ -1,79 +0,0 @@
1
- import {useEffect, useMemo, useState, type PropsWithChildren} from 'react'
2
- import {createEditor} from 'slate'
3
- import {Slate, withReact} from 'slate-react'
4
- import {debugWithName} from '../../utils/debug'
5
- import {KEY_TO_SLATE_ELEMENT, KEY_TO_VALUE_ELEMENT} from '../../utils/weakMaps'
6
- import type {EditorActor} from '../editor-machine'
7
- import {withPlugins} from '../plugins'
8
- import type {PortableTextEditor} from '../PortableTextEditor'
9
-
10
- const debug = debugWithName('component:PortableTextEditor:SlateContainer')
11
-
12
- /**
13
- * @internal
14
- */
15
- export interface SlateContainerProps extends PropsWithChildren {
16
- editorActor: EditorActor
17
- maxBlocks: number | undefined
18
- portableTextEditor: PortableTextEditor
19
- readOnly: boolean
20
- }
21
-
22
- /**
23
- * Sets up and encapsulates the Slate instance
24
- * @internal
25
- */
26
- export function SlateContainer(props: SlateContainerProps) {
27
- const {editorActor, portableTextEditor, readOnly, maxBlocks} = props
28
-
29
- // Create the slate instance, using `useState` ensures setup is only run once, initially
30
- const [[slateEditor, subscribe]] = useState(() => {
31
- debug('Creating new Slate editor instance')
32
- const {editor, subscribe: _sub} = withPlugins(withReact(createEditor()), {
33
- editorActor,
34
- maxBlocks,
35
- portableTextEditor,
36
- readOnly,
37
- })
38
- KEY_TO_VALUE_ELEMENT.set(editor, {})
39
- KEY_TO_SLATE_ELEMENT.set(editor, {})
40
- return [editor, _sub] as const
41
- })
42
-
43
- useEffect(() => {
44
- const unsubscribe = subscribe()
45
- return () => {
46
- unsubscribe()
47
- }
48
- }, [subscribe])
49
-
50
- // Update the slate instance when plugin dependent props change.
51
- useEffect(() => {
52
- debug('Re-initializing plugin chain')
53
- withPlugins(slateEditor, {
54
- editorActor,
55
- maxBlocks,
56
- portableTextEditor,
57
- readOnly,
58
- })
59
- }, [editorActor, portableTextEditor, maxBlocks, readOnly, slateEditor])
60
-
61
- const initialValue = useMemo(() => {
62
- return [slateEditor.pteCreateTextBlock({decorators: []})]
63
- }, [slateEditor])
64
-
65
- useEffect(() => {
66
- return () => {
67
- debug('Destroying Slate editor')
68
- slateEditor.destroy()
69
- }
70
- }, [slateEditor])
71
-
72
- return (
73
- <Slate editor={slateEditor} initialValue={initialValue}>
74
- {props.children}
75
- </Slate>
76
- )
77
- }
78
-
79
- SlateContainer.displayName = 'SlateContainer'
@@ -1,20 +0,0 @@
1
- import {createContext, useContext} from 'react'
2
-
3
- /**
4
- * A React context for sharing the editor's readOnly status.
5
- */
6
- export const PortableTextEditorReadOnlyContext = createContext<boolean>(false)
7
-
8
- /**
9
- * Get the current editor selection from the React context.
10
- */
11
- export const usePortableTextEditorReadOnlyStatus = (): boolean => {
12
- const readOnly = useContext(PortableTextEditorReadOnlyContext)
13
-
14
- if (readOnly === undefined) {
15
- throw new Error(
16
- `The \`usePortableTextEditorReadOnly\` hook must be used inside the <PortableTextEditor> component's context.`,
17
- )
18
- }
19
- return readOnly
20
- }