@portabletext/editor 1.49.12 → 1.49.13

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.
@@ -1280,6 +1280,142 @@ declare const editorMachine: StateMachine<
1280
1280
  never,
1281
1281
  never
1282
1282
  >,
1283
+ ActionFunction<
1284
+ {
1285
+ behaviors: Set<BehaviorConfig>
1286
+ converters: Set<Converter>
1287
+ getLegacySchema: () => PortableTextMemberSchemaTypes
1288
+ keyGenerator: () => string
1289
+ pendingEvents: Array<InternalPatchEvent | MutationEvent>
1290
+ pendingIncomingPatchesEvents: Array<PatchesEvent>
1291
+ schema: EditorSchema
1292
+ initialReadOnly: boolean
1293
+ maxBlocks: number | undefined
1294
+ selection: EditorSelection
1295
+ initialValue: Array<PortableTextBlock> | undefined
1296
+ internalDrag?: {
1297
+ ghost?: HTMLElement
1298
+ origin: Pick<EventPosition, 'selection'>
1299
+ }
1300
+ slateEditor?: PortableTextSlateEditor
1301
+ },
1302
+ {
1303
+ type: 'update selection'
1304
+ selection: EditorSelection
1305
+ },
1306
+ | InternalPatchEvent
1307
+ | MutationEvent
1308
+ | PatchesEvent
1309
+ | {
1310
+ type: 'update readOnly'
1311
+ readOnly: boolean
1312
+ }
1313
+ | {
1314
+ type: 'update maxBlocks'
1315
+ maxBlocks: number | undefined
1316
+ }
1317
+ | {
1318
+ type: 'add behavior'
1319
+ behaviorConfig: BehaviorConfig
1320
+ }
1321
+ | {
1322
+ type: 'remove behavior'
1323
+ behaviorConfig: BehaviorConfig
1324
+ }
1325
+ | {
1326
+ type: 'blur'
1327
+ editor: PortableTextSlateEditor
1328
+ }
1329
+ | {
1330
+ type: 'focus'
1331
+ editor: PortableTextSlateEditor
1332
+ }
1333
+ | {
1334
+ type: 'normalizing'
1335
+ }
1336
+ | {
1337
+ type: 'update selection'
1338
+ selection: EditorSelection
1339
+ }
1340
+ | {
1341
+ type: 'done normalizing'
1342
+ }
1343
+ | {
1344
+ type: 'done syncing value'
1345
+ }
1346
+ | {
1347
+ type: 'syncing value'
1348
+ }
1349
+ | {
1350
+ type: 'behavior event'
1351
+ behaviorEvent: BehaviorEvent
1352
+ editor: PortableTextSlateEditor
1353
+ nativeEvent?: {
1354
+ preventDefault: () => void
1355
+ }
1356
+ }
1357
+ | {
1358
+ type: 'dragstart'
1359
+ origin: Pick<EventPosition, 'selection'>
1360
+ ghost?: HTMLElement
1361
+ }
1362
+ | {
1363
+ type: 'dragend'
1364
+ }
1365
+ | {
1366
+ type: 'drop'
1367
+ },
1368
+ undefined,
1369
+ never,
1370
+ never,
1371
+ never,
1372
+ never,
1373
+ | InternalPatchEvent
1374
+ | MutationEvent
1375
+ | PatchesEvent
1376
+ | {
1377
+ type: 'blurred'
1378
+ event: FocusEvent_2<HTMLDivElement, Element>
1379
+ }
1380
+ | {
1381
+ type: 'done loading'
1382
+ }
1383
+ | {
1384
+ type: 'editable'
1385
+ }
1386
+ | {
1387
+ type: 'error'
1388
+ name: string
1389
+ description: string
1390
+ data: unknown
1391
+ }
1392
+ | {
1393
+ type: 'focused'
1394
+ event: FocusEvent_2<HTMLDivElement, Element>
1395
+ }
1396
+ | {
1397
+ type: 'invalid value'
1398
+ resolution: InvalidValueResolution | null
1399
+ value: Array<PortableTextBlock> | undefined
1400
+ }
1401
+ | {
1402
+ type: 'loading'
1403
+ }
1404
+ | {
1405
+ type: 'read only'
1406
+ }
1407
+ | {
1408
+ type: 'ready'
1409
+ }
1410
+ | {
1411
+ type: 'selection'
1412
+ selection: EditorSelection
1413
+ }
1414
+ | {
1415
+ type: 'value changed'
1416
+ value: Array<PortableTextBlock> | undefined
1417
+ }
1418
+ >,
1283
1419
  ]
1284
1420
  }
1285
1421
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.49.12",
3
+ "version": "1.49.13",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -79,8 +79,8 @@
79
79
  "slate-react": "0.114.2",
80
80
  "use-effect-event": "^1.0.2",
81
81
  "xstate": "^5.19.2",
82
- "@portabletext/block-tools": "1.1.26",
83
- "@portabletext/patches": "1.1.3"
82
+ "@portabletext/patches": "1.1.3",
83
+ "@portabletext/block-tools": "1.1.26"
84
84
  },
85
85
  "devDependencies": {
86
86
  "@portabletext/toolkit": "^2.0.17",
@@ -284,8 +284,8 @@ export const PortableTextEditable = forwardRef<
284
284
  // Output selection here in those cases where the editor selection was the same, and there are no set_selection operations made.
285
285
  // The selection is usually automatically emitted to change$ by the withPortableTextSelections plugin whenever there is a set_selection operation applied.
286
286
  if (!slateEditor.operations.some((o) => o.type === 'set_selection')) {
287
- relayActor.send({
288
- type: 'selection',
287
+ editorActor.send({
288
+ type: 'update selection',
289
289
  selection: normalizedSelection,
290
290
  })
291
291
  }
@@ -293,7 +293,7 @@ export const PortableTextEditable = forwardRef<
293
293
  }
294
294
  }
295
295
  }
296
- }, [editorActor, propsSelection, relayActor, slateEditor])
296
+ }, [editorActor, propsSelection, slateEditor])
297
297
 
298
298
  // Restore selection from props when the editor has been initialized properly with it's value
299
299
  useEffect(() => {
@@ -550,14 +550,14 @@ export const PortableTextEditable = forwardRef<
550
550
  const newSelection = PortableTextEditor.getSelection(portableTextEditor)
551
551
  // If the selection is the same, emit it explicitly here as there is no actual onChange event triggered.
552
552
  if (selection === newSelection) {
553
- relayActor.send({
554
- type: 'selection',
553
+ editorActor.send({
554
+ type: 'update selection',
555
555
  selection,
556
556
  })
557
557
  }
558
558
  }
559
559
  },
560
- [onFocus, slateEditor, portableTextEditor, relayActor],
560
+ [editorActor, onFocus, slateEditor, portableTextEditor, relayActor],
561
561
  )
562
562
 
563
563
  const handleClick = useCallback(
@@ -127,24 +127,24 @@ describe('Feature: Self-solving', () => {
127
127
  },
128
128
  })
129
129
  expect(onChange).toHaveBeenNthCalledWith(4, {
130
- type: 'selection',
131
- selection: {
132
- ...getTextSelection(initialValue, 'foo'),
133
- backward: false,
134
- },
135
- })
136
- expect(onChange).toHaveBeenNthCalledWith(5, {
137
130
  type: 'patch',
138
131
  patch: spanPatch,
139
132
  })
140
- expect(onChange).toHaveBeenNthCalledWith(6, {
133
+ expect(onChange).toHaveBeenNthCalledWith(5, {
141
134
  type: 'patch',
142
135
  patch: blockPatch,
143
136
  })
144
- expect(onChange).toHaveBeenNthCalledWith(7, {
137
+ expect(onChange).toHaveBeenNthCalledWith(6, {
145
138
  type: 'patch',
146
139
  patch: strongPatch,
147
140
  })
141
+ expect(onChange).toHaveBeenNthCalledWith(7, {
142
+ type: 'selection',
143
+ selection: {
144
+ ...getTextSelection(initialValue, 'foo'),
145
+ backward: false,
146
+ },
147
+ })
148
148
  expect(onChange).toHaveBeenNthCalledWith(8, {
149
149
  type: 'mutation',
150
150
  patches: [spanPatch, blockPatch],
@@ -271,21 +271,6 @@ function createActors(config: {
271
271
  }
272
272
  })
273
273
 
274
- config.subscriptions.push(() => {
275
- const subscription = config.relayActor.on('*', (event) => {
276
- if (event.type === 'selection') {
277
- config.editorActor.send({
278
- type: 'update selection',
279
- selection: event.selection,
280
- })
281
- }
282
- })
283
-
284
- return () => {
285
- subscription.unsubscribe()
286
- }
287
- })
288
-
289
274
  config.subscriptions.push(() => {
290
275
  const subscription = syncActor.on('*', (event) => {
291
276
  switch (event.type) {
@@ -340,6 +325,7 @@ function createActors(config: {
340
325
  case 'mutation':
341
326
  case 'ready':
342
327
  case 'read only':
328
+ case 'selection':
343
329
  config.relayActor.send(event)
344
330
  break
345
331
  case 'internal.patch':
@@ -319,7 +319,10 @@ export const editorMachine = setup({
319
319
  actions: assign({maxBlocks: ({event}) => event.maxBlocks}),
320
320
  },
321
321
  'update selection': {
322
- actions: [assign({selection: ({event}) => event.selection})],
322
+ actions: [
323
+ assign({selection: ({event}) => event.selection}),
324
+ emit(({event}) => ({...event, type: 'selection'})),
325
+ ],
323
326
  },
324
327
  },
325
328
  type: 'parallel',
@@ -4,7 +4,6 @@ import {slateRangeToSelection} from '../../internal-utils/slate-utils'
4
4
  import {SLATE_TO_PORTABLE_TEXT_RANGE} from '../../internal-utils/weakMaps'
5
5
  import type {EditorSelection, PortableTextSlateEditor} from '../../types/editor'
6
6
  import type {EditorActor} from '../editor-machine'
7
- import type {RelayActor} from '../relay-machine'
8
7
 
9
8
  const debug = debugWithName('plugin:withPortableTextSelections')
10
9
  const debugVerbose = debug.enabled && false
@@ -12,7 +11,6 @@ const debugVerbose = debug.enabled && false
12
11
  // This plugin will make sure that we emit a PT selection whenever the editor has changed.
13
12
  export function createWithPortableTextSelections(
14
13
  editorActor: EditorActor,
15
- relayActor: RelayActor,
16
14
  ): (editor: PortableTextSlateEditor) => PortableTextSlateEditor {
17
15
  let prevSelection: BaseRange | null = null
18
16
  return function withPortableTextSelections(
@@ -42,9 +40,9 @@ export function createWithPortableTextSelections(
42
40
  )
43
41
  }
44
42
  if (ptRange) {
45
- relayActor.send({type: 'selection', selection: ptRange})
43
+ editorActor.send({type: 'update selection', selection: ptRange})
46
44
  } else {
47
- relayActor.send({type: 'selection', selection: null})
45
+ editorActor.send({type: 'update selection', selection: null})
48
46
  }
49
47
  }
50
48
  prevSelection = editor.selection
@@ -58,10 +58,8 @@ export const withPlugins = <T extends Editor>(
58
58
  const withUtils = createWithUtils({
59
59
  editorActor,
60
60
  })
61
- const withPortableTextSelections = createWithPortableTextSelections(
62
- editorActor,
63
- relayActor,
64
- )
61
+ const withPortableTextSelections =
62
+ createWithPortableTextSelections(editorActor)
65
63
  const withEventListeners = createWithEventListeners(editorActor)
66
64
 
67
65
  // Ordering is important here, selection dealing last, data manipulation in the middle and core model stuff first.