@portabletext/editor 1.10.0 → 1.10.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.10.0",
3
+ "version": "1.10.1",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -94,17 +94,17 @@ export type PortableTextEditorProps<
94
94
  * Whether or not the editor should be in read-only mode
95
95
  */
96
96
  readOnly?: boolean
97
- }) & {
98
- /**
99
- * The current value of the portable text field
100
- */
101
- value?: PortableTextBlock[]
102
-
103
- /**
104
- * A ref to the editor instance
105
- */
106
- editorRef?: MutableRefObject<PortableTextEditor | null>
107
- }
97
+
98
+ /**
99
+ * The current value of the portable text field
100
+ */
101
+ value?: PortableTextBlock[]
102
+
103
+ /**
104
+ * A ref to the editor instance
105
+ */
106
+ editorRef?: MutableRefObject<PortableTextEditor | null>
107
+ }) & {}
108
108
  >
109
109
 
110
110
  /**
@@ -160,6 +160,7 @@ export class PortableTextEditor extends Component<
160
160
  input: {
161
161
  keyGenerator: props.keyGenerator || defaultKeyGenerator,
162
162
  schema: this.schemaTypes,
163
+ value: props.value,
163
164
  },
164
165
  })
165
166
  this.editorActor.start()
@@ -225,10 +226,20 @@ export class PortableTextEditor extends Component<
225
226
  : Number.parseInt(this.props.maxBlocks.toString(), 10),
226
227
  })
227
228
  }
228
- }
229
229
 
230
- if (this.props.editorRef !== prevProps.editorRef && this.props.editorRef) {
231
- this.props.editorRef.current = this
230
+ if (this.props.value !== prevProps.value) {
231
+ this.editorActor.send({
232
+ type: 'update value',
233
+ value: this.props.value,
234
+ })
235
+ }
236
+
237
+ if (
238
+ this.props.editorRef !== prevProps.editorRef &&
239
+ this.props.editorRef
240
+ ) {
241
+ this.props.editorRef.current = this
242
+ }
232
243
  }
233
244
  }
234
245
 
@@ -279,7 +290,6 @@ export class PortableTextEditor extends Component<
279
290
  */
280
291
  this.change$.next(change)
281
292
  }}
282
- value={this.props.value}
283
293
  />
284
294
  {this.props.children}
285
295
  </PortableTextEditorSelectionProvider>
@@ -27,7 +27,6 @@ export interface SynchronizerProps {
27
27
  editorActor: EditorActor
28
28
  getValue: () => Array<PortableTextBlock> | undefined
29
29
  onChange: (change: EditorChange) => void
30
- value: Array<PortableTextBlock> | undefined
31
30
  }
32
31
 
33
32
  /**
@@ -37,7 +36,8 @@ export interface SynchronizerProps {
37
36
  export function Synchronizer(props: SynchronizerProps) {
38
37
  const portableTextEditor = usePortableTextEditor()
39
38
  const readOnly = useSelector(props.editorActor, (s) => s.context.readOnly)
40
- const {editorActor, getValue, onChange, value} = props
39
+ const value = useSelector(props.editorActor, (s) => s.context.value)
40
+ const {editorActor, getValue, onChange} = props
41
41
  const pendingPatches = useRef<Patch[]>([])
42
42
 
43
43
  const syncValue = useSyncValue({
@@ -46,12 +46,16 @@ const networkLogic = fromCallback(({sendBack}) => {
46
46
  sendBack({type: 'offline'})
47
47
  }
48
48
 
49
- window.addEventListener('online', onlineHandler)
50
- window.addEventListener('offline', offlineHandler)
49
+ if (window) {
50
+ window.addEventListener('online', onlineHandler)
51
+ window.addEventListener('offline', offlineHandler)
52
+ }
51
53
 
52
54
  return () => {
53
- window.removeEventListener('online', onlineHandler)
54
- window.removeEventListener('offline', offlineHandler)
55
+ if (window) {
56
+ window.removeEventListener('online', onlineHandler)
57
+ window.removeEventListener('offline', offlineHandler)
58
+ }
55
59
  }
56
60
  })
57
61
 
@@ -102,6 +106,10 @@ export type InternalEditorEvent =
102
106
  type: 'update behaviors'
103
107
  behaviors: Array<Behavior>
104
108
  }
109
+ | {
110
+ type: 'update value'
111
+ value: Array<PortableTextBlock> | undefined
112
+ }
105
113
  | {
106
114
  type: 'toggle readOnly'
107
115
  }
@@ -163,6 +171,7 @@ export const editorMachine = setup({
163
171
  schema: PortableTextMemberSchemaTypes
164
172
  readOnly: boolean
165
173
  maxBlocks: number | undefined
174
+ value: Array<PortableTextBlock> | undefined
166
175
  },
167
176
  events: {} as InternalEditorEvent,
168
177
  emitted: {} as InternalEditorEmittedEvent,
@@ -170,6 +179,7 @@ export const editorMachine = setup({
170
179
  behaviors?: Array<Behavior>
171
180
  keyGenerator: () => string
172
181
  schema: PortableTextMemberSchemaTypes
182
+ value?: Array<PortableTextBlock>
173
183
  },
174
184
  },
175
185
  actions: {
@@ -315,6 +325,7 @@ export const editorMachine = setup({
315
325
  schema: input.schema,
316
326
  readOnly: false,
317
327
  maxBlocks: undefined,
328
+ value: input.value,
318
329
  }),
319
330
  invoke: {
320
331
  id: 'networkLogic',
@@ -352,6 +363,7 @@ export const editorMachine = setup({
352
363
  'done loading': {actions: emit({type: 'done loading'})},
353
364
  'update behaviors': {actions: 'assign behaviors'},
354
365
  'update schema': {actions: 'assign schema'},
366
+ 'update value': {actions: assign({value: ({event}) => event.value})},
355
367
  'toggle readOnly': {
356
368
  actions: assign({readOnly: ({context}) => !context.readOnly}),
357
369
  },
@@ -22,6 +22,7 @@ import {defaultKeyGenerator} from './key-generator'
22
22
  export type EditorConfig = {
23
23
  behaviors?: Array<Behavior>
24
24
  keyGenerator?: () => string
25
+ initialValue?: Array<PortableTextBlock>
25
26
  } & (
26
27
  | {
27
28
  schemaDefinition: SchemaDefinition
@@ -44,6 +45,7 @@ export type EditorEvent = PickFromUnion<
44
45
  | 'patches'
45
46
  | 'toggle readOnly'
46
47
  | 'update behaviors'
48
+ | 'update value'
47
49
  >
48
50
 
49
51
  /**
@@ -74,6 +76,7 @@ export function useEditor(config: EditorConfig): Editor {
74
76
  ? config.schema
75
77
  : compileType(config.schema),
76
78
  ),
79
+ value: config.initialValue,
77
80
  },
78
81
  })
79
82
  const slateEditor = createSlateEditor({editorActor})