@portabletext/editor 1.40.3 → 1.41.0

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.
Files changed (34) hide show
  1. package/lib/_chunks-cjs/editor-provider.cjs +72 -34
  2. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  3. package/lib/_chunks-cjs/util.is-selection-collapsed.cjs +10 -0
  4. package/lib/_chunks-cjs/util.is-selection-collapsed.cjs.map +1 -0
  5. package/lib/_chunks-cjs/util.slice-blocks.cjs.map +1 -1
  6. package/lib/_chunks-es/editor-provider.js +73 -35
  7. package/lib/_chunks-es/editor-provider.js.map +1 -1
  8. package/lib/_chunks-es/util.is-selection-collapsed.js +11 -0
  9. package/lib/_chunks-es/util.is-selection-collapsed.js.map +1 -0
  10. package/lib/_chunks-es/util.slice-blocks.js.map +1 -1
  11. package/lib/index.cjs +307 -144
  12. package/lib/index.cjs.map +1 -1
  13. package/lib/index.js +309 -145
  14. package/lib/index.js.map +1 -1
  15. package/lib/utils/index.cjs +7 -5
  16. package/lib/utils/index.cjs.map +1 -1
  17. package/lib/utils/index.d.cts +23 -2
  18. package/lib/utils/index.d.ts +23 -2
  19. package/lib/utils/index.js +6 -3
  20. package/lib/utils/index.js.map +1 -1
  21. package/package.json +4 -4
  22. package/src/behavior-actions/behavior.action.insert-blocks.ts +5 -1
  23. package/src/converters/converter.text-plain.ts +24 -11
  24. package/src/editor/Editable.tsx +336 -223
  25. package/src/editor/components/drop-indicator.tsx +4 -1
  26. package/src/internal-utils/drag-selection.test.ts +74 -1
  27. package/src/internal-utils/drag-selection.ts +20 -4
  28. package/src/internal-utils/dragging-on-drag-origin.ts +22 -0
  29. package/src/internal-utils/event-position.ts +69 -10
  30. package/src/internal-utils/slate-utils.ts +74 -6
  31. package/src/utils/index.ts +2 -0
  32. package/src/utils/util.get-selection-end-point.ts +20 -0
  33. package/src/utils/util.get-selection-start-point.ts +20 -0
  34. package/src/utils/util.is-keyed-segment.ts +2 -2
@@ -312,6 +312,68 @@ function fromSlateValue(value, textBlockType, keyMap = {}) {
312
312
  function isEqualToEmptyEditor(children, schemaTypes) {
313
313
  return children === void 0 || children && Array.isArray(children) && children.length === 0 || children && Array.isArray(children) && children.length === 1 && slate.Element.isElement(children[0]) && children[0]._type === schemaTypes.block.name && "style" in children[0] && children[0].style === schemaTypes.styles[0].value && !("listItem" in children[0]) && Array.isArray(children[0].children) && children[0].children.length === 1 && slate.Text.isText(children[0].children[0]) && children[0].children[0]._type === "span" && !children[0].children[0].marks?.join("") && children[0].children[0].text === "";
314
314
  }
315
+ function getFocusBlock({
316
+ editor
317
+ }) {
318
+ return editor.selection ? Array.from(slate.Editor.nodes(editor, {
319
+ at: editor.selection.focus.path.slice(0, 1),
320
+ match: (n) => !slate.Editor.isEditor(n)
321
+ })).at(0) ?? [void 0, void 0] : [void 0, void 0];
322
+ }
323
+ function getFocusChild({
324
+ editor
325
+ }) {
326
+ const [focusBlock, focusBlockPath] = getFocusBlock({
327
+ editor
328
+ }), childIndex = editor.selection?.focus.path.at(1);
329
+ if (!focusBlock || !focusBlockPath || childIndex === void 0)
330
+ return [void 0, void 0];
331
+ const focusChild = slate.Node.child(focusBlock, childIndex);
332
+ return focusChild ? [focusChild, [...focusBlockPath, childIndex]] : [void 0, void 0];
333
+ }
334
+ function getFirstBlock({
335
+ editor
336
+ }) {
337
+ const firstBlockPath = slate.Editor.start(editor, []).path.at(0);
338
+ return firstBlockPath !== void 0 ? slate.Editor.node(editor, [firstBlockPath]) ?? [void 0, void 0] : [void 0, void 0];
339
+ }
340
+ function getLastBlock({
341
+ editor
342
+ }) {
343
+ const lastBlockPath = slate.Editor.end(editor, []).path.at(0);
344
+ return lastBlockPath !== void 0 ? slate.Editor.node(editor, [lastBlockPath]) ?? [void 0, void 0] : [void 0, void 0];
345
+ }
346
+ function getNodeBlock({
347
+ editor,
348
+ schema: schema2,
349
+ node
350
+ }) {
351
+ if (slate.Editor.isEditor(node))
352
+ return;
353
+ if (isBlockElement(schema2, node))
354
+ return elementToBlock({
355
+ schema: schema2,
356
+ element: node
357
+ });
358
+ const parent = Array.from(slate.Editor.nodes(editor, {
359
+ mode: "highest",
360
+ at: [],
361
+ match: (n) => isBlockElement(schema2, n) && n.children.some((child) => child._key === node._key)
362
+ })).at(0)?.at(0);
363
+ return slate.Element.isElement(parent) ? elementToBlock({
364
+ schema: schema2,
365
+ element: parent
366
+ }) : void 0;
367
+ }
368
+ function elementToBlock({
369
+ schema: schema2,
370
+ element
371
+ }) {
372
+ return fromSlateValue([element], schema2.block.name)?.at(0);
373
+ }
374
+ function isBlockElement(schema2, node) {
375
+ return slate.Element.isElement(node) && (schema2.block.name === node._type || schema2.blockObjects.some((blockObject) => blockObject.name === node._type));
376
+ }
315
377
  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 = () => {
316
378
  const editor = React.useContext(PortableTextEditorContext);
317
379
  if (!editor)
@@ -2532,7 +2594,7 @@ const converterJson = {
2532
2594
  data: util_sliceBlocks.sliceBlocks({
2533
2595
  blocks: snapshot.context.value,
2534
2596
  selection
2535
- }).map((block) => types.isPortableTextTextBlock(block) ? block.children.map((child) => child._type === snapshot.context.schema.span.name ? child.text : "").join("") : "").filter((block) => block !== "").join(`
2597
+ }).map((block) => types.isPortableTextTextBlock(block) ? block.children.map((child) => child._type === snapshot.context.schema.span.name ? child.text : snapshot.beta.hasTag("dragging internally") ? `[${snapshot.context.schema.inlineObjects.find((inlineObjectType) => inlineObjectType.name === child._type)?.title ?? "Object"}]` : "").join("") : snapshot.beta.hasTag("dragging internally") ? `[${snapshot.context.schema.blockObjects.find((blockObjectType) => blockObjectType.name === block._type)?.title ?? "Object"}]` : "").filter((block) => block !== "").join(`
2536
2598
 
2537
2599
  `),
2538
2600
  mimeType: "text/plain",
@@ -4271,36 +4333,7 @@ const addAnnotationActionImplementation = ({
4271
4333
  action
4272
4334
  }) => {
4273
4335
  console.warn(`Deserialization of ${action.mimeType} failed with reason "${action.reason}"`);
4274
- };
4275
- function getFocusBlock({
4276
- editor
4277
- }) {
4278
- return editor.selection ? Array.from(slate.Editor.nodes(editor, {
4279
- at: editor.selection.focus.path.slice(0, 1),
4280
- match: (n) => !slate.Editor.isEditor(n)
4281
- })).at(0) ?? [void 0, void 0] : [void 0, void 0];
4282
- }
4283
- function getFocusChild({
4284
- editor
4285
- }) {
4286
- const [focusBlock, focusBlockPath] = getFocusBlock({
4287
- editor
4288
- }), childIndex = editor.selection?.focus.path.at(1);
4289
- if (!focusBlock || !focusBlockPath || childIndex === void 0)
4290
- return [void 0, void 0];
4291
- const focusChild = slate.Node.child(focusBlock, childIndex);
4292
- return focusChild ? [focusChild, [...focusBlockPath, childIndex]] : [void 0, void 0];
4293
- }
4294
- function getLastBlock({
4295
- editor
4296
- }) {
4297
- return Array.from(slate.Editor.nodes(editor, {
4298
- match: (n) => !slate.Editor.isEditor(n),
4299
- at: [],
4300
- reverse: !0
4301
- })).at(0) ?? [void 0, void 0];
4302
- }
4303
- const insertBlockActionImplementation = ({
4336
+ }, insertBlockActionImplementation = ({
4304
4337
  context,
4305
4338
  action
4306
4339
  }) => {
@@ -4588,15 +4621,17 @@ const selectActionImplementation = ({
4588
4621
  editor: action.editor,
4589
4622
  schema: context.schema
4590
4623
  }), index++;
4591
- } else
4624
+ } else {
4625
+ let index = 0;
4592
4626
  for (const block of fragment)
4593
4627
  insertBlock({
4594
4628
  block,
4595
- placement: "auto",
4629
+ placement: index === 0 ? "auto" : "after",
4596
4630
  select: "end",
4597
4631
  editor: action.editor,
4598
4632
  schema: context.schema
4599
- });
4633
+ }), index++;
4634
+ }
4600
4635
  }, deserializationSuccessActionImplementation = ({
4601
4636
  context,
4602
4637
  action
@@ -7672,6 +7707,9 @@ exports.defaultKeyGenerator = defaultKeyGenerator;
7672
7707
  exports.defineSchema = defineSchema;
7673
7708
  exports.fromSlateValue = fromSlateValue;
7674
7709
  exports.getEditorSnapshot = getEditorSnapshot;
7710
+ exports.getFirstBlock = getFirstBlock;
7711
+ exports.getLastBlock = getLastBlock;
7712
+ exports.getNodeBlock = getNodeBlock;
7675
7713
  exports.isEqualToEmptyEditor = isEqualToEmptyEditor;
7676
7714
  exports.moveRangeByOperation = moveRangeByOperation;
7677
7715
  exports.toPortableTextRange = toPortableTextRange;