@portabletext/editor 1.44.2 → 1.44.4

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.
@@ -124,87 +124,6 @@ function debugWithName(name) {
124
124
  const namespace = `${rootName}${name}`;
125
125
  return debug__default.default && debug__default.default.enabled(namespace) ? debug__default.default(namespace) : debug__default.default(rootName);
126
126
  }
127
- function createKeyedPath(point, value, types2) {
128
- const blockPath = [point.path[0]];
129
- if (!value)
130
- return null;
131
- const block = value[blockPath[0]];
132
- if (!block)
133
- return null;
134
- const keyedBlockPath = [{
135
- _key: block._key
136
- }];
137
- if (block._type !== types2.block.name)
138
- return keyedBlockPath;
139
- let keyedChildPath;
140
- const childPath = point.path.slice(0, 2), child = Array.isArray(block.children) && block.children[childPath[1]];
141
- return child && (keyedChildPath = ["children", {
142
- _key: child._key
143
- }]), keyedChildPath ? [...keyedBlockPath, ...keyedChildPath] : keyedBlockPath;
144
- }
145
- function toSlatePath(path, editor) {
146
- if (!editor)
147
- return [];
148
- const [block, blockPath] = Array.from(slate.Editor.nodes(editor, {
149
- at: [],
150
- match: (n) => types.isKeySegment(path[0]) && n._key === path[0]._key
151
- }))[0] || [void 0, void 0];
152
- if (!block || !slate.Element.isElement(block))
153
- return [];
154
- if (editor.isVoid(block))
155
- return [blockPath[0], 0];
156
- const childPath = [path[2]], childIndex = block.children.findIndex((child) => isEqual__default.default([{
157
- _key: child._key
158
- }], childPath));
159
- if (childIndex >= 0 && block.children[childIndex]) {
160
- const child = block.children[childIndex];
161
- return slate.Element.isElement(child) && editor.isVoid(child) ? blockPath.concat(childIndex).concat(0) : blockPath.concat(childIndex);
162
- }
163
- return [blockPath[0], 0];
164
- }
165
- function toPortableTextRange(value, range, types2) {
166
- if (!range)
167
- return null;
168
- let anchor = null, focus = null;
169
- const anchorPath = range.anchor && createKeyedPath(range.anchor, value, types2);
170
- anchorPath && range.anchor && (anchor = {
171
- path: anchorPath,
172
- offset: range.anchor.offset
173
- });
174
- const focusPath = range.focus && createKeyedPath(range.focus, value, types2);
175
- focusPath && range.focus && (focus = {
176
- path: focusPath,
177
- offset: range.focus.offset
178
- });
179
- const backward = !!(slate.Range.isRange(range) && slate.Range.isBackward(range));
180
- return anchor && focus ? {
181
- anchor,
182
- focus,
183
- backward
184
- } : null;
185
- }
186
- function toSlateRange(selection, editor) {
187
- if (!selection || !editor)
188
- return null;
189
- const anchor = {
190
- path: toSlatePath(selection.anchor.path, editor),
191
- offset: selection.anchor.offset
192
- }, focus = {
193
- path: toSlatePath(selection.focus.path, editor),
194
- offset: selection.focus.offset
195
- };
196
- return focus.path.length === 0 || anchor.path.length === 0 ? null : anchor && focus ? {
197
- anchor,
198
- focus
199
- } : null;
200
- }
201
- function moveRangeByOperation(range, operation) {
202
- const anchor = slate.Point.transform(range.anchor, operation), focus = slate.Point.transform(range.focus, operation);
203
- return anchor === null || focus === null ? null : slate.Point.equals(anchor, range.anchor) && slate.Point.equals(focus, range.focus) ? range : {
204
- anchor,
205
- focus
206
- };
207
- }
208
127
  const VOID_CHILD_KEY = "void-child";
209
128
  function keepObjectEquality(object, keyMap) {
210
129
  const value = keyMap[object._key];
@@ -315,7 +234,24 @@ function isEqualToEmptyEditor(children, schemaTypes) {
315
234
  function getFocusBlock({
316
235
  editor
317
236
  }) {
318
- return editor.selection ? slate.Editor.node(editor, editor.selection.focus.path.slice(0, 1)) ?? [void 0, void 0] : [void 0, void 0];
237
+ if (!editor.selection)
238
+ return [void 0, void 0];
239
+ try {
240
+ return slate.Editor.node(editor, editor.selection.focus.path.slice(0, 1)) ?? [void 0, void 0];
241
+ } catch {
242
+ return [void 0, void 0];
243
+ }
244
+ }
245
+ function getPointBlock({
246
+ editor,
247
+ point
248
+ }) {
249
+ try {
250
+ const [block] = slate.Editor.node(editor, point.path.slice(0, 1)) ?? [void 0, void 0];
251
+ return block ? [block, point.path] : [void 0, void 0];
252
+ } catch {
253
+ return [void 0, void 0];
254
+ }
319
255
  }
320
256
  function getFocusChild({
321
257
  editor
@@ -325,20 +261,49 @@ function getFocusChild({
325
261
  }), childIndex = editor.selection?.focus.path.at(1);
326
262
  if (!focusBlock || !focusBlockPath || childIndex === void 0)
327
263
  return [void 0, void 0];
328
- const focusChild = slate.Node.child(focusBlock, childIndex);
329
- return focusChild ? [focusChild, [...focusBlockPath, childIndex]] : [void 0, void 0];
264
+ try {
265
+ const focusChild = slate.Node.child(focusBlock, childIndex);
266
+ return focusChild ? [focusChild, [...focusBlockPath, childIndex]] : [void 0, void 0];
267
+ } catch {
268
+ return [void 0, void 0];
269
+ }
270
+ }
271
+ function getPointChild({
272
+ editor,
273
+ point
274
+ }) {
275
+ const [block, blockPath] = getPointBlock({
276
+ editor,
277
+ point
278
+ }), childIndex = point.path.at(1);
279
+ if (!block || !blockPath || childIndex === void 0)
280
+ return [void 0, void 0];
281
+ try {
282
+ const pointChild = slate.Node.child(block, childIndex);
283
+ return pointChild ? [pointChild, [...blockPath, childIndex]] : [void 0, void 0];
284
+ } catch {
285
+ return [void 0, void 0];
286
+ }
330
287
  }
331
288
  function getFirstBlock({
332
289
  editor
333
290
  }) {
334
291
  const firstBlockPath = slate.Editor.start(editor, []).path.at(0);
335
- return firstBlockPath !== void 0 ? slate.Editor.node(editor, [firstBlockPath]) ?? [void 0, void 0] : [void 0, void 0];
292
+ try {
293
+ return firstBlockPath !== void 0 ? slate.Editor.node(editor, [firstBlockPath]) ?? [void 0, void 0] : [void 0, void 0];
294
+ } catch {
295
+ return [void 0, void 0];
296
+ }
336
297
  }
337
298
  function getLastBlock({
338
299
  editor
339
300
  }) {
340
301
  const lastBlockPath = slate.Editor.end(editor, []).path.at(0);
341
- return lastBlockPath !== void 0 ? slate.Editor.node(editor, [lastBlockPath]) ?? [void 0, void 0] : [void 0, void 0];
302
+ try {
303
+ return lastBlockPath !== void 0 ? slate.Editor.node(editor, [lastBlockPath]) ?? [void 0, void 0] : [void 0, void 0];
304
+ } catch {
305
+ return [void 0, void 0];
306
+ }
342
307
  }
343
308
  function getNodeBlock({
344
309
  editor,
@@ -404,6 +369,89 @@ function isStyleActive({
404
369
  })];
405
370
  return selectedBlocks.length > 0 ? selectedBlocks.every(([node]) => node.style === style) : !1;
406
371
  }
372
+ function slateRangeToSelection({
373
+ schema: schema2,
374
+ editor,
375
+ range
376
+ }) {
377
+ const [anchorBlock] = getPointBlock({
378
+ editor,
379
+ point: range.anchor
380
+ }), [focusBlock] = getPointBlock({
381
+ editor,
382
+ point: range.focus
383
+ });
384
+ if (!anchorBlock || !focusBlock)
385
+ return null;
386
+ const [anchorChild] = anchorBlock._type === schema2.block.name ? getPointChild({
387
+ editor,
388
+ point: range.anchor
389
+ }) : [void 0, void 0], [focusChild] = focusBlock._type === schema2.block.name ? getPointChild({
390
+ editor,
391
+ point: range.focus
392
+ }) : [void 0, void 0], selection = {
393
+ anchor: {
394
+ path: [{
395
+ _key: anchorBlock._key
396
+ }],
397
+ offset: range.anchor.offset
398
+ },
399
+ focus: {
400
+ path: [{
401
+ _key: focusBlock._key
402
+ }],
403
+ offset: range.focus.offset
404
+ },
405
+ backward: slate.Range.isBackward(range)
406
+ };
407
+ return anchorChild && (selection.anchor.path.push("children"), selection.anchor.path.push({
408
+ _key: anchorChild._key
409
+ })), focusChild && (selection.focus.path.push("children"), selection.focus.path.push({
410
+ _key: focusChild._key
411
+ })), selection;
412
+ }
413
+ function toSlatePath(path, editor) {
414
+ if (!editor)
415
+ return [];
416
+ const [block, blockPath] = Array.from(slate.Editor.nodes(editor, {
417
+ at: [],
418
+ match: (n) => types.isKeySegment(path[0]) && n._key === path[0]._key
419
+ }))[0] || [void 0, void 0];
420
+ if (!block || !slate.Element.isElement(block))
421
+ return [];
422
+ if (editor.isVoid(block))
423
+ return [blockPath[0], 0];
424
+ const childPath = [path[2]], childIndex = block.children.findIndex((child) => isEqual__default.default([{
425
+ _key: child._key
426
+ }], childPath));
427
+ if (childIndex >= 0 && block.children[childIndex]) {
428
+ const child = block.children[childIndex];
429
+ return slate.Element.isElement(child) && editor.isVoid(child) ? blockPath.concat(childIndex).concat(0) : blockPath.concat(childIndex);
430
+ }
431
+ return [blockPath[0], 0];
432
+ }
433
+ function toSlateRange(selection, editor) {
434
+ if (!selection || !editor)
435
+ return null;
436
+ const anchor = {
437
+ path: toSlatePath(selection.anchor.path, editor),
438
+ offset: selection.anchor.offset
439
+ }, focus = {
440
+ path: toSlatePath(selection.focus.path, editor),
441
+ offset: selection.focus.offset
442
+ };
443
+ return focus.path.length === 0 || anchor.path.length === 0 ? null : anchor && focus ? {
444
+ anchor,
445
+ focus
446
+ } : null;
447
+ }
448
+ function moveRangeByOperation(range, operation) {
449
+ const anchor = slate.Point.transform(range.anchor, operation), focus = slate.Point.transform(range.focus, operation);
450
+ return anchor === null || focus === null ? null : slate.Point.equals(anchor, range.anchor) && slate.Point.equals(focus, range.focus) ? range : {
451
+ anchor,
452
+ focus
453
+ };
454
+ }
407
455
  const IS_PROCESSING_REMOTE_CHANGES = /* @__PURE__ */ new WeakMap(), KEY_TO_SLATE_ELEMENT = /* @__PURE__ */ new WeakMap(), KEY_TO_VALUE_ELEMENT = /* @__PURE__ */ new WeakMap(), SLATE_TO_PORTABLE_TEXT_RANGE = /* @__PURE__ */ new WeakMap(), EditorActorContext = React.createContext({}), PortableTextEditorContext = React.createContext(null), usePortableTextEditor = () => {
408
456
  const editor = React.useContext(PortableTextEditorContext);
409
457
  if (!editor)
@@ -3546,7 +3594,11 @@ function createEditableAPI(editor, editorActor) {
3546
3594
  }
3547
3595
  },
3548
3596
  editor
3549
- }), toPortableTextRange(fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types2)?.focus.path ?? [];
3597
+ }), editor.selection ? slateRangeToSelection({
3598
+ schema: editorActor.getSnapshot().context.schema,
3599
+ editor,
3600
+ range: editor.selection
3601
+ })?.focus.path ?? [] : [];
3550
3602
  if (!editor.selection)
3551
3603
  throw new Error("The editor has no selection");
3552
3604
  const [focusBlock] = Array.from(slate.Editor.nodes(editor, {
@@ -3574,7 +3626,11 @@ function createEditableAPI(editor, editorActor) {
3574
3626
  })), slate.Transforms.insertNodes(editor, child, {
3575
3627
  select: !0,
3576
3628
  at: editor.selection
3577
- }), editor.onChange(), toPortableTextRange(fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types2)?.focus.path || [];
3629
+ }), editor.onChange(), editor.selection ? slateRangeToSelection({
3630
+ schema: editorActor.getSnapshot().context.schema,
3631
+ editor,
3632
+ range: editor.selection
3633
+ })?.focus.path ?? [] : [];
3578
3634
  },
3579
3635
  insertBlock: (type, value) => (editorActor.send({
3580
3636
  type: "behavior event",
@@ -3587,7 +3643,11 @@ function createEditableAPI(editor, editorActor) {
3587
3643
  placement: "auto"
3588
3644
  },
3589
3645
  editor
3590
- }), toPortableTextRange(fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types2)?.focus.path ?? []),
3646
+ }), editor.selection ? slateRangeToSelection({
3647
+ schema: editorActor.getSnapshot().context.schema,
3648
+ editor,
3649
+ range: editor.selection
3650
+ })?.focus.path ?? [] : []),
3591
3651
  hasBlockStyle: (style) => {
3592
3652
  try {
3593
3653
  return isStyleActive({
@@ -3745,7 +3805,11 @@ function createEditableAPI(editor, editorActor) {
3745
3805
  const existing = SLATE_TO_PORTABLE_TEXT_RANGE.get(editor.selection);
3746
3806
  if (existing)
3747
3807
  return existing;
3748
- ptRange = toPortableTextRange(fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types2), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
3808
+ ptRange = slateRangeToSelection({
3809
+ schema: editorActor.getSnapshot().context.schema,
3810
+ editor,
3811
+ range: editor.selection
3812
+ }), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
3749
3813
  }
3750
3814
  return ptRange;
3751
3815
  },
@@ -4036,7 +4100,11 @@ const addAnnotationActionImplementation = ({
4036
4100
  } : void 0, selection = manualSelection ? toSlateRange(manualSelection, action.editor) ?? editor.selection : editor.selection;
4037
4101
  if (!selection)
4038
4102
  return;
4039
- const editorSelection = toPortableTextRange(value, selection, context.schema), anchorOffset = editorSelection ? util_selectionPointToBlockOffset.selectionPointToBlockOffset({
4103
+ const editorSelection = slateRangeToSelection({
4104
+ schema: context.schema,
4105
+ editor,
4106
+ range: selection
4107
+ }), anchorOffset = editorSelection ? util_selectionPointToBlockOffset.selectionPointToBlockOffset({
4040
4108
  value,
4041
4109
  selectionPoint: editorSelection.anchor
4042
4110
  }) : void 0, focusOffset = editorSelection ? util_selectionPointToBlockOffset.selectionPointToBlockOffset({
@@ -4762,7 +4830,11 @@ function createWithEventListeners(editorActor) {
4762
4830
  type: "behavior event",
4763
4831
  behaviorEvent: {
4764
4832
  type: "select",
4765
- selection: toPortableTextRange(fromSlateValue(editor.children, editorActor.getSnapshot().context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), range, editorActor.getSnapshot().context.schema)
4833
+ selection: slateRangeToSelection({
4834
+ schema: editorActor.getSnapshot().context.schema,
4835
+ editor,
4836
+ range
4837
+ })
4766
4838
  },
4767
4839
  editor,
4768
4840
  defaultActionCallback: () => {
@@ -5319,7 +5391,7 @@ function createWithPortableTextBlockStyle(editorActor, types2) {
5319
5391
  };
5320
5392
  }
5321
5393
  debugWithName("plugin:withPortableTextSelections");
5322
- function createWithPortableTextSelections(editorActor, types2) {
5394
+ function createWithPortableTextSelections(editorActor) {
5323
5395
  let prevSelection = null;
5324
5396
  return function(editor) {
5325
5397
  const emitPortableTextSelection = () => {
@@ -5327,12 +5399,11 @@ function createWithPortableTextSelections(editorActor, types2) {
5327
5399
  let ptRange = null;
5328
5400
  if (editor.selection) {
5329
5401
  const existing = SLATE_TO_PORTABLE_TEXT_RANGE.get(editor.selection);
5330
- if (existing)
5331
- ptRange = existing;
5332
- else {
5333
- const value = editor.children;
5334
- ptRange = toPortableTextRange(value, editor.selection, types2), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
5335
- }
5402
+ existing ? ptRange = existing : (ptRange = slateRangeToSelection({
5403
+ schema: editorActor.getSnapshot().context.schema,
5404
+ editor,
5405
+ range: editor.selection
5406
+ }), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange));
5336
5407
  }
5337
5408
  ptRange ? editorActor.send({
5338
5409
  type: "notify.selection",
@@ -5446,7 +5517,7 @@ const withPlugins = (editor, options) => {
5446
5517
  }), withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor, schemaTypes), withPortableTextBlockStyle = createWithPortableTextBlockStyle(editorActor, schemaTypes), withPlaceholderBlock = createWithPlaceholderBlock(editorActor), withUtils = createWithUtils({
5447
5518
  editorActor,
5448
5519
  schemaTypes
5449
- }), withPortableTextSelections = createWithPortableTextSelections(editorActor, schemaTypes);
5520
+ }), withPortableTextSelections = createWithPortableTextSelections(editorActor);
5450
5521
  return createWithEventListeners(editorActor)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(e)))))))))));
5451
5522
  }, debug$3 = debugWithName("component:PortableTextEditor:SlateContainer"), slateEditors = /* @__PURE__ */ new WeakMap();
5452
5523
  function createSlateEditor(config) {
@@ -6371,7 +6442,11 @@ function createEditorSnapshot({
6371
6442
  hasTag,
6372
6443
  internalDrag
6373
6444
  }) {
6374
- const value = slateChildrenToBlocks(schema2, editor.children), selection = toPortableTextRange(value, editor.selection, schema2);
6445
+ const value = slateChildrenToBlocks(schema2, editor.children), selection = editor.selection ? slateRangeToSelection({
6446
+ schema: schema2,
6447
+ editor,
6448
+ range: editor.selection
6449
+ }) : null;
6375
6450
  return {
6376
6451
  context: {
6377
6452
  activeDecorators: getActiveDecorators({
@@ -7505,7 +7580,7 @@ exports.getLastBlock = getLastBlock;
7505
7580
  exports.getNodeBlock = getNodeBlock;
7506
7581
  exports.isEqualToEmptyEditor = isEqualToEmptyEditor;
7507
7582
  exports.moveRangeByOperation = moveRangeByOperation;
7508
- exports.toPortableTextRange = toPortableTextRange;
7583
+ exports.slateRangeToSelection = slateRangeToSelection;
7509
7584
  exports.toSlateRange = toSlateRange;
7510
7585
  exports.useEditor = useEditor;
7511
7586
  exports.useEditorSelector = useEditorSelector;