@portabletext/editor
Version:
Portable Text Editor made in React
208 lines (207 loc) • 7.33 kB
JavaScript
import { isKeySegment } from "@sanity/types";
import { getBlockKeyFromSelectionPoint, isTextBlock, isSpan$1 as isSpan, isListBlock, getSelectionStartPoint as getSelectionStartPoint$1, getSelectionEndPoint, getChildKeyFromSelectionPoint, isSpan as isSpan$1, sliceBlocks } from "./selection-point.js";
const getSelectionStartPoint = (snapshot) => {
if (snapshot.context.selection)
return snapshot.context.selection.backward ? snapshot.context.selection.focus : snapshot.context.selection.anchor;
}, isSelectionCollapsed = (snapshot) => snapshot.context.selection ? JSON.stringify(snapshot.context.selection.anchor.path) === JSON.stringify(snapshot.context.selection.focus.path) && snapshot.context.selection?.anchor.offset === snapshot.context.selection?.focus.offset : !1, getFocusBlock = (snapshot) => {
if (!snapshot.context.selection)
return;
const key = getBlockKeyFromSelectionPoint(snapshot.context.selection.focus), node = key ? snapshot.context.value.find((block) => block._key === key) : void 0;
return node && key ? {
node,
path: [{
_key: key
}]
} : void 0;
}, getFocusListBlock = (snapshot) => {
const focusTextBlock = getFocusTextBlock(snapshot);
return focusTextBlock && isListBlock(snapshot.context, focusTextBlock.node) ? {
node: focusTextBlock.node,
path: focusTextBlock.path
} : void 0;
}, getFocusTextBlock = (snapshot) => {
const focusBlock = getFocusBlock(snapshot);
return focusBlock && isTextBlock(snapshot.context, focusBlock.node) ? {
node: focusBlock.node,
path: focusBlock.path
} : void 0;
}, getFocusBlockObject = (snapshot) => {
const focusBlock = getFocusBlock(snapshot);
return focusBlock && !isTextBlock(snapshot.context, focusBlock.node) ? {
node: focusBlock.node,
path: focusBlock.path
} : void 0;
}, getFocusChild = (snapshot) => {
if (!snapshot.context.selection)
return;
const focusBlock = getFocusTextBlock(snapshot);
if (!focusBlock)
return;
const key = getChildKeyFromSelectionPoint(snapshot.context.selection.focus), node = key ? focusBlock.node.children.find((span) => span._key === key) : void 0;
return node && key ? {
node,
path: [...focusBlock.path, "children", {
_key: key
}]
} : void 0;
}, getFocusSpan = (snapshot) => {
const focusChild = getFocusChild(snapshot);
return focusChild && isSpan(snapshot.context, focusChild.node) ? {
node: focusChild.node,
path: focusChild.path
} : void 0;
}, getFirstBlock = (snapshot) => {
const node = snapshot.context.value[0];
return node ? {
node,
path: [{
_key: node._key
}]
} : void 0;
}, getLastBlock = (snapshot) => {
const node = snapshot.context.value[snapshot.context.value.length - 1] ? snapshot.context.value[snapshot.context.value.length - 1] : void 0;
return node ? {
node,
path: [{
_key: node._key
}]
} : void 0;
}, getSelectedBlocks = (snapshot) => {
if (!snapshot.context.selection)
return [];
const selectedBlocks = [], startPoint = getSelectionStartPoint$1(snapshot.context.selection), endPoint = getSelectionEndPoint(snapshot.context.selection), startKey = getBlockKeyFromSelectionPoint(startPoint), endKey = getBlockKeyFromSelectionPoint(endPoint);
if (!startKey || !endKey)
return selectedBlocks;
for (const block of snapshot.context.value) {
if (block._key === startKey) {
if (selectedBlocks.push({
node: block,
path: [{
_key: block._key
}]
}), startKey === endKey)
break;
continue;
}
if (block._key === endKey) {
selectedBlocks.push({
node: block,
path: [{
_key: block._key
}]
});
break;
}
selectedBlocks.length > 0 && selectedBlocks.push({
node: block,
path: [{
_key: block._key
}]
});
}
return selectedBlocks;
}, getSelectionStartBlock = (snapshot) => {
if (!snapshot.context.selection)
return;
const startPoint = getSelectionStartPoint$1(snapshot.context.selection), key = getBlockKeyFromSelectionPoint(startPoint), node = key ? snapshot.context.value.find((block) => block._key === key) : void 0;
return node && key ? {
node,
path: [{
_key: key
}]
} : void 0;
}, getSelectionEndBlock = (snapshot) => {
if (!snapshot.context.selection)
return;
const endPoint = getSelectionEndPoint(snapshot.context.selection), key = getBlockKeyFromSelectionPoint(endPoint), node = key ? snapshot.context.value.find((block) => block._key === key) : void 0;
return node && key ? {
node,
path: [{
_key: key
}]
} : void 0;
}, getPreviousBlock = (snapshot) => {
let previousBlock;
const selectionStartBlock = getSelectionStartBlock(snapshot);
if (!selectionStartBlock)
return;
let foundSelectionStartBlock = !1;
for (const block of snapshot.context.value) {
if (block._key === selectionStartBlock.node._key) {
foundSelectionStartBlock = !0;
break;
}
previousBlock = {
node: block,
path: [{
_key: block._key
}]
};
}
if (foundSelectionStartBlock && previousBlock)
return previousBlock;
}, getNextBlock = (snapshot) => {
let nextBlock;
const selectionEndBlock = getSelectionEndBlock(snapshot);
if (!selectionEndBlock)
return;
let foundSelectionEndBlock = !1;
for (const block of snapshot.context.value) {
if (block._key === selectionEndBlock.node._key) {
foundSelectionEndBlock = !0;
continue;
}
if (foundSelectionEndBlock) {
nextBlock = {
node: block,
path: [{
_key: block._key
}]
};
break;
}
}
if (foundSelectionEndBlock && nextBlock)
return nextBlock;
}, getPreviousInlineObject = (snapshot) => {
const focusTextBlock = getFocusTextBlock(snapshot), selectionStartPoint = getSelectionStartPoint(snapshot), selectionStartPointChildKey = selectionStartPoint && isKeySegment(selectionStartPoint.path[2]) ? selectionStartPoint.path[2]._key : void 0;
if (!focusTextBlock || !selectionStartPointChildKey)
return;
let inlineObject;
for (const child of focusTextBlock.node.children) {
if (child._key === selectionStartPointChildKey)
break;
isSpan$1(snapshot.context, child) || (inlineObject = {
node: child,
path: [...focusTextBlock.path, "children", {
_key: child._key
}]
});
}
return inlineObject;
}, getSelectedSlice = (snapshot) => sliceBlocks({
context: snapshot.context,
blocks: snapshot.context.value
}), getSelectionText = (snapshot) => getSelectedSlice(snapshot).reduce((text, block) => isTextBlock(snapshot.context, block) ? text + block.children.reduce((text2, child) => isSpan(snapshot.context, child) ? text2 + child.text : text2, "") : text, ""), isSelectionExpanded = (snapshot) => !isSelectionCollapsed(snapshot);
export {
getFirstBlock,
getFocusBlock,
getFocusBlockObject,
getFocusChild,
getFocusListBlock,
getFocusSpan,
getFocusTextBlock,
getLastBlock,
getNextBlock,
getPreviousBlock,
getPreviousInlineObject,
getSelectedBlocks,
getSelectedSlice,
getSelectionEndBlock,
getSelectionStartBlock,
getSelectionStartPoint,
getSelectionText,
isSelectionCollapsed,
isSelectionExpanded
};
//# sourceMappingURL=selector.is-selection-expanded.js.map