Skip to content

Commit

Permalink
feat(utils): add getBlockEndPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhg committed Jan 23, 2025
1 parent 3622e7d commit e9d208f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/editor/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
34 changes: 34 additions & 0 deletions packages/editor/src/utils/util.get-block-end-point.ts
Original file line number Diff line number Diff line change
@@ -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,
}
}

0 comments on commit e9d208f

Please sign in to comment.