diff --git a/packages/editor/src/utils/index.ts b/packages/editor/src/utils/index.ts index ce55fa68..3c69d3e3 100644 --- a/packages/editor/src/utils/index.ts +++ b/packages/editor/src/utils/index.ts @@ -4,6 +4,7 @@ export { blockOffsetToSpanSelectionPoint, spanSelectionPointToBlockOffset, } from './util.block-offset' +export {getBlockEndPoint} from './util.get-block-end-point' export {getBlockStartPoint} from './util.get-block-start-point' export {getTextBlockText} from './util.get-text-block-text' export {isEmptyTextBlock} from './util.is-empty-text-block' diff --git a/packages/editor/src/utils/util.get-block-end-point.ts b/packages/editor/src/utils/util.get-block-end-point.ts new file mode 100644 index 00000000..463fdc33 --- /dev/null +++ b/packages/editor/src/utils/util.get-block-end-point.ts @@ -0,0 +1,34 @@ +import { + isPortableTextSpan, + isPortableTextTextBlock, + type KeyedSegment, + type PortableTextBlock, +} from '@sanity/types' +import type {EditorSelectionPoint} from '../types/editor' + +/** + * @public + */ +export function getBlockEndPoint({ + node, + path, +}: { + node: PortableTextBlock + path: [KeyedSegment] +}): EditorSelectionPoint { + if (isPortableTextTextBlock(node)) { + const lastChild = node.children[node.children.length - 1] + + if (lastChild) { + return { + path: [...path, 'children', {_key: lastChild._key}], + offset: isPortableTextSpan(lastChild) ? lastChild.text.length : 0, + } + } + } + + return { + path, + offset: 0, + } +}