@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
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ function getSelectionEndPoint(selection) {
3
+ return selection ? selection.backward ? selection.anchor : selection.focus : null;
4
+ }
5
+ function isSelectionCollapsed(selection) {
6
+ return selection ? selection.anchor.path.join() === selection.focus.path.join() && selection.anchor.offset === selection.focus.offset : !1;
7
+ }
8
+ exports.getSelectionEndPoint = getSelectionEndPoint;
9
+ exports.isSelectionCollapsed = isSelectionCollapsed;
10
+ //# sourceMappingURL=util.is-selection-collapsed.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.is-selection-collapsed.cjs","sources":["../../src/utils/util.get-selection-end-point.ts","../../src/utils/util.is-selection-collapsed.ts"],"sourcesContent":["import type {EditorSelection, EditorSelectionPoint} from '..'\n\n/**\n * @public\n */\nexport function getSelectionEndPoint<\n TEditorSelection extends NonNullable<EditorSelection> | null,\n TEditorSelectionPoint extends\n EditorSelectionPoint | null = TEditorSelection extends NonNullable<EditorSelection>\n ? EditorSelectionPoint\n : null,\n>(selection: TEditorSelection): TEditorSelectionPoint {\n if (!selection) {\n return null as TEditorSelectionPoint\n }\n\n return (\n selection.backward ? selection.anchor : selection.focus\n ) as TEditorSelectionPoint\n}\n","import type {EditorSelection} from '../types/editor'\n\n/**\n * @public\n */\nexport function isSelectionCollapsed(selection: EditorSelection) {\n if (!selection) {\n return false\n }\n\n return (\n selection.anchor.path.join() === selection.focus.path.join() &&\n selection.anchor.offset === selection.focus.offset\n )\n}\n"],"names":["getSelectionEndPoint","selection","backward","anchor","focus","isSelectionCollapsed","path","join","offset"],"mappings":";AAKO,SAASA,qBAMdC,WAAoD;AACpD,SAAKA,YAKHA,UAAUC,WAAWD,UAAUE,SAASF,UAAUG,QAJ3C;AAMX;ACdO,SAASC,qBAAqBJ,WAA4B;AAC/D,SAAKA,YAKHA,UAAUE,OAAOG,KAAKC,KAAAA,MAAWN,UAAUG,MAAME,KAAKC,KAAAA,KACtDN,UAAUE,OAAOK,WAAWP,UAAUG,MAAMI,SALrC;AAOX;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"util.slice-blocks.cjs","sources":["../../src/utils/util.is-keyed-segment.ts","../../src/utils/util.block-offset.ts","../../src/utils/util.get-block-end-point.ts","../../src/utils/util.get-block-start-point.ts","../../src/utils/util.get-text-block-text.ts","../../src/utils/util.is-empty-text-block.ts","../../src/utils/util.is-equal-selection-points.ts","../../src/utils/util.is-span.ts","../../src/utils/util.reverse-selection.ts","../../src/utils/util.slice-blocks.ts"],"sourcesContent":["import type {KeyedSegment, PathSegment} from '@sanity/types'\n\n/**\n * @public\n */\nexport function isKeyedSegment(segment: PathSegment): segment is KeyedSegment {\n return typeof segment === 'object' && segment !== null && '_key' in segment\n}\n","import {\n isPortableTextSpan,\n isPortableTextTextBlock,\n type KeyedSegment,\n type PortableTextBlock,\n} from '@sanity/types'\nimport type {BlockOffset} from '../types/block-offset'\nimport type {EditorSelectionPoint} from '../types/editor'\nimport {isKeyedSegment} from './util.is-keyed-segment'\n\n/**\n * @public\n */\nexport function blockOffsetToSpanSelectionPoint({\n value,\n blockOffset,\n direction,\n}: {\n value: Array<PortableTextBlock>\n blockOffset: BlockOffset\n direction: 'forward' | 'backward'\n}) {\n let offsetLeft = blockOffset.offset\n let selectionPoint:\n | {path: [KeyedSegment, 'children', KeyedSegment]; offset: number}\n | undefined\n let skippedInlineObject = false\n\n for (const block of value) {\n if (block._key !== blockOffset.path[0]._key) {\n continue\n }\n\n if (!isPortableTextTextBlock(block)) {\n continue\n }\n\n for (const child of block.children) {\n if (direction === 'forward') {\n if (!isPortableTextSpan(child)) {\n continue\n }\n\n if (offsetLeft <= child.text.length) {\n selectionPoint = {\n path: [...blockOffset.path, 'children', {_key: child._key}],\n offset: offsetLeft,\n }\n break\n }\n\n offsetLeft -= child.text.length\n\n continue\n }\n\n if (!isPortableTextSpan(child)) {\n skippedInlineObject = true\n continue\n }\n\n if (offsetLeft === 0 && selectionPoint && !skippedInlineObject) {\n if (skippedInlineObject) {\n selectionPoint = {\n path: [...blockOffset.path, 'children', {_key: child._key}],\n offset: 0,\n }\n }\n break\n }\n\n if (offsetLeft > child.text.length) {\n offsetLeft -= child.text.length\n continue\n }\n\n if (offsetLeft <= child.text.length) {\n selectionPoint = {\n path: [...blockOffset.path, 'children', {_key: child._key}],\n offset: offsetLeft,\n }\n\n offsetLeft -= child.text.length\n\n if (offsetLeft !== 0) {\n break\n }\n }\n }\n }\n\n return selectionPoint\n}\n\n/**\n * @public\n */\nexport function spanSelectionPointToBlockOffset({\n value,\n selectionPoint,\n}: {\n value: Array<PortableTextBlock>\n selectionPoint: EditorSelectionPoint\n}): BlockOffset | undefined {\n let offset = 0\n\n const blockKey = isKeyedSegment(selectionPoint.path[0])\n ? selectionPoint.path[0]._key\n : undefined\n const spanKey = isKeyedSegment(selectionPoint.path[2])\n ? selectionPoint.path[2]._key\n : undefined\n\n if (!blockKey || !spanKey) {\n return undefined\n }\n\n for (const block of value) {\n if (block._key !== blockKey) {\n continue\n }\n\n if (!isPortableTextTextBlock(block)) {\n continue\n }\n\n for (const child of block.children) {\n if (!isPortableTextSpan(child)) {\n continue\n }\n\n if (child._key === spanKey) {\n return {\n path: [{_key: block._key}],\n offset: offset + selectionPoint.offset,\n }\n }\n\n offset += child.text.length\n }\n }\n}\n","import {\n isPortableTextSpan,\n isPortableTextTextBlock,\n type KeyedSegment,\n type PortableTextBlock,\n} from '@sanity/types'\nimport type {EditorSelectionPoint} from '../types/editor'\n\n/**\n * @public\n */\nexport function getBlockEndPoint({\n node,\n path,\n}: {\n node: PortableTextBlock\n path: [KeyedSegment]\n}): EditorSelectionPoint {\n if (isPortableTextTextBlock(node)) {\n const lastChild = node.children[node.children.length - 1]\n\n if (lastChild) {\n return {\n path: [...path, 'children', {_key: lastChild._key}],\n offset: isPortableTextSpan(lastChild) ? lastChild.text.length : 0,\n }\n }\n }\n\n return {\n path,\n offset: 0,\n }\n}\n","import {\n isPortableTextTextBlock,\n type KeyedSegment,\n type PortableTextBlock,\n} from '@sanity/types'\nimport type {EditorSelectionPoint} from '../types/editor'\n\n/**\n * @public\n */\nexport function getBlockStartPoint({\n node,\n path,\n}: {\n node: PortableTextBlock\n path: [KeyedSegment]\n}): EditorSelectionPoint {\n if (isPortableTextTextBlock(node)) {\n return {\n path: [...path, 'children', {_key: node.children[0]._key}],\n offset: 0,\n }\n }\n\n return {\n path,\n offset: 0,\n }\n}\n","import type {PortableTextTextBlock} from '@sanity/types'\n\n/**\n * @public\n */\nexport function getTextBlockText(block: PortableTextTextBlock) {\n return block.children.map((child) => child.text ?? '').join('')\n}\n","import {\n isPortableTextSpan,\n isPortableTextTextBlock,\n type PortableTextBlock,\n} from '@sanity/types'\nimport {getTextBlockText} from './util.get-text-block-text'\n\n/**\n * @public\n */\nexport function isEmptyTextBlock(block: PortableTextBlock) {\n if (!isPortableTextTextBlock(block)) {\n return false\n }\n\n const onlyText = block.children.every(isPortableTextSpan)\n const blockText = getTextBlockText(block)\n\n return onlyText && blockText === ''\n}\n","import type {EditorSelectionPoint} from '../types/editor'\n\n/**\n * @public\n */\nexport function isEqualSelectionPoints(\n a: EditorSelectionPoint,\n b: EditorSelectionPoint,\n) {\n return (\n a.offset === b.offset && JSON.stringify(a.path) === JSON.stringify(b.path)\n )\n}\n","import type {PortableTextChild, PortableTextSpan} from '@sanity/types'\nimport type {EditorContext} from '..'\n\n/**\n * @public\n */\nexport function isSpan(\n context: Pick<EditorContext, 'schema'>,\n child: PortableTextChild,\n): child is PortableTextSpan {\n return child._type === context.schema.span.name\n}\n","import type {EditorSelection} from '../types/editor'\n\n/**\n * @public\n */\nexport function reverseSelection<\n TEditorSelection extends NonNullable<EditorSelection> | null,\n>(selection: TEditorSelection): TEditorSelection {\n if (!selection) {\n return selection\n }\n\n if (selection.backward) {\n return {\n anchor: selection.focus,\n focus: selection.anchor,\n backward: false,\n } as TEditorSelection\n }\n\n return {\n anchor: selection.focus,\n focus: selection.anchor,\n backward: true,\n } as TEditorSelection\n}\n","import {\n isKeySegment,\n isPortableTextSpan,\n isPortableTextTextBlock,\n type PortableTextBlock,\n} from '@sanity/types'\nimport type {EditorSelection} from '..'\n\n/**\n * @public\n */\nexport function sliceBlocks({\n blocks,\n selection,\n}: {\n blocks: Array<PortableTextBlock>\n selection: EditorSelection\n}): Array<PortableTextBlock> {\n const slice: Array<PortableTextBlock> = []\n\n if (!selection) {\n return slice\n }\n\n let startBlock: PortableTextBlock | undefined\n const middleBlocks: PortableTextBlock[] = []\n let endBlock: PortableTextBlock | undefined\n\n const startPoint = selection.backward ? selection.focus : selection.anchor\n const endPoint = selection.backward ? selection.anchor : selection.focus\n\n const startBlockKey = isKeySegment(startPoint.path[0])\n ? startPoint.path[0]._key\n : undefined\n const endBlockKey = isKeySegment(endPoint.path[0])\n ? endPoint.path[0]._key\n : undefined\n const startChildKey = isKeySegment(startPoint.path[2])\n ? startPoint.path[2]._key\n : undefined\n const endChildKey = isKeySegment(endPoint.path[2])\n ? endPoint.path[2]._key\n : undefined\n\n if (!startBlockKey || !endBlockKey) {\n return slice\n }\n\n for (const block of blocks) {\n if (!isPortableTextTextBlock(block)) {\n if (block._key === startBlockKey && block._key === endBlockKey) {\n startBlock = block\n break\n }\n }\n\n if (block._key === startBlockKey) {\n if (!isPortableTextTextBlock(block)) {\n startBlock = block\n continue\n }\n\n if (startChildKey) {\n for (const child of block.children) {\n if (child._key === startChildKey) {\n if (isPortableTextSpan(child)) {\n const text =\n child._key === endChildKey\n ? child.text.slice(startPoint.offset, endPoint.offset)\n : child.text.slice(startPoint.offset)\n\n startBlock = {\n ...block,\n children: [\n {\n ...child,\n text,\n },\n ],\n }\n } else {\n startBlock = {\n ...block,\n children: [child],\n }\n }\n\n if (startChildKey === endChildKey) {\n break\n }\n continue\n }\n\n if (startBlock && isPortableTextTextBlock(startBlock)) {\n if (\n endChildKey &&\n child._key === endChildKey &&\n isPortableTextSpan(child)\n ) {\n startBlock.children.push({\n ...child,\n text: child.text.slice(0, endPoint.offset),\n })\n } else {\n startBlock.children.push(child)\n }\n\n if (\n block._key === endBlockKey &&\n endChildKey &&\n child._key === endChildKey\n ) {\n break\n }\n }\n }\n\n if (startBlockKey === endBlockKey) {\n break\n }\n\n continue\n }\n\n startBlock = block\n\n if (startBlockKey === endBlockKey) {\n break\n }\n }\n\n if (block._key === endBlockKey) {\n if (!isPortableTextTextBlock(block)) {\n endBlock = block\n break\n }\n\n if (endChildKey) {\n endBlock = {\n ...block,\n children: [],\n }\n\n for (const child of block.children) {\n if (endBlock && isPortableTextTextBlock(endBlock)) {\n if (child._key === endChildKey && isPortableTextSpan(child)) {\n endBlock.children.push({\n ...child,\n text: child.text.slice(0, endPoint.offset),\n })\n\n break\n }\n\n endBlock.children.push(child)\n\n if (endChildKey && child._key === endChildKey) {\n break\n }\n }\n }\n\n break\n }\n\n endBlock = block\n\n break\n }\n\n if (startBlock) {\n middleBlocks.push(block)\n }\n }\n\n return [\n ...(startBlock ? [startBlock] : []),\n ...middleBlocks,\n ...(endBlock ? [endBlock] : []),\n ]\n}\n"],"names":["isKeyedSegment","segment","blockOffsetToSpanSelectionPoint","value","blockOffset","direction","offsetLeft","offset","selectionPoint","skippedInlineObject","block","_key","path","isPortableTextTextBlock","child","children","isPortableTextSpan","text","length","spanSelectionPointToBlockOffset","blockKey","undefined","spanKey","getBlockEndPoint","node","lastChild","getBlockStartPoint","getTextBlockText","map","join","isEmptyTextBlock","onlyText","every","blockText","isEqualSelectionPoints","a","b","JSON","stringify","isSpan","context","_type","schema","span","name","reverseSelection","selection","backward","anchor","focus","sliceBlocks","blocks","slice","startBlock","middleBlocks","endBlock","startPoint","endPoint","startBlockKey","isKeySegment","endBlockKey","startChildKey","endChildKey","push"],"mappings":";;AAKO,SAASA,eAAeC,SAA+C;AAC5E,SAAO,OAAOA,WAAY,YAAYA,YAAY,QAAQ,UAAUA;AACtE;ACMO,SAASC,gCAAgC;AAAA,EAC9CC;AAAAA,EACAC;AAAAA,EACAC;AAKF,GAAG;AACD,MAAIC,aAAaF,YAAYG,QACz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AA,GAChC,GAAGC,cACH,GAAIC,WAAW,CAACA,QAAQ,IAAI,CAAA,CAAG;AAEnC;;;;;;;;;;;;"}
1
+ {"version":3,"file":"util.slice-blocks.cjs","sources":["../../src/utils/util.is-keyed-segment.ts","../../src/utils/util.block-offset.ts","../../src/utils/util.get-block-end-point.ts","../../src/utils/util.get-block-start-point.ts","../../src/utils/util.get-text-block-text.ts","../../src/utils/util.is-empty-text-block.ts","../../src/utils/util.is-equal-selection-points.ts","../../src/utils/util.is-span.ts","../../src/utils/util.reverse-selection.ts","../../src/utils/util.slice-blocks.ts"],"sourcesContent":["import type {KeyedSegment} from '@sanity/types'\n\n/**\n * @public\n */\nexport function isKeyedSegment(segment: unknown): segment is KeyedSegment {\n return typeof segment === 'object' && segment !== null && '_key' in segment\n}\n","import {\n isPortableTextSpan,\n isPortableTextTextBlock,\n type KeyedSegment,\n type PortableTextBlock,\n} from '@sanity/types'\nimport type {BlockOffset} from '../types/block-offset'\nimport type {EditorSelectionPoint} from '../types/editor'\nimport {isKeyedSegment} from './util.is-keyed-segment'\n\n/**\n * @public\n */\nexport function blockOffsetToSpanSelectionPoint({\n value,\n blockOffset,\n direction,\n}: {\n value: Array<PortableTextBlock>\n blockOffset: BlockOffset\n direction: 'forward' | 'backward'\n}) {\n let offsetLeft = blockOffset.offset\n let selectionPoint:\n | {path: [KeyedSegment, 'children', KeyedSegment]; offset: number}\n | undefined\n let skippedInlineObject = false\n\n for (const block of value) {\n if (block._key !== blockOffset.path[0]._key) {\n continue\n }\n\n if (!isPortableTextTextBlock(block)) {\n continue\n }\n\n for (const child of block.children) {\n if (direction === 'forward') {\n if (!isPortableTextSpan(child)) {\n continue\n }\n\n if (offsetLeft <= child.text.length) {\n selectionPoint = {\n path: [...blockOffset.path, 'children', {_key: child._key}],\n offset: offsetLeft,\n }\n break\n }\n\n offsetLeft -= child.text.length\n\n continue\n }\n\n if (!isPortableTextSpan(child)) {\n skippedInlineObject = true\n continue\n }\n\n if (offsetLeft === 0 && selectionPoint && !skippedInlineObject) {\n if (skippedInlineObject) {\n selectionPoint = {\n path: [...blockOffset.path, 'children', {_key: child._key}],\n offset: 0,\n }\n }\n break\n }\n\n if (offsetLeft > child.text.length) {\n offsetLeft -= child.text.length\n continue\n }\n\n if (offsetLeft <= child.text.length) {\n selectionPoint = {\n path: [...blockOffset.path, 'children', {_key: child._key}],\n offset: offsetLeft,\n }\n\n offsetLeft -= child.text.length\n\n if (offsetLeft !== 0) {\n break\n }\n }\n }\n }\n\n return selectionPoint\n}\n\n/**\n * @public\n */\nexport function spanSelectionPointToBlockOffset({\n value,\n selectionPoint,\n}: {\n value: Array<PortableTextBlock>\n selectionPoint: EditorSelectionPoint\n}): BlockOffset | undefined {\n let offset = 0\n\n const blockKey = isKeyedSegment(selectionPoint.path[0])\n ? selectionPoint.path[0]._key\n : undefined\n const spanKey = isKeyedSegment(selectionPoint.path[2])\n ? selectionPoint.path[2]._key\n : undefined\n\n if (!blockKey || !spanKey) {\n return undefined\n }\n\n for (const block of value) {\n if (block._key !== blockKey) {\n continue\n }\n\n if (!isPortableTextTextBlock(block)) {\n continue\n }\n\n for (const child of block.children) {\n if (!isPortableTextSpan(child)) {\n continue\n }\n\n if (child._key === spanKey) {\n return {\n path: [{_key: block._key}],\n offset: offset + selectionPoint.offset,\n }\n }\n\n offset += child.text.length\n }\n }\n}\n","import {\n isPortableTextSpan,\n isPortableTextTextBlock,\n type KeyedSegment,\n type PortableTextBlock,\n} from '@sanity/types'\nimport type {EditorSelectionPoint} from '../types/editor'\n\n/**\n * @public\n */\nexport function getBlockEndPoint({\n node,\n path,\n}: {\n node: PortableTextBlock\n path: [KeyedSegment]\n}): EditorSelectionPoint {\n if (isPortableTextTextBlock(node)) {\n const lastChild = node.children[node.children.length - 1]\n\n if (lastChild) {\n return {\n path: [...path, 'children', {_key: lastChild._key}],\n offset: isPortableTextSpan(lastChild) ? lastChild.text.length : 0,\n }\n }\n }\n\n return {\n path,\n offset: 0,\n }\n}\n","import {\n isPortableTextTextBlock,\n type KeyedSegment,\n type PortableTextBlock,\n} from '@sanity/types'\nimport type {EditorSelectionPoint} from '../types/editor'\n\n/**\n * @public\n */\nexport function getBlockStartPoint({\n node,\n path,\n}: {\n node: PortableTextBlock\n path: [KeyedSegment]\n}): EditorSelectionPoint {\n if (isPortableTextTextBlock(node)) {\n return {\n path: [...path, 'children', {_key: node.children[0]._key}],\n offset: 0,\n }\n }\n\n return {\n path,\n offset: 0,\n }\n}\n","import type {PortableTextTextBlock} from '@sanity/types'\n\n/**\n * @public\n */\nexport function getTextBlockText(block: PortableTextTextBlock) {\n return block.children.map((child) => child.text ?? '').join('')\n}\n","import {\n isPortableTextSpan,\n isPortableTextTextBlock,\n type PortableTextBlock,\n} from '@sanity/types'\nimport {getTextBlockText} from './util.get-text-block-text'\n\n/**\n * @public\n */\nexport function isEmptyTextBlock(block: PortableTextBlock) {\n if (!isPortableTextTextBlock(block)) {\n return false\n }\n\n const onlyText = block.children.every(isPortableTextSpan)\n const blockText = getTextBlockText(block)\n\n return onlyText && blockText === ''\n}\n","import type {EditorSelectionPoint} from '../types/editor'\n\n/**\n * @public\n */\nexport function isEqualSelectionPoints(\n a: EditorSelectionPoint,\n b: EditorSelectionPoint,\n) {\n return (\n a.offset === b.offset && JSON.stringify(a.path) === JSON.stringify(b.path)\n )\n}\n","import type {PortableTextChild, PortableTextSpan} from '@sanity/types'\nimport type {EditorContext} from '..'\n\n/**\n * @public\n */\nexport function isSpan(\n context: Pick<EditorContext, 'schema'>,\n child: PortableTextChild,\n): child is PortableTextSpan {\n return child._type === context.schema.span.name\n}\n","import type {EditorSelection} from '../types/editor'\n\n/**\n * @public\n */\nexport function reverseSelection<\n TEditorSelection extends NonNullable<EditorSelection> | null,\n>(selection: TEditorSelection): TEditorSelection {\n if (!selection) {\n return selection\n }\n\n if (selection.backward) {\n return {\n anchor: selection.focus,\n focus: selection.anchor,\n backward: false,\n } as TEditorSelection\n }\n\n return {\n anchor: selection.focus,\n focus: selection.anchor,\n backward: true,\n } as TEditorSelection\n}\n","import {\n isKeySegment,\n isPortableTextSpan,\n isPortableTextTextBlock,\n type PortableTextBlock,\n} from '@sanity/types'\nimport type {EditorSelection} from '..'\n\n/**\n * @public\n */\nexport function sliceBlocks({\n blocks,\n selection,\n}: {\n blocks: Array<PortableTextBlock>\n selection: EditorSelection\n}): Array<PortableTextBlock> {\n const slice: Array<PortableTextBlock> = []\n\n if (!selection) {\n return slice\n }\n\n let startBlock: PortableTextBlock | undefined\n const middleBlocks: PortableTextBlock[] = []\n let endBlock: PortableTextBlock | undefined\n\n const startPoint = selection.backward ? selection.focus : selection.anchor\n const endPoint = selection.backward ? selection.anchor : selection.focus\n\n const startBlockKey = isKeySegment(startPoint.path[0])\n ? startPoint.path[0]._key\n : undefined\n const endBlockKey = isKeySegment(endPoint.path[0])\n ? endPoint.path[0]._key\n : undefined\n const startChildKey = isKeySegment(startPoint.path[2])\n ? startPoint.path[2]._key\n : undefined\n const endChildKey = isKeySegment(endPoint.path[2])\n ? endPoint.path[2]._key\n : undefined\n\n if (!startBlockKey || !endBlockKey) {\n return slice\n }\n\n for (const block of blocks) {\n if (!isPortableTextTextBlock(block)) {\n if (block._key === startBlockKey && block._key === endBlockKey) {\n startBlock = block\n break\n }\n }\n\n if (block._key === startBlockKey) {\n if (!isPortableTextTextBlock(block)) {\n startBlock = block\n continue\n }\n\n if (startChildKey) {\n for (const child of block.children) {\n if (child._key === startChildKey) {\n if (isPortableTextSpan(child)) {\n const text =\n child._key === endChildKey\n ? child.text.slice(startPoint.offset, endPoint.offset)\n : child.text.slice(startPoint.offset)\n\n startBlock = {\n ...block,\n children: [\n {\n ...child,\n text,\n },\n ],\n }\n } else {\n startBlock = {\n ...block,\n children: [child],\n }\n }\n\n if (startChildKey === endChildKey) {\n break\n }\n continue\n }\n\n if (startBlock && isPortableTextTextBlock(startBlock)) {\n if (\n endChildKey &&\n child._key === endChildKey &&\n isPortableTextSpan(child)\n ) {\n startBlock.children.push({\n ...child,\n text: child.text.slice(0, endPoint.offset),\n })\n } else {\n startBlock.children.push(child)\n }\n\n if (\n block._key === endBlockKey &&\n endChildKey &&\n child._key === endChildKey\n ) {\n break\n }\n }\n }\n\n if (startBlockKey === endBlockKey) {\n break\n }\n\n continue\n }\n\n startBlock = block\n\n if (startBlockKey === endBlockKey) {\n break\n }\n }\n\n if (block._key === endBlockKey) {\n if (!isPortableTextTextBlock(block)) {\n endBlock = block\n break\n }\n\n if (endChildKey) {\n endBlock = {\n ...block,\n children: [],\n }\n\n for (const child of block.children) {\n if (endBlock && isPortableTextTextBlock(endBlock)) {\n if (child._key === endChildKey && isPortableTextSpan(child)) {\n endBlock.children.push({\n ...child,\n text: child.text.slice(0, endPoint.offset),\n })\n\n break\n }\n\n endBlock.children.push(child)\n\n if (endChildKey && child._key === endChildKey) {\n break\n }\n }\n }\n\n break\n }\n\n endBlock = block\n\n break\n }\n\n if (startBlock) {\n middleBlocks.push(block)\n }\n }\n\n return [\n ...(startBlock ? [startBlock] : []),\n ...middleBlocks,\n ...(endBlock ? [endBlock] : []),\n ]\n}\n"],"names":["isKeyedSegment","segment","blockOffsetToSpanSelectionPoint","value","blockOffset","direction","offsetLeft","offset","selectionPoint","skippedInlineObject","block","_key","path","isPortableTextTextBlock","child","children","isPortableTextSpan","text","length","spanSelectionPointToBlockOffset","blockKey","undefined","spanKey","getBlockEndPoint","node","lastChild","getBlockStartPoint","getTextBlockText","map","join","isEmptyTextBlock","onlyText","every","blockText","isEqualSelectionPoints","a","b","JSON","stringify","isSpan","context","_type","schema","span","name","reverseSelection","selection","backward","anchor","focus","sliceBlocks","blocks","slice","startBlock","middleBlocks","endBlock","startPoint","endPoint","startBlockKey","isKeySegment","endBlockKey","startChildKey","endChildKey","push"],"mappings":";;AAKO,SAASA,eAAeC,SAA2C;AACxE,SAAO,OAAOA,WAAY,YAAYA,YAAY,QAAQ,UAAUA;AACtE;ACMO,SAASC,gCAAgC;AAAA,EAC9CC;AAAAA,EACAC;AAAAA,EAC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SAAO,CACL,GAAI2C,aAAa,CAACA,UAAU,IAAI,CAAA,GAChC,GAAGC,cACH,GAAIC,WAAW,CAACA,QAAQ,IAAI,CAAA,CAAG;AAEnC;;;;;;;;;;;;"}
@@ -5,7 +5,7 @@ import { ReactEditor, withReact, Slate } from "slate-react";
5
5
  import { useSelector, useActorRef } from "@xstate/react";
6
6
  import debug$e from "debug";
7
7
  import isEqual from "lodash/isEqual.js";
8
- import { Editor, Element, Range, Point, Text, Operation, Transforms, Path, insertText, Node, createEditor } from "slate";
8
+ import { Editor, Element, Range, Point, Text, Node, Operation, Transforms, Path, insertText, createEditor } from "slate";
9
9
  import { setup, stateIn, fromCallback, assign, enqueueActions, emit, assertEvent, and, not, createActor } from "xstate";
10
10
  import { unset, set, setIfMissing, insert, diffMatchPatch as diffMatchPatch$1, applyAll } from "@portabletext/patches";
11
11
  import { defineType, defineField, isKeySegment, isPortableTextTextBlock, isPortableTextSpan as isPortableTextSpan$1, isPortableTextListBlock } from "@sanity/types";
@@ -337,6 +337,68 @@ function fromSlateValue(value, textBlockType, keyMap = {}) {
337
337
  function isEqualToEmptyEditor(children, schemaTypes) {
338
338
  return children === void 0 || children && Array.isArray(children) && children.length === 0 || children && Array.isArray(children) && children.length === 1 && 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 && Text.isText(children[0].children[0]) && children[0].children[0]._type === "span" && !children[0].children[0].marks?.join("") && children[0].children[0].text === "";
339
339
  }
340
+ function getFocusBlock({
341
+ editor
342
+ }) {
343
+ return editor.selection ? Array.from(Editor.nodes(editor, {
344
+ at: editor.selection.focus.path.slice(0, 1),
345
+ match: (n) => !Editor.isEditor(n)
346
+ })).at(0) ?? [void 0, void 0] : [void 0, void 0];
347
+ }
348
+ function getFocusChild({
349
+ editor
350
+ }) {
351
+ const [focusBlock, focusBlockPath] = getFocusBlock({
352
+ editor
353
+ }), childIndex = editor.selection?.focus.path.at(1);
354
+ if (!focusBlock || !focusBlockPath || childIndex === void 0)
355
+ return [void 0, void 0];
356
+ const focusChild = Node.child(focusBlock, childIndex);
357
+ return focusChild ? [focusChild, [...focusBlockPath, childIndex]] : [void 0, void 0];
358
+ }
359
+ function getFirstBlock({
360
+ editor
361
+ }) {
362
+ const firstBlockPath = Editor.start(editor, []).path.at(0);
363
+ return firstBlockPath !== void 0 ? Editor.node(editor, [firstBlockPath]) ?? [void 0, void 0] : [void 0, void 0];
364
+ }
365
+ function getLastBlock({
366
+ editor
367
+ }) {
368
+ const lastBlockPath = Editor.end(editor, []).path.at(0);
369
+ return lastBlockPath !== void 0 ? Editor.node(editor, [lastBlockPath]) ?? [void 0, void 0] : [void 0, void 0];
370
+ }
371
+ function getNodeBlock({
372
+ editor,
373
+ schema,
374
+ node
375
+ }) {
376
+ if (Editor.isEditor(node))
377
+ return;
378
+ if (isBlockElement(schema, node))
379
+ return elementToBlock({
380
+ schema,
381
+ element: node
382
+ });
383
+ const parent = Array.from(Editor.nodes(editor, {
384
+ mode: "highest",
385
+ at: [],
386
+ match: (n) => isBlockElement(schema, n) && n.children.some((child) => child._key === node._key)
387
+ })).at(0)?.at(0);
388
+ return Element.isElement(parent) ? elementToBlock({
389
+ schema,
390
+ element: parent
391
+ }) : void 0;
392
+ }
393
+ function elementToBlock({
394
+ schema,
395
+ element
396
+ }) {
397
+ return fromSlateValue([element], schema.block.name)?.at(0);
398
+ }
399
+ function isBlockElement(schema, node) {
400
+ return Element.isElement(node) && (schema.block.name === node._type || schema.blockObjects.some((blockObject) => blockObject.name === node._type));
401
+ }
340
402
  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 = createContext({}), PortableTextEditorContext = createContext(null), usePortableTextEditor = () => {
341
403
  const editor = useContext(PortableTextEditorContext);
342
404
  if (!editor)
@@ -2557,7 +2619,7 @@ const converterJson = {
2557
2619
  data: sliceBlocks({
2558
2620
  blocks: snapshot.context.value,
2559
2621
  selection
2560
- }).map((block) => isPortableTextTextBlock(block) ? block.children.map((child) => child._type === snapshot.context.schema.span.name ? child.text : "").join("") : "").filter((block) => block !== "").join(`
2622
+ }).map((block) => 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(`
2561
2623
 
2562
2624
  `),
2563
2625
  mimeType: "text/plain",
@@ -4296,36 +4358,7 @@ const addAnnotationActionImplementation = ({
4296
4358
  action
4297
4359
  }) => {
4298
4360
  console.warn(`Deserialization of ${action.mimeType} failed with reason "${action.reason}"`);
4299
- };
4300
- function getFocusBlock({
4301
- editor
4302
- }) {
4303
- return editor.selection ? Array.from(Editor.nodes(editor, {
4304
- at: editor.selection.focus.path.slice(0, 1),
4305
- match: (n) => !Editor.isEditor(n)
4306
- })).at(0) ?? [void 0, void 0] : [void 0, void 0];
4307
- }
4308
- function getFocusChild({
4309
- editor
4310
- }) {
4311
- const [focusBlock, focusBlockPath] = getFocusBlock({
4312
- editor
4313
- }), childIndex = editor.selection?.focus.path.at(1);
4314
- if (!focusBlock || !focusBlockPath || childIndex === void 0)
4315
- return [void 0, void 0];
4316
- const focusChild = Node.child(focusBlock, childIndex);
4317
- return focusChild ? [focusChild, [...focusBlockPath, childIndex]] : [void 0, void 0];
4318
- }
4319
- function getLastBlock({
4320
- editor
4321
- }) {
4322
- return Array.from(Editor.nodes(editor, {
4323
- match: (n) => !Editor.isEditor(n),
4324
- at: [],
4325
- reverse: !0
4326
- })).at(0) ?? [void 0, void 0];
4327
- }
4328
- const insertBlockActionImplementation = ({
4361
+ }, insertBlockActionImplementation = ({
4329
4362
  context,
4330
4363
  action
4331
4364
  }) => {
@@ -4613,15 +4646,17 @@ const selectActionImplementation = ({
4613
4646
  editor: action.editor,
4614
4647
  schema: context.schema
4615
4648
  }), index++;
4616
- } else
4649
+ } else {
4650
+ let index = 0;
4617
4651
  for (const block of fragment)
4618
4652
  insertBlock({
4619
4653
  block,
4620
- placement: "auto",
4654
+ placement: index === 0 ? "auto" : "after",
4621
4655
  select: "end",
4622
4656
  editor: action.editor,
4623
4657
  schema: context.schema
4624
- });
4658
+ }), index++;
4659
+ }
4625
4660
  }, deserializationSuccessActionImplementation = ({
4626
4661
  context,
4627
4662
  action
@@ -7698,6 +7733,9 @@ export {
7698
7733
  defineSchema,
7699
7734
  fromSlateValue,
7700
7735
  getEditorSnapshot,
7736
+ getFirstBlock,
7737
+ getLastBlock,
7738
+ getNodeBlock,
7701
7739
  isEqualToEmptyEditor,
7702
7740
  moveRangeByOperation,
7703
7741
  toPortableTextRange,