@portabletext/editor
Version:
Portable Text Editor made in React
226 lines (225 loc) • 7.64 kB
JavaScript
import { getInline, getSelectionStartPoint, getSelectionEndPoint, getFocusTextBlock, getSelectionText, comparePaths } from "../_chunks-es/selector.is-at-the-start-of-block.js";
import { compareApplicableSchema, getActiveAnnotations, getActiveListItem, getActiveStyle, getApplicableSchema, getCaretWordSelection, getFirstBlock, getFocusBlock, getFocusBlockObject, getFocusChild, getFocusInlineObject, getFocusListBlock, getFocusSpan, getFragment, getLastBlock, getMarkState, getNextBlock, getNextInlineObject, getNextSpan, getPreviousBlock, getPreviousInlineObject, getPreviousSpan, getSelectedBlocks, getSelectedSpans, getSelectedTextBlocks, getSelectedValue, getSelectionEndBlock, getSelectionEndChild, getSelectionStartBlock, getSelectionStartChild, isActiveAnnotation, isActiveDecorator, isActiveListItem, isActiveStyle, isAtTheEndOfBlock, isAtTheStartOfBlock, isOverlappingSelection, isSelectingEntireBlocks, isSelectionCollapsed, isSelectionExpanded } from "../_chunks-es/selector.is-at-the-start-of-block.js";
import { getEnclosingBlock, parentPath } from "../_chunks-es/get-path-sub-schema.js";
import { isSpan, isTextBlock } from "@portabletext/schema";
import { spanSelectionPointToBlockOffset, getBlockEndPoint, getSelectionEndPoint as getSelectionEndPoint$1, getBlockStartPoint, getSelectionStartPoint as getSelectionStartPoint$1 } from "../_chunks-es/util.slice-blocks.js";
import { getChildren, isKeyedSegment, isObjectNode } from "../_chunks-es/get-ancestor.js";
const getAnchorBlock = (snapshot) => {
const selection = snapshot.context.selection;
if (selection)
return getEnclosingBlock(snapshot, selection.anchor.path);
}, getAnchorChild = (snapshot) => {
const selection = snapshot.context.selection;
if (selection)
return getInline(snapshot, selection.anchor.path);
}, getAnchorSpan = (snapshot) => {
const anchorChild = getAnchorChild(snapshot);
return anchorChild && isSpan(snapshot.context, anchorChild.node) ? {
node: anchorChild.node,
path: anchorChild.path
} : void 0;
}, getAnchorTextBlock = (snapshot) => {
const anchorBlock = getAnchorBlock(snapshot);
return anchorBlock && isTextBlock(snapshot.context, anchorBlock.node) ? {
node: anchorBlock.node,
path: anchorBlock.path
} : void 0;
}, getBlockOffsets = (snapshot) => {
if (!snapshot.context.selection)
return;
const selectionStartPoint = getSelectionStartPoint(snapshot), selectionEndPoint = getSelectionEndPoint(snapshot);
if (!selectionStartPoint || !selectionEndPoint)
return;
const start = spanSelectionPointToBlockOffset({
snapshot,
selectionPoint: selectionStartPoint
}), end = spanSelectionPointToBlockOffset({
snapshot,
selectionPoint: selectionEndPoint
});
return start && end ? {
start,
end
} : void 0;
}, getNextInlineObjects = (snapshot) => {
const point = getSelectionEndPoint(snapshot);
if (!point)
return [];
const endSegment = point.path.at(-1), endKey = isKeyedSegment(endSegment) ? endSegment._key : void 0;
if (!endKey)
return [];
const children = getChildren(snapshot, parentPath(point.path)), inlineObjects = [];
let endFound = !1;
for (const child of children) {
const segment = child.path.at(-1);
if ((isKeyedSegment(segment) ? segment._key : void 0) === endKey) {
endFound = !0;
continue;
}
endFound && isObjectNode({
schema: snapshot.context.schema
}, child.node) && inlineObjects.push({
node: child.node,
path: child.path
});
}
return inlineObjects;
}, getPreviousInlineObjects = (snapshot) => {
const point = getSelectionStartPoint(snapshot);
if (!point)
return [];
const startSegment = point.path.at(-1), startKey = isKeyedSegment(startSegment) ? startSegment._key : void 0;
if (!startKey)
return [];
const children = getChildren(snapshot, parentPath(point.path)), inlineObjects = [];
for (const child of children) {
const segment = child.path.at(-1);
if ((isKeyedSegment(segment) ? segment._key : void 0) === startKey)
break;
isObjectNode({
schema: snapshot.context.schema
}, child.node) && inlineObjects.push({
node: child.node,
path: child.path
});
}
return inlineObjects;
}, getSelection = (snapshot) => snapshot.context.selection, getBlockTextAfter = (snapshot) => {
if (!snapshot.context.selection)
return "";
const endPoint = getSelectionEndPoint$1(snapshot.context.selection), block = getFocusTextBlock({
...snapshot,
context: {
...snapshot.context,
selection: {
anchor: endPoint,
focus: endPoint
}
}
});
if (!block)
return "";
const endOfBlock = getBlockEndPoint({
context: snapshot.context,
block
});
return getSelectionText({
context: {
...snapshot.context,
selection: {
anchor: endPoint,
focus: endOfBlock
}
}
});
}, getBlockTextBefore = (snapshot) => {
if (!snapshot.context.selection)
return "";
const startPoint = getSelectionStartPoint$1(snapshot.context.selection), block = getFocusTextBlock({
...snapshot,
context: {
...snapshot.context,
selection: {
anchor: startPoint,
focus: startPoint
}
}
});
if (!block)
return "";
const startOfBlock = getBlockStartPoint({
context: snapshot.context,
block
});
return getSelectionText({
context: {
...snapshot.context,
selection: {
anchor: startOfBlock,
focus: startPoint
}
}
});
}, getValue = (snapshot) => snapshot.context.value;
function comparePoints(snapshot, pointA, pointB) {
const pathComparison = comparePaths(pointA.path, pointB.path, {
children: snapshot.context.value
});
return pathComparison !== 0 ? pathComparison : pointA.offset < pointB.offset ? -1 : pointA.offset > pointB.offset ? 1 : 0;
}
function isPointAfterSelection(point) {
return (snapshot) => {
if (!snapshot.context.selection)
return !1;
const endPoint = getSelectionEndPoint$1(snapshot.context.selection);
return comparePoints(snapshot, point, endPoint) === 1;
};
}
function isPointBeforeSelection(point) {
return (snapshot) => {
if (!snapshot.context.selection)
return !1;
const startPoint = getSelectionStartPoint$1(snapshot.context.selection);
return comparePoints(snapshot, point, startPoint) === -1;
};
}
export {
compareApplicableSchema,
getActiveAnnotations,
getActiveListItem,
getActiveStyle,
getAnchorBlock,
getAnchorChild,
getAnchorSpan,
getAnchorTextBlock,
getApplicableSchema,
getBlockOffsets,
getBlockTextAfter,
getBlockTextBefore,
getCaretWordSelection,
getFirstBlock,
getFocusBlock,
getFocusBlockObject,
getFocusChild,
getFocusInlineObject,
getFocusListBlock,
getFocusSpan,
getFocusTextBlock,
getFragment,
getLastBlock,
getMarkState,
getNextBlock,
getNextInlineObject,
getNextInlineObjects,
getNextSpan,
getPreviousBlock,
getPreviousInlineObject,
getPreviousInlineObjects,
getPreviousSpan,
getSelectedBlocks,
getSelectedSpans,
getSelectedTextBlocks,
getSelectedValue,
getSelection,
getSelectionEndBlock,
getSelectionEndChild,
getSelectionEndPoint,
getSelectionStartBlock,
getSelectionStartChild,
getSelectionStartPoint,
getSelectionText,
getValue,
isActiveAnnotation,
isActiveDecorator,
isActiveListItem,
isActiveStyle,
isAtTheEndOfBlock,
isAtTheStartOfBlock,
isOverlappingSelection,
isPointAfterSelection,
isPointBeforeSelection,
isSelectingEntireBlocks,
isSelectionCollapsed,
isSelectionExpanded
};
//# sourceMappingURL=index.js.map