UNPKG

@portabletext/editor

Version:

Portable Text Editor made in React

80 lines (79 loc) 2.26 kB
"use strict"; var selectionPoint = require("./selection-point.cjs"); function blockOffsetToBlockSelectionPoint({ context, blockOffset }) { let selectionPoint2; for (const block of context.value) if (block._key === blockOffset.path[0]._key) { selectionPoint2 = { path: [{ _key: block._key }], offset: blockOffset.offset }; break; } return selectionPoint2; } function blockOffsetToSelectionPoint({ context, blockOffset, direction }) { return selectionPoint.blockOffsetToSpanSelectionPoint({ context, blockOffset, direction }) || blockOffsetToBlockSelectionPoint({ context, blockOffset }); } function blockOffsetsToSelection({ context, offsets, backward }) { const anchor = blockOffsetToSelectionPoint({ context, blockOffset: offsets.anchor, direction: backward ? "backward" : "forward" }), focus = blockOffsetToSelectionPoint({ context, blockOffset: offsets.focus, direction: backward ? "forward" : "backward" }); return !anchor || !focus ? null : { anchor, focus, backward }; } function childSelectionPointToBlockOffset({ context, selectionPoint: selectionPoint$1 }) { let offset = 0; const blockKey = selectionPoint.getBlockKeyFromSelectionPoint(selectionPoint$1), childKey = selectionPoint.getChildKeyFromSelectionPoint(selectionPoint$1); if (!(!blockKey || !childKey)) { for (const block of context.value) if (block._key === blockKey && selectionPoint.isTextBlock(context, block)) for (const child of block.children) { if (child._key === childKey) return { path: [{ _key: block._key }], offset: offset + selectionPoint$1.offset }; selectionPoint.isSpan$1(context, child) && (offset += child.text.length); } } } exports.blockOffsetToBlockSelectionPoint = blockOffsetToBlockSelectionPoint; exports.blockOffsetToSelectionPoint = blockOffsetToSelectionPoint; exports.blockOffsetsToSelection = blockOffsetsToSelection; exports.childSelectionPointToBlockOffset = childSelectionPointToBlockOffset; //# sourceMappingURL=util.child-selection-point-to-block-offset.cjs.map