@portabletext/editor 1.40.3 → 1.40.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.
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ function isSelectionCollapsed(selection) {
3
+ return selection ? selection.anchor.path.join() === selection.focus.path.join() && selection.anchor.offset === selection.focus.offset : !1;
4
+ }
5
+ exports.isSelectionCollapsed = isSelectionCollapsed;
6
+ //# sourceMappingURL=util.is-selection-collapsed.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.is-selection-collapsed.cjs","sources":["../../src/utils/util.is-selection-collapsed.ts"],"sourcesContent":["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":["isSelectionCollapsed","selection","anchor","path","join","focus","offset"],"mappings":";AAKO,SAASA,qBAAqBC,WAA4B;AAC/D,SAAKA,YAKHA,UAAUC,OAAOC,KAAKC,KAAAA,MAAWH,UAAUI,MAAMF,KAAKC,KAAAA,KACtDH,UAAUC,OAAOI,WAAWL,UAAUI,MAAMC,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,QACzBC,gBAGAC,sBAAsB;AAE1B,aAAWC,SAASP;AACdO,QAAAA,MAAMC,SAASP,YAAYQ,KAAK,CAAC,EAAED,QAIlCE,8BAAwBH,KAAK;AAIvBI,iBAAAA,SAASJ,MAAMK,UAAU;AAClC,YAAIV,cAAc,WAAW;AACvB,cAAA,CAACW,yBAAmBF,KAAK;AAC3B;AAGER,cAAAA,cAAcQ,MAAMG,KAAKC,QAAQ;AAClB,6BAAA;AAAA,cACfN,MAAM,CAAC,GAAGR,YAAYQ,MAAM,YAAY;AAAA,gBAACD,MAAMG,MAAMH;AAAAA,cAAAA,CAAK;AAAA,cAC1DJ,QAAQD;AAAAA,YACV;AACA;AAAA,UAAA;AAGFA,wBAAcQ,MAAMG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AAkBE;AACpB;AAAA,IAAA;AAIAlD,QAAAA,MAAMC,SAASiD,aAAa;AAC1B,UAAA,CAAC/C,MAAAA,wBAAwBH,KAAK,GAAG;AACxBA,mBAAAA;AACX;AAAA,MAAA;AAGF,UAAIoD,aAAa;AACJ,mBAAA;AAAA,UACT,GAAGpD;AAAAA,UACHK,UAAU,CAAA;AAAA,QACZ;AAEA,mBAAWD,SAASJ,MAAMK;AACpBwC,cAAAA,YAAY1C,8BAAwB0C,QAAQ,GAAG;AACjD,gBAAIzC,MAAMH,SAASmD,eAAe9C,MAAAA,mBAAmBF,KAAK,GAAG;AAC3DyC,uBAASxC,SAASgD,KAAK;AAAA,gBACrB,GAAGjD;AAAAA,gBACHG,MAAMH,MAAMG,KAAKmC,MAAM,GAAGK,SAASlD,MAAM;AAAA,cAAA,CAC1C;AAED;AAAA,YAAA;AAKF,gBAFAgD,SAASxC,SAASgD,KAAKjD,KAAK,GAExBgD,eAAehD,MAAMH,SAASmD;AAChC;AAAA,UAAA;AAKN;AAAA,MAAA;AAGSpD,iBAAAA;AAEX;AAAA,IAAA;AAGE2C,kBACFC,aAAaS,KAAKrD,KAAK;AAAA,EAAA;AAI3B,SAAO,CACL,GAAI2C,aAAa,CAACA,UAAU,IAAI,CAAA,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,EACAC;AAKF,GAAG;AACD,MAAIC,aAAaF,YAAYG,QACzBC,gBAGAC,sBAAsB;AAE1B,aAAWC,SAASP;AACdO,QAAAA,MAAMC,SAASP,YAAYQ,KAAK,CAAC,EAAED,QAIlCE,8BAAwBH,KAAK;AAIvBI,iBAAAA,SAASJ,MAAMK,UAAU;AAClC,YAAIV,cAAc,WAAW;AACvB,cAAA,CAACW,yBAAmBF,KAAK;AAC3B;AAGER,cAAAA,cAAcQ,MAAMG,KAAKC,QAAQ;AAClB,6BAAA;AAAA,cACfN,MAAM,CAAC,GAAGR,YAAYQ,MAAM,YAAY;AAAA,gBAACD,MAAMG,MAAMH;AAAAA,cAAAA,CAAK;AAAA,cAC1DJ,QAAQD;AAAAA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;AAAA,MAAA;AAGFP,UAAAA,aAAa3C,OAETgD,kBAAkBE;AACpB;AAAA,IAAA;AAIAlD,QAAAA,MAAMC,SAASiD,aAAa;AAC1B,UAAA,CAAC/C,MAAAA,wBAAwBH,KAAK,GAAG;AACxBA,mBAAAA;AACX;AAAA,MAAA;AAGF,UAAIoD,aAAa;AACJ,mBAAA;AAAA,UACT,GAAGpD;AAAAA,UACHK,UAAU,CAAA;AAAA,QACZ;AAEA,mBAAWD,SAASJ,MAAMK;AACpBwC,cAAAA,YAAY1C,8BAAwB0C,QAAQ,GAAG;AACjD,gBAAIzC,MAAMH,SAASmD,eAAe9C,MAAAA,mBAAmBF,KAAK,GAAG;AAC3DyC,uBAASxC,SAASgD,KAAK;AAAA,gBACrB,GAAGjD;AAAAA,gBACHG,MAAMH,MAAMG,KAAKmC,MAAM,GAAGK,SAASlD,MAAM;AAAA,cAAA,CAC1C;AAED;AAAA,YAAA;AAKF,gBAFAgD,SAASxC,SAASgD,KAAKjD,KAAK,GAExBgD,eAAehD,MAAMH,SAASmD;AAChC;AAAA,UAAA;AAKN;AAAA,MAAA;AAGSpD,iBAAAA;AAEX;AAAA,IAAA;AAGE2C,kBACFC,aAAaS,KAAKrD,KAAK;AAAA,EAAA;AAI3B,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,65 @@ 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 getLastBlock({
360
+ editor
361
+ }) {
362
+ return Array.from(Editor.nodes(editor, {
363
+ match: (n) => !Editor.isEditor(n),
364
+ at: [],
365
+ reverse: !0
366
+ })).at(0) ?? [void 0, void 0];
367
+ }
368
+ function getNodeBlock({
369
+ editor,
370
+ schema,
371
+ node
372
+ }) {
373
+ if (Editor.isEditor(node))
374
+ return;
375
+ if (isBlockElement(schema, node))
376
+ return elementToBlock({
377
+ schema,
378
+ element: node
379
+ });
380
+ const parent = Array.from(Editor.nodes(editor, {
381
+ mode: "highest",
382
+ at: [],
383
+ match: (n) => isBlockElement(schema, n) && n.children.some((child) => child._key === node._key)
384
+ })).at(0)?.at(0);
385
+ return Element.isElement(parent) ? elementToBlock({
386
+ schema,
387
+ element: parent
388
+ }) : void 0;
389
+ }
390
+ function elementToBlock({
391
+ schema,
392
+ element
393
+ }) {
394
+ return fromSlateValue([element], schema.block.name)?.at(0);
395
+ }
396
+ function isBlockElement(schema, node) {
397
+ return Element.isElement(node) && (schema.block.name === node._type || schema.blockObjects.some((blockObject) => blockObject.name === node._type));
398
+ }
340
399
  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
400
  const editor = useContext(PortableTextEditorContext);
342
401
  if (!editor)
@@ -4296,36 +4355,7 @@ const addAnnotationActionImplementation = ({
4296
4355
  action
4297
4356
  }) => {
4298
4357
  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 = ({
4358
+ }, insertBlockActionImplementation = ({
4329
4359
  context,
4330
4360
  action
4331
4361
  }) => {
@@ -4613,15 +4643,17 @@ const selectActionImplementation = ({
4613
4643
  editor: action.editor,
4614
4644
  schema: context.schema
4615
4645
  }), index++;
4616
- } else
4646
+ } else {
4647
+ let index = 0;
4617
4648
  for (const block of fragment)
4618
4649
  insertBlock({
4619
4650
  block,
4620
- placement: "auto",
4651
+ placement: index === 0 ? "auto" : "after",
4621
4652
  select: "end",
4622
4653
  editor: action.editor,
4623
4654
  schema: context.schema
4624
- });
4655
+ }), index++;
4656
+ }
4625
4657
  }, deserializationSuccessActionImplementation = ({
4626
4658
  context,
4627
4659
  action
@@ -7698,6 +7730,7 @@ export {
7698
7730
  defineSchema,
7699
7731
  fromSlateValue,
7700
7732
  getEditorSnapshot,
7733
+ getNodeBlock,
7701
7734
  isEqualToEmptyEditor,
7702
7735
  moveRangeByOperation,
7703
7736
  toPortableTextRange,