@portabletext/editor 1.7.0 → 1.7.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.7.0",
3
+ "version": "1.7.1",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -43,7 +43,7 @@
43
43
  ],
44
44
  "dependencies": {
45
45
  "@portabletext/patches": "1.1.0",
46
- "@xstate/react": "^4.1.3",
46
+ "@xstate/react": "^5.0.0",
47
47
  "debug": "^4.3.4",
48
48
  "get-random-values-esm": "^1.0.2",
49
49
  "is-hotkey-esm": "^1.0.0",
@@ -54,15 +54,15 @@
54
54
  "slate-dom": "^0.111.0",
55
55
  "slate-react": "0.111.0",
56
56
  "use-effect-event": "^1.0.2",
57
- "xstate": "^5.18.2"
57
+ "xstate": "^5.19.0"
58
58
  },
59
59
  "devDependencies": {
60
60
  "@portabletext/toolkit": "^2.0.16",
61
- "@sanity/block-tools": "^3.63.0",
61
+ "@sanity/block-tools": "^3.64.0",
62
62
  "@sanity/diff-match-patch": "^3.1.1",
63
63
  "@sanity/pkg-utils": "^6.11.10",
64
- "@sanity/schema": "^3.63.0",
65
- "@sanity/types": "^3.63.0",
64
+ "@sanity/schema": "^3.64.0",
65
+ "@sanity/types": "^3.64.0",
66
66
  "@testing-library/jest-dom": "^6.6.3",
67
67
  "@testing-library/react": "^16.0.1",
68
68
  "@types/debug": "^4.1.5",
@@ -90,9 +90,9 @@
90
90
  "@sanity/gherkin-driver": "^0.0.1"
91
91
  },
92
92
  "peerDependencies": {
93
- "@sanity/block-tools": "^3.63.0",
94
- "@sanity/schema": "^3.63.0",
95
- "@sanity/types": "^3.63.0",
93
+ "@sanity/block-tools": "^3.64.0",
94
+ "@sanity/schema": "^3.64.0",
95
+ "@sanity/types": "^3.64.0",
96
96
  "react": "^16.9 || ^17 || ^18",
97
97
  "rxjs": "^7.8.1",
98
98
  "styled-components": "^6.1.13"
@@ -136,6 +136,7 @@ export class PortableTextEditor extends Component<
136
136
  if (props.editor) {
137
137
  const editor = props.editor as Editor
138
138
  this.editorActor = editor._internal.editorActor
139
+ this.slateEditor = editor._internal.slateEditor
139
140
  this.editorActor.start()
140
141
  this.schemaTypes = this.editorActor.getSnapshot().context.schema
141
142
  } else {
@@ -163,6 +164,10 @@ export class PortableTextEditor extends Component<
163
164
  })
164
165
  this.editorActor.start()
165
166
 
167
+ this.slateEditor = createSlateEditor({
168
+ editorActor: this.editorActor,
169
+ })
170
+
166
171
  if (props.readOnly) {
167
172
  this.editorActor.send({
168
173
  type: 'toggle readOnly',
@@ -179,9 +184,6 @@ export class PortableTextEditor extends Component<
179
184
  })
180
185
  }
181
186
  }
182
- this.slateEditor = createSlateEditor({
183
- editorActor: this.editorActor,
184
- })
185
187
  this.editable = createEditableAPI(
186
188
  this.slateEditor.instance,
187
189
  this.editorActor,
@@ -230,10 +232,6 @@ export class PortableTextEditor extends Component<
230
232
  }
231
233
  }
232
234
 
233
- componentWillUnmount(): void {
234
- this.slateEditor.destroy()
235
- }
236
-
237
235
  public setEditable = (editable: EditableAPI) => {
238
236
  this.editable = {...this.editable, ...editable}
239
237
  }
@@ -12,10 +12,12 @@ type SlateEditorConfig = {
12
12
  editorActor: EditorActor
13
13
  }
14
14
 
15
+ /**
16
+ * @internal
17
+ */
15
18
  export type SlateEditor = {
16
19
  instance: PortableTextSlateEditor
17
20
  initialValue: Array<Descendant>
18
- destroy: () => void
19
21
  }
20
22
 
21
23
  const slateEditors = new WeakMap<EditorActor, SlateEditor>()
@@ -45,12 +47,8 @@ export function createSlateEditor(config: SlateEditorConfig): SlateEditor {
45
47
  unsubscriptions.push(subscription())
46
48
  }
47
49
 
48
- const initialValue = [instance.pteCreateTextBlock({decorators: []})]
49
-
50
- const slateEditor: SlateEditor = {
51
- instance,
52
- initialValue,
53
- destroy: () => {
50
+ config.editorActor.subscribe((snapshot) => {
51
+ if (snapshot.status !== 'active') {
54
52
  debug('Destroying Slate editor')
55
53
  instance.destroy()
56
54
  for (const unsubscribe of unsubscriptions) {
@@ -58,7 +56,14 @@ export function createSlateEditor(config: SlateEditorConfig): SlateEditor {
58
56
  }
59
57
  subscriptions = []
60
58
  unsubscriptions = []
61
- },
59
+ }
60
+ })
61
+
62
+ const initialValue = [instance.pteCreateTextBlock({decorators: []})]
63
+
64
+ const slateEditor: SlateEditor = {
65
+ instance,
66
+ initialValue,
62
67
  }
63
68
 
64
69
  slateEditors.set(config.editorActor, slateEditor)
@@ -7,6 +7,7 @@ import {useActorRef, useSelector} from '@xstate/react'
7
7
  import {getPortableTextMemberSchemaTypes} from '../utils/getPortableTextMemberSchemaTypes'
8
8
  import {compileType} from '../utils/schema'
9
9
  import type {Behavior, PickFromUnion} from './behavior/behavior.types'
10
+ import {createSlateEditor, type SlateEditor} from './create-slate-editor'
10
11
  import {compileSchemaDefinition, type SchemaDefinition} from './define-schema'
11
12
  import {
12
13
  editorMachine,
@@ -54,6 +55,7 @@ export type Editor = {
54
55
  readOnly: boolean
55
56
  _internal: {
56
57
  editorActor: EditorActor
58
+ slateEditor: SlateEditor
57
59
  }
58
60
  }
59
61
 
@@ -74,6 +76,7 @@ export function useEditor(config: EditorConfig): Editor {
74
76
  ),
75
77
  },
76
78
  })
79
+ const slateEditor = createSlateEditor({editorActor})
77
80
  const readOnly = useSelector(editorActor, (s) => s.context.readOnly)
78
81
 
79
82
  return {
@@ -84,6 +87,7 @@ export function useEditor(config: EditorConfig): Editor {
84
87
  readOnly,
85
88
  _internal: {
86
89
  editorActor,
90
+ slateEditor,
87
91
  },
88
92
  }
89
93
  }
package/src/index.ts CHANGED
@@ -15,6 +15,7 @@ export {
15
15
  type BehaviorGuard,
16
16
  type PickFromUnion,
17
17
  } from './editor/behavior/behavior.types'
18
+ export type {SlateEditor} from './editor/create-slate-editor'
18
19
  export {
19
20
  defineSchema,
20
21
  type BaseDefinition,