@portabletext/editor 1.44.2 → 1.44.3

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];
@@ -317,6 +236,13 @@ function getFocusBlock({
317
236
  }) {
318
237
  return editor.selection ? slate.Editor.node(editor, editor.selection.focus.path.slice(0, 1)) ?? [void 0, void 0] : [void 0, void 0];
319
238
  }
239
+ function getPointBlock({
240
+ editor,
241
+ point
242
+ }) {
243
+ const [block] = slate.Editor.node(editor, point.path.slice(0, 1)) ?? [void 0, void 0];
244
+ return block ? [block, point.path] : [void 0, void 0];
245
+ }
320
246
  function getFocusChild({
321
247
  editor
322
248
  }) {
@@ -328,6 +254,23 @@ function getFocusChild({
328
254
  const focusChild = slate.Node.child(focusBlock, childIndex);
329
255
  return focusChild ? [focusChild, [...focusBlockPath, childIndex]] : [void 0, void 0];
330
256
  }
257
+ function getPointChild({
258
+ editor,
259
+ point
260
+ }) {
261
+ const [block, blockPath] = getPointBlock({
262
+ editor,
263
+ point
264
+ }), childIndex = point.path.at(1);
265
+ if (!block || !blockPath || childIndex === void 0)
266
+ return [void 0, void 0];
267
+ try {
268
+ const pointChild = slate.Node.child(block, childIndex);
269
+ return pointChild ? [pointChild, [...blockPath, childIndex]] : [void 0, void 0];
270
+ } catch {
271
+ return [void 0, void 0];
272
+ }
273
+ }
331
274
  function getFirstBlock({
332
275
  editor
333
276
  }) {
@@ -404,6 +347,89 @@ function isStyleActive({
404
347
  })];
405
348
  return selectedBlocks.length > 0 ? selectedBlocks.every(([node]) => node.style === style) : !1;
406
349
  }
350
+ function slateRangeToSelection({
351
+ schema: schema2,
352
+ editor,
353
+ range
354
+ }) {
355
+ const [anchorBlock] = getPointBlock({
356
+ editor,
357
+ point: range.anchor
358
+ }), [focusBlock] = getPointBlock({
359
+ editor,
360
+ point: range.focus
361
+ });
362
+ if (!anchorBlock || !focusBlock)
363
+ return null;
364
+ const [anchorChild] = anchorBlock._type === schema2.block.name ? getPointChild({
365
+ editor,
366
+ point: range.anchor
367
+ }) : [void 0, void 0], [focusChild] = focusBlock._type === schema2.block.name ? getPointChild({
368
+ editor,
369
+ point: range.focus
370
+ }) : [void 0, void 0], selection = {
371
+ anchor: {
372
+ path: [{
373
+ _key: anchorBlock._key
374
+ }],
375
+ offset: range.anchor.offset
376
+ },
377
+ focus: {
378
+ path: [{
379
+ _key: focusBlock._key
380
+ }],
381
+ offset: range.focus.offset
382
+ },
383
+ backward: slate.Range.isBackward(range)
384
+ };
385
+ return anchorChild && (selection.anchor.path.push("children"), selection.anchor.path.push({
386
+ _key: anchorChild._key
387
+ })), focusChild && (selection.focus.path.push("children"), selection.focus.path.push({
388
+ _key: focusChild._key
389
+ })), selection;
390
+ }
391
+ function toSlatePath(path, editor) {
392
+ if (!editor)
393
+ return [];
394
+ const [block, blockPath] = Array.from(slate.Editor.nodes(editor, {
395
+ at: [],
396
+ match: (n) => types.isKeySegment(path[0]) && n._key === path[0]._key
397
+ }))[0] || [void 0, void 0];
398
+ if (!block || !slate.Element.isElement(block))
399
+ return [];
400
+ if (editor.isVoid(block))
401
+ return [blockPath[0], 0];
402
+ const childPath = [path[2]], childIndex = block.children.findIndex((child) => isEqual__default.default([{
403
+ _key: child._key
404
+ }], childPath));
405
+ if (childIndex >= 0 && block.children[childIndex]) {
406
+ const child = block.children[childIndex];
407
+ return slate.Element.isElement(child) && editor.isVoid(child) ? blockPath.concat(childIndex).concat(0) : blockPath.concat(childIndex);
408
+ }
409
+ return [blockPath[0], 0];
410
+ }
411
+ function toSlateRange(selection, editor) {
412
+ if (!selection || !editor)
413
+ return null;
414
+ const anchor = {
415
+ path: toSlatePath(selection.anchor.path, editor),
416
+ offset: selection.anchor.offset
417
+ }, focus = {
418
+ path: toSlatePath(selection.focus.path, editor),
419
+ offset: selection.focus.offset
420
+ };
421
+ return focus.path.length === 0 || anchor.path.length === 0 ? null : anchor && focus ? {
422
+ anchor,
423
+ focus
424
+ } : null;
425
+ }
426
+ function moveRangeByOperation(range, operation) {
427
+ const anchor = slate.Point.transform(range.anchor, operation), focus = slate.Point.transform(range.focus, operation);
428
+ return anchor === null || focus === null ? null : slate.Point.equals(anchor, range.anchor) && slate.Point.equals(focus, range.focus) ? range : {
429
+ anchor,
430
+ focus
431
+ };
432
+ }
407
433
  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
434
  const editor = React.useContext(PortableTextEditorContext);
409
435
  if (!editor)
@@ -3546,7 +3572,11 @@ function createEditableAPI(editor, editorActor) {
3546
3572
  }
3547
3573
  },
3548
3574
  editor
3549
- }), toPortableTextRange(fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types2)?.focus.path ?? [];
3575
+ }), editor.selection ? slateRangeToSelection({
3576
+ schema: editorActor.getSnapshot().context.schema,
3577
+ editor,
3578
+ range: editor.selection
3579
+ })?.focus.path ?? [] : [];
3550
3580
  if (!editor.selection)
3551
3581
  throw new Error("The editor has no selection");
3552
3582
  const [focusBlock] = Array.from(slate.Editor.nodes(editor, {
@@ -3574,7 +3604,11 @@ function createEditableAPI(editor, editorActor) {
3574
3604
  })), slate.Transforms.insertNodes(editor, child, {
3575
3605
  select: !0,
3576
3606
  at: editor.selection
3577
- }), editor.onChange(), toPortableTextRange(fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types2)?.focus.path || [];
3607
+ }), editor.onChange(), editor.selection ? slateRangeToSelection({
3608
+ schema: editorActor.getSnapshot().context.schema,
3609
+ editor,
3610
+ range: editor.selection
3611
+ })?.focus.path ?? [] : [];
3578
3612
  },
3579
3613
  insertBlock: (type, value) => (editorActor.send({
3580
3614
  type: "behavior event",
@@ -3587,7 +3621,11 @@ function createEditableAPI(editor, editorActor) {
3587
3621
  placement: "auto"
3588
3622
  },
3589
3623
  editor
3590
- }), toPortableTextRange(fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types2)?.focus.path ?? []),
3624
+ }), editor.selection ? slateRangeToSelection({
3625
+ schema: editorActor.getSnapshot().context.schema,
3626
+ editor,
3627
+ range: editor.selection
3628
+ })?.focus.path ?? [] : []),
3591
3629
  hasBlockStyle: (style) => {
3592
3630
  try {
3593
3631
  return isStyleActive({
@@ -3745,7 +3783,11 @@ function createEditableAPI(editor, editorActor) {
3745
3783
  const existing = SLATE_TO_PORTABLE_TEXT_RANGE.get(editor.selection);
3746
3784
  if (existing)
3747
3785
  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);
3786
+ ptRange = slateRangeToSelection({
3787
+ schema: editorActor.getSnapshot().context.schema,
3788
+ editor,
3789
+ range: editor.selection
3790
+ }), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
3749
3791
  }
3750
3792
  return ptRange;
3751
3793
  },
@@ -4036,7 +4078,11 @@ const addAnnotationActionImplementation = ({
4036
4078
  } : void 0, selection = manualSelection ? toSlateRange(manualSelection, action.editor) ?? editor.selection : editor.selection;
4037
4079
  if (!selection)
4038
4080
  return;
4039
- const editorSelection = toPortableTextRange(value, selection, context.schema), anchorOffset = editorSelection ? util_selectionPointToBlockOffset.selectionPointToBlockOffset({
4081
+ const editorSelection = slateRangeToSelection({
4082
+ schema: context.schema,
4083
+ editor,
4084
+ range: selection
4085
+ }), anchorOffset = editorSelection ? util_selectionPointToBlockOffset.selectionPointToBlockOffset({
4040
4086
  value,
4041
4087
  selectionPoint: editorSelection.anchor
4042
4088
  }) : void 0, focusOffset = editorSelection ? util_selectionPointToBlockOffset.selectionPointToBlockOffset({
@@ -4762,7 +4808,11 @@ function createWithEventListeners(editorActor) {
4762
4808
  type: "behavior event",
4763
4809
  behaviorEvent: {
4764
4810
  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)
4811
+ selection: slateRangeToSelection({
4812
+ schema: editorActor.getSnapshot().context.schema,
4813
+ editor,
4814
+ range
4815
+ })
4766
4816
  },
4767
4817
  editor,
4768
4818
  defaultActionCallback: () => {
@@ -5319,7 +5369,7 @@ function createWithPortableTextBlockStyle(editorActor, types2) {
5319
5369
  };
5320
5370
  }
5321
5371
  debugWithName("plugin:withPortableTextSelections");
5322
- function createWithPortableTextSelections(editorActor, types2) {
5372
+ function createWithPortableTextSelections(editorActor) {
5323
5373
  let prevSelection = null;
5324
5374
  return function(editor) {
5325
5375
  const emitPortableTextSelection = () => {
@@ -5327,12 +5377,11 @@ function createWithPortableTextSelections(editorActor, types2) {
5327
5377
  let ptRange = null;
5328
5378
  if (editor.selection) {
5329
5379
  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
- }
5380
+ existing ? ptRange = existing : (ptRange = slateRangeToSelection({
5381
+ schema: editorActor.getSnapshot().context.schema,
5382
+ editor,
5383
+ range: editor.selection
5384
+ }), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange));
5336
5385
  }
5337
5386
  ptRange ? editorActor.send({
5338
5387
  type: "notify.selection",
@@ -5446,7 +5495,7 @@ const withPlugins = (editor, options) => {
5446
5495
  }), withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor, schemaTypes), withPortableTextBlockStyle = createWithPortableTextBlockStyle(editorActor, schemaTypes), withPlaceholderBlock = createWithPlaceholderBlock(editorActor), withUtils = createWithUtils({
5447
5496
  editorActor,
5448
5497
  schemaTypes
5449
- }), withPortableTextSelections = createWithPortableTextSelections(editorActor, schemaTypes);
5498
+ }), withPortableTextSelections = createWithPortableTextSelections(editorActor);
5450
5499
  return createWithEventListeners(editorActor)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(e)))))))))));
5451
5500
  }, debug$3 = debugWithName("component:PortableTextEditor:SlateContainer"), slateEditors = /* @__PURE__ */ new WeakMap();
5452
5501
  function createSlateEditor(config) {
@@ -6371,7 +6420,11 @@ function createEditorSnapshot({
6371
6420
  hasTag,
6372
6421
  internalDrag
6373
6422
  }) {
6374
- const value = slateChildrenToBlocks(schema2, editor.children), selection = toPortableTextRange(value, editor.selection, schema2);
6423
+ const value = slateChildrenToBlocks(schema2, editor.children), selection = editor.selection ? slateRangeToSelection({
6424
+ schema: schema2,
6425
+ editor,
6426
+ range: editor.selection
6427
+ }) : null;
6375
6428
  return {
6376
6429
  context: {
6377
6430
  activeDecorators: getActiveDecorators({
@@ -7505,7 +7558,7 @@ exports.getLastBlock = getLastBlock;
7505
7558
  exports.getNodeBlock = getNodeBlock;
7506
7559
  exports.isEqualToEmptyEditor = isEqualToEmptyEditor;
7507
7560
  exports.moveRangeByOperation = moveRangeByOperation;
7508
- exports.toPortableTextRange = toPortableTextRange;
7561
+ exports.slateRangeToSelection = slateRangeToSelection;
7509
7562
  exports.toSlateRange = toSlateRange;
7510
7563
  exports.useEditor = useEditor;
7511
7564
  exports.useEditorSelector = useEditorSelector;