@portabletext/editor
Version:
Portable Text Editor made in React
130 lines (129 loc) • 3.71 kB
JavaScript
import { blockOffsetToSpanSelectionPoint, getAncestorTextBlock, isEqualSelectionPoints, parseBlock } from "../_chunks-es/util.slice-blocks.js";
import { getBlockEndPoint, getBlockStartPoint, getSelectionEndPoint, getSelectionStartPoint, isEqualPaths, isSelectionCollapsed, sliceBlocks, spanSelectionPointToBlockOffset } from "../_chunks-es/util.slice-blocks.js";
import { getNode, isKeyedSegment } from "../_chunks-es/get-ancestor.js";
import { isSpan, isTextBlock } from "@portabletext/schema";
import { isSpan as isSpan2, isTextBlock as isTextBlock2 } from "@portabletext/schema";
import { getTextBlockText, isEmptyTextBlock } from "../_chunks-es/util.is-empty-text-block.js";
function blockOffsetToBlockSelectionPoint({
snapshot,
blockOffset
}) {
const blockEntry = getNode(snapshot, blockOffset.path);
if (blockEntry)
return {
path: blockEntry.path,
offset: blockOffset.offset
};
}
function blockOffsetToSelectionPoint({
snapshot,
blockOffset,
direction
}) {
return blockOffsetToSpanSelectionPoint({
snapshot,
blockOffset,
direction
}) || blockOffsetToBlockSelectionPoint({
snapshot,
blockOffset
});
}
function blockOffsetsToSelection({
snapshot,
offsets,
backward
}) {
const anchor = blockOffsetToSelectionPoint({
snapshot,
blockOffset: offsets.anchor,
direction: backward ? "backward" : "forward"
}), focus = blockOffsetToSelectionPoint({
snapshot,
blockOffset: offsets.focus,
direction: backward ? "forward" : "backward"
});
return !anchor || !focus ? null : {
anchor,
focus,
backward
};
}
function childSelectionPointToBlockOffset({
snapshot,
selectionPoint
}) {
const childSegment = selectionPoint.path.at(-1);
if (!isKeyedSegment(childSegment))
return;
const textBlock = getAncestorTextBlock(snapshot, selectionPoint.path);
if (!textBlock)
return;
let offset = 0;
for (const child of textBlock.node.children) {
if (child._key === childSegment._key)
return {
path: textBlock.path,
offset: offset + selectionPoint.offset
};
isSpan(snapshot.context, child) && (offset += child.text.length);
}
}
function isEqualSelections(a, b) {
return !a && !b ? !0 : !a || !b ? !1 : isEqualSelectionPoints(a.anchor, b.anchor) && isEqualSelectionPoints(a.focus, b.focus);
}
function mergeTextBlocks({
context,
targetBlock,
incomingBlock
}) {
const parsedIncomingBlock = parseBlock({
keyGenerator: context.keyGenerator,
block: incomingBlock,
options: {
normalize: !1,
removeUnusedMarkDefs: !0,
validateFields: !1
},
schema: context.schema
});
return !parsedIncomingBlock || !isTextBlock(context, parsedIncomingBlock) ? targetBlock : {
...targetBlock,
children: [...targetBlock.children, ...parsedIncomingBlock.children],
markDefs: [...targetBlock.markDefs ?? [], ...parsedIncomingBlock.markDefs ?? []]
};
}
function reverseSelection(selection) {
return selection && (selection.backward ? {
anchor: selection.focus,
focus: selection.anchor,
backward: !1
} : {
anchor: selection.focus,
focus: selection.anchor,
backward: !0
});
}
export {
blockOffsetToSpanSelectionPoint,
blockOffsetsToSelection,
childSelectionPointToBlockOffset,
getBlockEndPoint,
getBlockStartPoint,
getSelectionEndPoint,
getSelectionStartPoint,
getTextBlockText,
isEmptyTextBlock,
isEqualPaths,
isEqualSelectionPoints,
isEqualSelections,
isKeyedSegment,
isSelectionCollapsed,
isSpan2 as isSpan,
isTextBlock2 as isTextBlock,
mergeTextBlocks,
reverseSelection,
sliceBlocks,
spanSelectionPointToBlockOffset
};
//# sourceMappingURL=index.js.map