@portabletext/editor
Version:
Portable Text Editor made in React
206 lines (205 loc) • 7.89 kB
JavaScript
;
var types = require("@sanity/types"), selectionPoint = require("./selection-point.cjs");
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 = selectionPoint.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 && selectionPoint.isListBlock(snapshot.context, focusTextBlock.node) ? {
node: focusTextBlock.node,
path: focusTextBlock.path
} : void 0;
}, getFocusTextBlock = (snapshot) => {
const focusBlock = getFocusBlock(snapshot);
return focusBlock && selectionPoint.isTextBlock(snapshot.context, focusBlock.node) ? {
node: focusBlock.node,
path: focusBlock.path
} : void 0;
}, getFocusBlockObject = (snapshot) => {
const focusBlock = getFocusBlock(snapshot);
return focusBlock && !selectionPoint.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 = selectionPoint.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 && selectionPoint.isSpan$1(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 = selectionPoint.getSelectionStartPoint(snapshot.context.selection), endPoint = selectionPoint.getSelectionEndPoint(snapshot.context.selection), startKey = selectionPoint.getBlockKeyFromSelectionPoint(startPoint), endKey = selectionPoint.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 = selectionPoint.getSelectionStartPoint(snapshot.context.selection), key = selectionPoint.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 = selectionPoint.getSelectionEndPoint(snapshot.context.selection), key = selectionPoint.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 && types.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;
selectionPoint.isSpan(snapshot.context, child) || (inlineObject = {
node: child,
path: [...focusTextBlock.path, "children", {
_key: child._key
}]
});
}
return inlineObject;
}, getSelectedSlice = (snapshot) => selectionPoint.sliceBlocks({
context: snapshot.context,
blocks: snapshot.context.value
}), getSelectionText = (snapshot) => getSelectedSlice(snapshot).reduce((text, block) => selectionPoint.isTextBlock(snapshot.context, block) ? text + block.children.reduce((text2, child) => selectionPoint.isSpan$1(snapshot.context, child) ? text2 + child.text : text2, "") : text, ""), isSelectionExpanded = (snapshot) => !isSelectionCollapsed(snapshot);
exports.getFirstBlock = getFirstBlock;
exports.getFocusBlock = getFocusBlock;
exports.getFocusBlockObject = getFocusBlockObject;
exports.getFocusChild = getFocusChild;
exports.getFocusListBlock = getFocusListBlock;
exports.getFocusSpan = getFocusSpan;
exports.getFocusTextBlock = getFocusTextBlock;
exports.getLastBlock = getLastBlock;
exports.getNextBlock = getNextBlock;
exports.getPreviousBlock = getPreviousBlock;
exports.getPreviousInlineObject = getPreviousInlineObject;
exports.getSelectedBlocks = getSelectedBlocks;
exports.getSelectedSlice = getSelectedSlice;
exports.getSelectionEndBlock = getSelectionEndBlock;
exports.getSelectionStartBlock = getSelectionStartBlock;
exports.getSelectionStartPoint = getSelectionStartPoint;
exports.getSelectionText = getSelectionText;
exports.isSelectionCollapsed = isSelectionCollapsed;
exports.isSelectionExpanded = isSelectionExpanded;
//# sourceMappingURL=selector.is-selection-expanded.cjs.map