UNPKG

@portabletext/editor

Version:

Portable Text Editor made in React

37 lines (30 loc) 998 B
import type {KeyedSegment} from '@portabletext/patches' import type {PortableTextObject, PortableTextSpan} from '@sanity/types' import type {EditorSelector} from '../editor/editor-selector' import {getChildKeyFromSelectionPoint} from '../selection/selection-point' import {getAnchorTextBlock} from './selector.get-anchor-text-block' /** * @public */ export const getAnchorChild: EditorSelector< | { node: PortableTextObject | PortableTextSpan path: [KeyedSegment, 'children', KeyedSegment] } | undefined > = (snapshot) => { if (!snapshot.context.selection) { return undefined } const anchorBlock = getAnchorTextBlock(snapshot) if (!anchorBlock) { return undefined } const key = getChildKeyFromSelectionPoint(snapshot.context.selection.anchor) const node = key ? anchorBlock.node.children.find((span) => span._key === key) : undefined return node && key ? {node, path: [...anchorBlock.path, 'children', {_key: key}]} : undefined }