@lexical/selection
Advanced tools
Comparing version 0.12.2 to 0.12.3
@@ -10,2 +10,3 @@ /** | ||
var lexical = require('lexical'); | ||
var utils = require('@lexical/utils'); | ||
@@ -28,6 +29,4 @@ /** | ||
*/ | ||
function getDOMTextNode(element) { | ||
let node = element; | ||
while (node != null) { | ||
@@ -37,18 +36,14 @@ if (node.nodeType === Node.TEXT_NODE) { | ||
} | ||
node = node.firstChild; | ||
} | ||
return null; | ||
} | ||
function getDOMIndexWithinParent(node) { | ||
const parent = node.parentNode; | ||
if (parent == null) { | ||
throw new Error('Should never happen'); | ||
} | ||
return [parent, Array.from(parent.childNodes).indexOf(node)]; | ||
} | ||
/** | ||
@@ -63,4 +58,2 @@ * Creates a selection range for the DOM. | ||
*/ | ||
function createDOMRange(editor, anchorNode, _anchorOffset, focusNode, _focusOffset) { | ||
@@ -74,29 +67,21 @@ const anchorKey = anchorNode.getKey(); | ||
let focusOffset = _focusOffset; | ||
if (lexical.$isTextNode(anchorNode)) { | ||
anchorDOM = getDOMTextNode(anchorDOM); | ||
} | ||
if (lexical.$isTextNode(focusNode)) { | ||
focusDOM = getDOMTextNode(focusDOM); | ||
} | ||
if (anchorNode === undefined || focusNode === undefined || anchorDOM === null || focusDOM === null) { | ||
return null; | ||
} | ||
if (anchorDOM.nodeName === 'BR') { | ||
[anchorDOM, anchorOffset] = getDOMIndexWithinParent(anchorDOM); | ||
} | ||
if (focusDOM.nodeName === 'BR') { | ||
[focusDOM, focusOffset] = getDOMIndexWithinParent(focusDOM); | ||
} | ||
const firstChild = anchorDOM.firstChild; | ||
if (anchorDOM === focusDOM && firstChild != null && firstChild.nodeName === 'BR' && anchorOffset === 0 && focusOffset === 0) { | ||
focusOffset = 1; | ||
} | ||
try { | ||
@@ -108,3 +93,2 @@ range.setStart(anchorDOM, anchorOffset); | ||
} | ||
if (range.collapsed && (anchorOffset !== focusOffset || anchorKey !== focusKey)) { | ||
@@ -115,5 +99,5 @@ // Range is backwards, we need to reverse it | ||
} | ||
return range; | ||
} | ||
/** | ||
@@ -125,10 +109,7 @@ * Creates DOMRects, generally used to help the editor find a specific location on the screen. | ||
*/ | ||
function createRectsFromDOMRange(editor, range) { | ||
const rootElement = editor.getRootElement(); | ||
if (rootElement === null) { | ||
return []; | ||
} | ||
const rootRect = rootElement.getBoundingClientRect(); | ||
@@ -138,23 +119,20 @@ const computedStyle = getComputedStyle(rootElement); | ||
const selectionRects = Array.from(range.getClientRects()); | ||
let selectionRectsLength = selectionRects.length; //sort rects from top left to bottom right. | ||
let selectionRectsLength = selectionRects.length; | ||
//sort rects from top left to bottom right. | ||
selectionRects.sort((a, b) => { | ||
const top = a.top - b.top; // Some rects match position closely, but not perfectly, | ||
const top = a.top - b.top; | ||
// Some rects match position closely, but not perfectly, | ||
// so we give a 3px tolerance. | ||
if (Math.abs(top) <= 3) { | ||
return a.left - b.left; | ||
} | ||
return top; | ||
}); | ||
let prevRect; | ||
for (let i = 0; i < selectionRectsLength; i++) { | ||
const selectionRect = selectionRects[i]; // Exclude rects that overlap preceding Rects in the sorted list. | ||
const isOverlappingRect = prevRect && prevRect.top <= selectionRect.top && prevRect.top + prevRect.height > selectionRect.top && prevRect.left + prevRect.width > selectionRect.left; // Exclude selections that span the entire element | ||
const selectionRect = selectionRects[i]; | ||
// Exclude rects that overlap preceding Rects in the sorted list. | ||
const isOverlappingRect = prevRect && prevRect.top <= selectionRect.top && prevRect.top + prevRect.height > selectionRect.top && prevRect.left + prevRect.width > selectionRect.left; | ||
// Exclude selections that span the entire element | ||
const selectionSpansElement = selectionRect.width + rootPadding === rootRect.width; | ||
if (isOverlappingRect || selectionSpansElement) { | ||
@@ -165,8 +143,7 @@ selectionRects.splice(i--, 1); | ||
} | ||
prevRect = selectionRect; | ||
} | ||
return selectionRects; | ||
} | ||
/** | ||
@@ -177,17 +154,14 @@ * Creates an object containing all the styles and their values provided in the CSS string. | ||
*/ | ||
function getStyleObjectFromRawCSS(css) { | ||
const styleObject = {}; | ||
const styles = css.split(';'); | ||
for (const style of styles) { | ||
if (style !== '') { | ||
const [key, value] = style.split(/:([^]+)/); // split on first colon | ||
styleObject[key.trim()] = value.trim(); | ||
} | ||
} | ||
return styleObject; | ||
} | ||
/** | ||
@@ -198,6 +172,4 @@ * Given a CSS string, returns an object from the style cache. | ||
*/ | ||
function getStyleObjectFromCSS(css) { | ||
let value = CSS_TO_STYLES.get(css); | ||
if (value === undefined) { | ||
@@ -207,3 +179,2 @@ value = getStyleObjectFromRawCSS(css); | ||
} | ||
{ | ||
@@ -213,5 +184,5 @@ // Freeze the value in DEV to prevent accidental mutations | ||
} | ||
return value; | ||
} | ||
/** | ||
@@ -222,6 +193,4 @@ * Gets the CSS styles from the style object. | ||
*/ | ||
function getCSSFromStyleObject(styles) { | ||
let css = ''; | ||
for (const style in styles) { | ||
@@ -232,3 +201,2 @@ if (style) { | ||
} | ||
return css; | ||
@@ -244,3 +212,2 @@ } | ||
*/ | ||
function $updateElementNodeProperties(target, source) { | ||
@@ -255,3 +222,2 @@ target.__first = source.__first; | ||
} | ||
function $updateTextNodeProperties(target, source) { | ||
@@ -264,2 +230,3 @@ target.__format = source.__format; | ||
} | ||
/** | ||
@@ -270,7 +237,5 @@ * Returns a copy of a node, but generates a new key for the copy. | ||
*/ | ||
function $cloneWithProperties(node) { | ||
const constructor = node.constructor; // @ts-expect-error | ||
const constructor = node.constructor; | ||
// @ts-expect-error | ||
const clone = constructor.clone(node); | ||
@@ -280,13 +245,11 @@ clone.__parent = node.__parent; | ||
clone.__prev = node.__prev; | ||
if (lexical.$isElementNode(node) && lexical.$isElementNode(clone)) { | ||
return $updateElementNodeProperties(clone, node); | ||
} | ||
if (lexical.$isTextNode(node) && lexical.$isTextNode(clone)) { | ||
return $updateTextNodeProperties(clone, node); | ||
} | ||
return clone; | ||
} | ||
/** | ||
@@ -299,3 +262,2 @@ * Generally used to append text content to HTML and JSON. Grabs the text content and "slices" | ||
*/ | ||
function $sliceSelectedTextNodeContent(selection, textNode) { | ||
@@ -307,3 +269,2 @@ if (textNode.isSelected() && !textNode.isSegmented() && !textNode.isToken() && (lexical.$isRangeSelection(selection) || lexical.DEPRECATED_$isGridSelection(selection))) { | ||
const isFocus = textNode.is(focusNode); | ||
if (isAnchor || isFocus) { | ||
@@ -317,3 +278,2 @@ const isBackward = selection.isBackward(); | ||
let endOffset = undefined; | ||
if (isSame) { | ||
@@ -331,3 +291,2 @@ startOffset = anchorOffset > focusOffset ? focusOffset : anchorOffset; | ||
} | ||
textNode.__text = textNode.__text.slice(startOffset, endOffset); | ||
@@ -337,5 +296,5 @@ return textNode; | ||
} | ||
return textNode; | ||
} | ||
/** | ||
@@ -346,3 +305,2 @@ * Determines if the current selection is at the end of the node. | ||
*/ | ||
function $isAtNodeEnd(point) { | ||
@@ -352,5 +310,5 @@ if (point.type === 'text') { | ||
} | ||
return point.offset === point.getNode().getChildrenSize(); | ||
} | ||
/** | ||
@@ -364,3 +322,2 @@ * Trims text from a node in order to shorten it, eg. to enforce a text's max length. If it deletes text | ||
*/ | ||
function trimTextContentFromAnchor(editor, anchor, delCount) { | ||
@@ -370,6 +327,4 @@ // Work from the current selection anchor point | ||
let remaining = delCount; | ||
if (lexical.$isElementNode(currentNode)) { | ||
const descendantNode = currentNode.getDescendantByIndex(anchor.offset); | ||
if (descendantNode !== null) { | ||
@@ -379,14 +334,16 @@ currentNode = descendantNode; | ||
} | ||
while (remaining > 0 && currentNode !== null) { | ||
if (lexical.$isElementNode(currentNode)) { | ||
const lastDescendant = currentNode.getLastDescendant(); | ||
if (lastDescendant !== null) { | ||
currentNode = lastDescendant; | ||
} | ||
} | ||
let nextNode = currentNode.getPreviousSibling(); | ||
let additionalElementWhitespace = 0; | ||
if (nextNode === null) { | ||
let parent = currentNode.getParentOrThrow(); | ||
let parentSibling = parent.getPreviousSibling(); | ||
while (parentSibling === null) { | ||
parent = parent.getParent(); | ||
if (parent === null) { | ||
@@ -396,20 +353,12 @@ nextNode = null; | ||
} | ||
parentSibling = parent.getPreviousSibling(); | ||
} | ||
if (parent !== null) { | ||
additionalElementWhitespace = parent.isInline() ? 0 : 2; | ||
if (lexical.$isElementNode(parentSibling)) { | ||
nextNode = parentSibling.getLastDescendant(); | ||
} else { | ||
nextNode = parentSibling; | ||
} | ||
nextNode = parentSibling; | ||
} | ||
} | ||
let text = currentNode.getTextContent(); // If the text is empty, we need to consider adding in two line breaks to match | ||
let text = currentNode.getTextContent(); | ||
// If the text is empty, we need to consider adding in two line breaks to match | ||
// the content if we were to get it from its parent. | ||
if (text === '' && lexical.$isElementNode(currentNode) && !currentNode.isInline()) { | ||
@@ -419,25 +368,19 @@ // TODO: should this be handled in core? | ||
} | ||
const currentNodeSize = text.length; | ||
if (!lexical.$isTextNode(currentNode) || remaining >= currentNodeSize) { | ||
const parent = currentNode.getParent(); | ||
currentNode.remove(); | ||
if (parent != null && parent.getChildrenSize() === 0 && !lexical.$isRootNode(parent)) { | ||
parent.remove(); | ||
} | ||
remaining -= currentNodeSize + additionalElementWhitespace; | ||
currentNode = nextNode; | ||
} else { | ||
const key = currentNode.getKey(); // See if we can just revert it to what was in the last editor state | ||
const key = currentNode.getKey(); | ||
// See if we can just revert it to what was in the last editor state | ||
const prevTextContent = editor.getEditorState().read(() => { | ||
const prevNode = lexical.$getNodeByKey(key); | ||
if (lexical.$isTextNode(prevNode) && prevNode.isSimpleText()) { | ||
return prevNode.getTextContent(); | ||
} | ||
return null; | ||
@@ -447,7 +390,5 @@ }); | ||
const slicedText = text.slice(0, offset); | ||
if (prevTextContent !== null && prevTextContent !== text) { | ||
const prevSelection = lexical.$getPreviousSelection(); | ||
let target = currentNode; | ||
if (!currentNode.isSimpleText()) { | ||
@@ -460,3 +401,2 @@ const textNode = lexical.$createTextNode(prevTextContent); | ||
} | ||
if (lexical.$isRangeSelection(prevSelection) && prevSelection.isCollapsed()) { | ||
@@ -469,12 +409,10 @@ const prevOffset = prevSelection.anchor.offset; | ||
const isSelected = anchor.key === key; | ||
let anchorOffset = anchor.offset; // Move offset to end if it's less than the remaining number, otherwise | ||
let anchorOffset = anchor.offset; | ||
// Move offset to end if it's less than the remaining number, otherwise | ||
// we'll have a negative splitStart. | ||
if (anchorOffset < remaining) { | ||
anchorOffset = currentNodeSize; | ||
} | ||
const splitStart = isSelected ? anchorOffset - remaining : 0; | ||
const splitEnd = isSelected ? anchorOffset : offset; | ||
if (isSelected && splitStart === 0) { | ||
@@ -491,3 +429,2 @@ const [excessNode] = currentNode.splitText(splitStart, splitEnd); | ||
} | ||
remaining = 0; | ||
@@ -497,2 +434,3 @@ } | ||
} | ||
/** | ||
@@ -502,3 +440,2 @@ * Gets the TextNode's style object and adds the styles to the CSS. | ||
*/ | ||
function $addNodeStyle(node) { | ||
@@ -509,3 +446,2 @@ const CSSText = node.getStyle(); | ||
} | ||
function $patchStyle(target, patch) { | ||
@@ -519,5 +455,5 @@ const prevStyles = getStyleObjectFromCSS('getStyle' in target ? target.getStyle() : target.style); | ||
} | ||
return styles; | ||
}, { ...prevStyles | ||
}, { | ||
...prevStyles | ||
} || {}); | ||
@@ -528,2 +464,3 @@ const newCSSText = getCSSFromStyleObject(newStyles); | ||
} | ||
/** | ||
@@ -536,8 +473,5 @@ * Applies the provided styles to the TextNodes in the provided Selection. | ||
*/ | ||
function $patchStyleText(selection, patch) { | ||
const selectedNodes = selection.getNodes(); | ||
const selectedNodesLength = selectedNodes.length; | ||
if (lexical.DEPRECATED_$isGridSelection(selection)) { | ||
@@ -547,6 +481,4 @@ const cellSelection = lexical.$createRangeSelection(); | ||
const cellSelectionFocus = cellSelection.focus; | ||
for (let i = 0; i < selectedNodesLength; i++) { | ||
const node = selectedNodes[i]; | ||
if (lexical.DEPRECATED_$isGridCellNode(node)) { | ||
@@ -558,11 +490,8 @@ cellSelectionAnchor.set(node.getKey(), 0, 'element'); | ||
} | ||
lexical.$setSelection(selection); | ||
return; | ||
} | ||
const lastIndex = selectedNodesLength - 1; | ||
let firstNode = selectedNodes[0]; | ||
let lastNode = selectedNodes[lastIndex]; | ||
if (selection.isCollapsed()) { | ||
@@ -572,3 +501,2 @@ $patchStyle(selection, patch); | ||
} | ||
const anchor = selection.anchor; | ||
@@ -585,8 +513,8 @@ const focus = selection.focus; | ||
const endType = isBefore ? focus.type : anchor.type; | ||
const endKey = isBefore ? focus.key : anchor.key; // This is the case where the user only selected the very end of the | ||
const endKey = isBefore ? focus.key : anchor.key; | ||
// This is the case where the user only selected the very end of the | ||
// first node so we don't want to include it in the formatting change. | ||
if (lexical.$isTextNode(firstNode) && startOffset === firstNodeTextLength) { | ||
const nextSibling = firstNode.getNextSibling(); | ||
if (lexical.$isTextNode(nextSibling)) { | ||
@@ -598,15 +526,16 @@ // we basically make the second node the firstNode, changing offsets accordingly | ||
} | ||
} // This is the case where we only selected a single node | ||
} | ||
// This is the case where we only selected a single node | ||
if (selectedNodes.length === 1) { | ||
if (lexical.$isTextNode(firstNode)) { | ||
startOffset = startType === 'element' ? 0 : anchorOffset > focusOffset ? focusOffset : anchorOffset; | ||
endOffset = endType === 'element' ? firstNodeTextLength : anchorOffset > focusOffset ? anchorOffset : focusOffset; // No actual text is selected, so do nothing. | ||
endOffset = endType === 'element' ? firstNodeTextLength : anchorOffset > focusOffset ? anchorOffset : focusOffset; | ||
// No actual text is selected, so do nothing. | ||
if (startOffset === endOffset) { | ||
return; | ||
} // The entire node is selected, so just format it | ||
} | ||
// The entire node is selected, so just format it | ||
if (startOffset === 0 && endOffset === firstNodeTextLength) { | ||
@@ -624,3 +553,2 @@ $patchStyle(firstNode, patch); | ||
} // multiple nodes selected. | ||
} else { | ||
@@ -632,33 +560,31 @@ if (lexical.$isTextNode(firstNode) && startOffset < firstNode.getTextContentSize()) { | ||
startOffset = 0; | ||
anchor.set(firstNode.getKey(), startOffset, 'text'); | ||
} | ||
$patchStyle(firstNode, patch); | ||
} | ||
if (lexical.$isTextNode(lastNode)) { | ||
const lastNodeText = lastNode.getTextContent(); | ||
const lastNodeTextLength = lastNodeText.length; // The last node might not actually be the end node | ||
const lastNodeTextLength = lastNodeText.length; | ||
// The last node might not actually be the end node | ||
// | ||
// If not, assume the last node is fully-selected unless the end offset is | ||
// zero. | ||
if (lastNode.__key !== endKey && endOffset !== 0) { | ||
endOffset = lastNodeTextLength; | ||
} // if the entire last node isn't selected, split it | ||
} | ||
// if the entire last node isn't selected, split it | ||
if (endOffset !== lastNodeTextLength) { | ||
[lastNode] = lastNode.splitText(endOffset); | ||
} | ||
if (endOffset !== 0) { | ||
$patchStyle(lastNode, patch); | ||
} | ||
} // style all the text nodes in between | ||
} | ||
// style all the text nodes in between | ||
for (let i = 1; i < lastIndex; i++) { | ||
const selectedNode = selectedNodes[i]; | ||
const selectedNodeKey = selectedNode.getKey(); | ||
if (lexical.$isTextNode(selectedNode) && selectedNodeKey !== firstNode.getKey() && selectedNodeKey !== lastNode.getKey() && !selectedNode.isToken()) { | ||
@@ -678,2 +604,3 @@ $patchStyle(selectedNode, patch); | ||
*/ | ||
/** | ||
@@ -684,3 +611,2 @@ * Converts all nodes in the selection that are of one block type to another. | ||
*/ | ||
function $setBlocksType(selection, createElement) { | ||
@@ -691,3 +617,2 @@ if (selection.anchor.key === 'root') { | ||
const firstChild = root.getFirstChild(); | ||
if (firstChild) { | ||
@@ -698,28 +623,14 @@ firstChild.replace(element, true); | ||
} | ||
return; | ||
} | ||
const nodes = selection.getNodes(); | ||
let maybeBlock = selection.anchor.getNode().getParentOrThrow(); | ||
if (nodes.indexOf(maybeBlock) === -1) { | ||
nodes.push(maybeBlock); | ||
const firstSelectedBlock = utils.$getAncestor(selection.anchor.getNode(), utils.INTERNAL_$isBlock); | ||
if (firstSelectedBlock && nodes.indexOf(firstSelectedBlock) === -1) { | ||
nodes.push(firstSelectedBlock); | ||
} | ||
if (maybeBlock.isInline()) { | ||
maybeBlock = maybeBlock.getParentOrThrow(); | ||
if (nodes.indexOf(maybeBlock) === -1) { | ||
nodes.push(maybeBlock); | ||
} | ||
} | ||
for (let i = 0; i < nodes.length; i++) { | ||
const node = nodes[i]; | ||
if (!isBlock(node)) { | ||
if (!utils.INTERNAL_$isBlock(node)) { | ||
continue; | ||
} | ||
const targetElement = createElement(); | ||
@@ -731,31 +642,17 @@ targetElement.setFormat(node.getFormatType()); | ||
} | ||
function isBlock(node) { | ||
if (!lexical.$isElementNode(node) || lexical.$isRootOrShadowRoot(node)) { | ||
return false; | ||
} | ||
const firstChild = node.getFirstChild(); | ||
const isLeafElement = firstChild === null || lexical.$isLineBreakNode(firstChild) || lexical.$isTextNode(firstChild) || firstChild.isInline(); | ||
return !node.isInline() && node.canBeEmpty() !== false && isLeafElement; | ||
} | ||
function isPointAttached(point) { | ||
return point.getNode().isAttached(); | ||
} | ||
function $removeParentEmptyElements(startingNode) { | ||
let node = startingNode; | ||
while (node !== null && !lexical.$isRootOrShadowRoot(node)) { | ||
const latest = node.getLatest(); | ||
const parentNode = node.getParent(); | ||
if (latest.getChildrenSize() === 0) { | ||
node.remove(true); | ||
} | ||
node = parentNode; | ||
} | ||
} | ||
/** | ||
@@ -768,4 +665,2 @@ * @deprecated | ||
*/ | ||
function $wrapNodes(selection, createElement, wrappingElement = null) { | ||
@@ -775,3 +670,2 @@ const nodes = selection.getNodes(); | ||
const anchor = selection.anchor; | ||
if (nodesLength === 0 || nodesLength === 1 && anchor.type === 'element' && anchor.getNode().getChildrenSize() === 0) { | ||
@@ -784,20 +678,16 @@ const target = anchor.type === 'text' ? anchor.getNode().getParentOrThrow() : anchor.getNode(); | ||
children.forEach(child => element.append(child)); | ||
if (wrappingElement) { | ||
element = wrappingElement.append(element); | ||
} | ||
target.replace(element); | ||
return; | ||
} | ||
let topLevelNode = null; | ||
let descendants = []; | ||
for (let i = 0; i < nodesLength; i++) { | ||
const node = nodes[i]; // Determine whether wrapping has to be broken down into multiple chunks. This can happen if the | ||
const node = nodes[i]; | ||
// Determine whether wrapping has to be broken down into multiple chunks. This can happen if the | ||
// user selected multiple Root-like nodes that have to be treated separately as if they are | ||
// their own branch. I.e. you don't want to wrap a whole table, but rather the contents of each | ||
// of each of the cell nodes. | ||
if (lexical.$isRootOrShadowRoot(node)) { | ||
@@ -814,5 +704,5 @@ $wrapNodesImpl(selection, descendants, descendants.length, createElement, wrappingElement); | ||
} | ||
$wrapNodesImpl(selection, descendants, descendants.length, createElement, wrappingElement); | ||
} | ||
/** | ||
@@ -827,3 +717,2 @@ * Wraps each node into a new ElementNode. | ||
*/ | ||
function $wrapNodesImpl(selection, nodes, nodesLength, createElement, wrappingElement = null) { | ||
@@ -833,21 +722,16 @@ if (nodes.length === 0) { | ||
} | ||
const firstNode = nodes[0]; | ||
const elementMapping = new Map(); | ||
const elements = []; // The below logic is to find the right target for us to | ||
const elements = []; | ||
// The below logic is to find the right target for us to | ||
// either insertAfter/insertBefore/append the corresponding | ||
// elements to. This is made more complicated due to nested | ||
// structures. | ||
let target = lexical.$isElementNode(firstNode) ? firstNode : firstNode.getParentOrThrow(); | ||
if (target.isInline()) { | ||
target = target.getParentOrThrow(); | ||
} | ||
let targetIsPrevSibling = false; | ||
while (target !== null) { | ||
const prevSibling = target.getPreviousSibling(); | ||
if (prevSibling !== null) { | ||
@@ -858,5 +742,3 @@ target = prevSibling; | ||
} | ||
target = target.getParentOrThrow(); | ||
if (lexical.$isRootOrShadowRoot(target)) { | ||
@@ -866,8 +748,7 @@ break; | ||
} | ||
const emptyElements = new Set(); | ||
const emptyElements = new Set(); // Find any top level empty elements | ||
// Find any top level empty elements | ||
for (let i = 0; i < nodesLength; i++) { | ||
const node = nodes[i]; | ||
if (lexical.$isElementNode(node) && node.getChildrenSize() === 0) { | ||
@@ -877,18 +758,15 @@ emptyElements.add(node.getKey()); | ||
} | ||
const movedNodes = new Set(); | ||
const movedNodes = new Set(); // Move out all leaf nodes into our elements array. | ||
// Move out all leaf nodes into our elements array. | ||
// If we find a top level empty element, also move make | ||
// an element for that. | ||
for (let i = 0; i < nodesLength; i++) { | ||
const node = nodes[i]; | ||
let parent = node.getParent(); | ||
if (parent !== null && parent.isInline()) { | ||
parent = parent.getParent(); | ||
} | ||
if (parent !== null && lexical.$isLeafNode(node) && !movedNodes.has(node.getKey())) { | ||
const parentKey = parent.getKey(); | ||
if (elementMapping.get(parentKey) === undefined) { | ||
@@ -899,9 +777,8 @@ const targetElement = createElement(); | ||
elements.push(targetElement); | ||
elementMapping.set(parentKey, targetElement); // Move node and its siblings to the new | ||
elementMapping.set(parentKey, targetElement); | ||
// Move node and its siblings to the new | ||
// element. | ||
parent.getChildren().forEach(child => { | ||
targetElement.append(child); | ||
movedNodes.add(child.getKey()); | ||
if (lexical.$isElementNode(child)) { | ||
@@ -922,3 +799,2 @@ // Skip nested leaf nodes if the parent has already been moved | ||
} | ||
if (wrappingElement !== null) { | ||
@@ -930,6 +806,6 @@ for (let i = 0; i < elements.length; i++) { | ||
} | ||
let lastElement = null; | ||
let lastElement = null; // If our target is Root-like, let's see if we can re-adjust | ||
// If our target is Root-like, let's see if we can re-adjust | ||
// so that the target is the first child instead. | ||
if (lexical.$isRootOrShadowRoot(target)) { | ||
@@ -947,7 +823,5 @@ if (targetIsPrevSibling) { | ||
const firstChild = target.getFirstChild(); | ||
if (lexical.$isElementNode(firstChild)) { | ||
target = firstChild; | ||
} | ||
if (firstChild === null) { | ||
@@ -986,5 +860,3 @@ if (wrappingElement) { | ||
} | ||
const prevSelection = lexical.$getPreviousSelection(); | ||
if (lexical.$isRangeSelection(prevSelection) && isPointAttached(prevSelection.anchor) && isPointAttached(prevSelection.focus)) { | ||
@@ -998,2 +870,3 @@ lexical.$setSelection(prevSelection.clone()); | ||
} | ||
/** | ||
@@ -1005,3 +878,2 @@ * Determines if the default character selection should be overridden. Used with DecoratorNodes | ||
*/ | ||
function $shouldOverrideDefaultCharacterSelection(selection, isBackward) { | ||
@@ -1011,2 +883,3 @@ const possibleNode = lexical.$getAdjacentNode(selection.focus, isBackward); | ||
} | ||
/** | ||
@@ -1019,6 +892,6 @@ * Moves the selection according to the arguments. | ||
*/ | ||
function $moveCaretSelection(selection, isHoldingShift, isBackward, granularity) { | ||
selection.modify(isHoldingShift ? 'extend' : 'move', isBackward, granularity); | ||
} | ||
/** | ||
@@ -1029,3 +902,2 @@ * Tests a parent element for right to left direction. | ||
*/ | ||
function $isParentElementRTL(selection) { | ||
@@ -1036,2 +908,3 @@ const anchorNode = selection.anchor.getNode(); | ||
} | ||
/** | ||
@@ -1043,3 +916,2 @@ * Moves selection by character according to arguments. | ||
*/ | ||
function $moveCharacter(selection, isHoldingShift, isBackward) { | ||
@@ -1049,2 +921,3 @@ const isRTL = $isParentElementRTL(selection); | ||
} | ||
/** | ||
@@ -1054,3 +927,2 @@ * Expands the current Selection to cover all of the content in the editor. | ||
*/ | ||
function $selectAll(selection) { | ||
@@ -1067,3 +939,2 @@ const anchor = selection.anchor; | ||
let lastOffset = 0; | ||
if (lexical.$isTextNode(firstNode)) { | ||
@@ -1074,3 +945,2 @@ firstType = 'text'; | ||
} | ||
if (lexical.$isTextNode(lastNode)) { | ||
@@ -1082,3 +952,2 @@ lastType = 'text'; | ||
} | ||
if (firstNode && lastNode) { | ||
@@ -1089,2 +958,3 @@ anchor.set(firstNode.getKey(), 0, firstType); | ||
} | ||
/** | ||
@@ -1097,13 +967,11 @@ * Returns the current value of a CSS property for Nodes, if set. If not set, it returns the defaultValue. | ||
*/ | ||
function $getNodeStyleValueForProperty(node, styleProperty, defaultValue) { | ||
const css = node.getStyle(); | ||
const styleObject = getStyleObjectFromCSS(css); | ||
if (styleObject !== null) { | ||
return styleObject[styleProperty] || defaultValue; | ||
} | ||
return defaultValue; | ||
} | ||
/** | ||
@@ -1117,4 +985,2 @@ * Returns the current value of a CSS property for TextNodes in the Selection, if set. If not set, it returns the defaultValue. | ||
*/ | ||
function $getSelectionStyleValueForProperty(selection, styleProperty, defaultValue = '') { | ||
@@ -1128,7 +994,5 @@ let styleValue = null; | ||
const endNode = isBackward ? focus.getNode() : anchor.getNode(); | ||
if (selection.style !== '') { | ||
if (selection.isCollapsed() && selection.style !== '') { | ||
const css = selection.style; | ||
const styleObject = getStyleObjectFromCSS(css); | ||
if (styleObject !== null && styleProperty in styleObject) { | ||
@@ -1138,15 +1002,13 @@ return styleObject[styleProperty]; | ||
} | ||
for (let i = 0; i < nodes.length; i++) { | ||
const node = nodes[i]; | ||
for (let i = 0; i < nodes.length; i++) { | ||
const node = nodes[i]; // if no actual characters in the end node are selected, we don't | ||
// if no actual characters in the end node are selected, we don't | ||
// include it in the selection for purposes of determining style | ||
// value | ||
if (i !== 0 && endOffset === 0 && node.is(endNode)) { | ||
continue; | ||
} | ||
if (lexical.$isTextNode(node)) { | ||
const nodeStyleValue = $getNodeStyleValueForProperty(node, styleProperty, defaultValue); | ||
if (styleValue === null) { | ||
@@ -1162,3 +1024,2 @@ styleValue = nodeStyleValue; | ||
} | ||
return styleValue === null ? defaultValue : styleValue; | ||
@@ -1165,0 +1026,0 @@ } |
@@ -7,25 +7,25 @@ /** | ||
*/ | ||
'use strict';var k=require("lexical");let u=new Map;function v(a){for(;null!=a;){if(a.nodeType===Node.TEXT_NODE)return a;a=a.firstChild}return null}function w(a){let b=a.parentNode;if(null==b)throw Error("Should never happen");return[b,Array.from(b.childNodes).indexOf(a)]}function y(a){let b={};a=a.split(";");for(let c of a)if(""!==c){let [e,d]=c.split(/:([^]+)/);b[e.trim()]=d.trim()}return b}function z(a){let b=u.get(a);void 0===b&&(b=y(a),u.set(a,b));return b} | ||
function A(a){let b="";for(let c in a)c&&(b+=`${c}: ${a[c]};`);return b}function B(a,b){var c=z("getStyle"in a?a.getStyle():a.style);b=Object.entries(b).reduce((e,[d,f])=>{null===f?delete e[d]:e[d]=f;return e},{...c});c=A(b);a.setStyle(c);u.set(c,b)} | ||
function C(a,b){var c=a.getNodes(),e=c.length;if(k.DEPRECATED_$isGridSelection(a)){var d=k.$createRangeSelection(),f=d.anchor,h=d.focus;for(var g=0;g<e;g++){var l=c[g];k.DEPRECATED_$isGridCellNode(l)&&(f.set(l.getKey(),0,"element"),h.set(l.getKey(),l.getChildrenSize(),"element"),C(k.$normalizeSelection__EXPERIMENTAL(d),b))}k.$setSelection(a)}else if(--e,d=c[0],f=c[e],a.isCollapsed())B(a,b);else{var m=a.anchor,p=a.focus;g=d.getTextContent().length;l=p.offset;var n=m.offset,r=m.isBefore(p);h=r?n:l; | ||
a=r?l:n;var q=r?m.type:p.type,t=r?p.type:m.type;m=r?p.key:m.key;k.$isTextNode(d)&&h===g&&(p=d.getNextSibling(),k.$isTextNode(p)&&(h=n=0,d=p));if(1===c.length)k.$isTextNode(d)&&(h="element"===q?0:n>l?l:n,a="element"===t?g:n>l?n:l,h!==a&&(0===h&&a===g?(B(d,b),d.select(h,a)):(c=d.splitText(h,a),c=0===h?c[0]:c[1],B(c,b),c.select(0,a-h))));else for(k.$isTextNode(d)&&h<d.getTextContentSize()&&(0!==h&&(d=d.splitText(h)[1]),B(d,b)),k.$isTextNode(f)&&(h=f.getTextContent().length,f.__key!==m&&0!==a&&(a=h), | ||
a!==h&&([f]=f.splitText(a)),0!==a&&B(f,b)),a=1;a<e;a++)h=c[a],g=h.getKey(),k.$isTextNode(h)&&g!==d.getKey()&&g!==f.getKey()&&!h.isToken()&&B(h,b)}}function D(a){for(;null!==a&&!k.$isRootOrShadowRoot(a);){let b=a.getLatest(),c=a.getParent();0===b.getChildrenSize()&&a.remove(!0);a=c}} | ||
function E(a,b,c,e,d=null){if(0!==b.length){var f=b[0],h=new Map,g=[];f=k.$isElementNode(f)?f:f.getParentOrThrow();f.isInline()&&(f=f.getParentOrThrow());for(var l=!1;null!==f;){var m=f.getPreviousSibling();if(null!==m){f=m;l=!0;break}f=f.getParentOrThrow();if(k.$isRootOrShadowRoot(f))break}m=new Set;for(var p=0;p<c;p++){var n=b[p];k.$isElementNode(n)&&0===n.getChildrenSize()&&m.add(n.getKey())}var r=new Set;for(p=0;p<c;p++){n=b[p];var q=n.getParent();null!==q&&q.isInline()&&(q=q.getParent());if(null!== | ||
q&&k.$isLeafNode(n)&&!r.has(n.getKey())){if(n=q.getKey(),void 0===h.get(n)){let t=e();t.setFormat(q.getFormatType());t.setIndent(q.getIndent());g.push(t);h.set(n,t);q.getChildren().forEach(x=>{t.append(x);r.add(x.getKey());k.$isElementNode(x)&&x.getChildrenKeys().forEach(H=>r.add(H))});D(q)}}else m.has(n.getKey())&&(q=e(),q.setFormat(n.getFormatType()),q.setIndent(n.getIndent()),g.push(q),n.remove(!0))}if(null!==d)for(b=0;b<g.length;b++)d.append(g[b]);b=null;if(k.$isRootOrShadowRoot(f))if(l)if(null!== | ||
d)f.insertAfter(d);else for(d=g.length-1;0<=d;d--)f.insertAfter(g[d]);else if(l=f.getFirstChild(),k.$isElementNode(l)&&(f=l),null===l)if(d)f.append(d);else for(d=0;d<g.length;d++)l=g[d],f.append(l),b=l;else if(null!==d)l.insertBefore(d);else for(f=0;f<g.length;f++)d=g[f],l.insertBefore(d),b=d;else if(d)f.insertAfter(d);else for(d=g.length-1;0<=d;d--)l=g[d],f.insertAfter(l),b=l;g=k.$getPreviousSelection();k.$isRangeSelection(g)&&g.anchor.getNode().isAttached()&&g.focus.getNode().isAttached()?k.$setSelection(g.clone()): | ||
null!==b?b.selectEnd():a.dirty=!0}}function F(a,b,c,e){a.modify(b?"extend":"move",c,e)}function G(a){a=a.anchor.getNode();return"rtl"===(k.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}exports.$addNodeStyle=function(a){a=a.getStyle();let b=y(a);u.set(a,b)}; | ||
'use strict';var k=require("lexical"),u=require("@lexical/utils");let v=new Map;function w(a){for(;null!=a;){if(a.nodeType===Node.TEXT_NODE)return a;a=a.firstChild}return null}function y(a){let b=a.parentNode;if(null==b)throw Error("Should never happen");return[b,Array.from(b.childNodes).indexOf(a)]}function z(a){let b={};a=a.split(";");for(let d of a)if(""!==d){let [f,c]=d.split(/:([^]+)/);b[f.trim()]=c.trim()}return b}function A(a){let b=v.get(a);void 0===b&&(b=z(a),v.set(a,b));return b} | ||
function B(a){let b="";for(let d in a)d&&(b+=`${d}: ${a[d]};`);return b}function C(a,b){var d=A("getStyle"in a?a.getStyle():a.style);b=Object.entries(b).reduce((f,[c,g])=>{null===g?delete f[c]:f[c]=g;return f},{...d});d=B(b);a.setStyle(d);v.set(d,b)} | ||
function D(a,b){var d=a.getNodes(),f=d.length;if(k.DEPRECATED_$isGridSelection(a)){var c=k.$createRangeSelection(),g=c.anchor,h=c.focus;for(var e=0;e<f;e++){var l=d[e];k.DEPRECATED_$isGridCellNode(l)&&(g.set(l.getKey(),0,"element"),h.set(l.getKey(),l.getChildrenSize(),"element"),D(k.$normalizeSelection__EXPERIMENTAL(c),b))}k.$setSelection(a)}else if(--f,c=d[0],g=d[f],a.isCollapsed())C(a,b);else{e=a.anchor;var m=a.focus;l=c.getTextContent().length;var p=m.offset,n=e.offset,r=e.isBefore(m);h=r?n:p; | ||
a=r?p:n;var q=r?e.type:m.type,t=r?m.type:e.type;m=r?m.key:e.key;k.$isTextNode(c)&&h===l&&(r=c.getNextSibling(),k.$isTextNode(r)&&(h=n=0,c=r));if(1===d.length)k.$isTextNode(c)&&(h="element"===q?0:n>p?p:n,a="element"===t?l:n>p?n:p,h!==a&&(0===h&&a===l?(C(c,b),c.select(h,a)):(d=c.splitText(h,a),d=0===h?d[0]:d[1],C(d,b),d.select(0,a-h))));else for(k.$isTextNode(c)&&h<c.getTextContentSize()&&(0!==h&&(c=c.splitText(h)[1],h=0,e.set(c.getKey(),h,"text")),C(c,b)),k.$isTextNode(g)&&(h=g.getTextContent().length, | ||
g.__key!==m&&0!==a&&(a=h),a!==h&&([g]=g.splitText(a)),0!==a&&C(g,b)),a=1;a<f;a++)h=d[a],e=h.getKey(),k.$isTextNode(h)&&e!==c.getKey()&&e!==g.getKey()&&!h.isToken()&&C(h,b)}}function E(a){for(;null!==a&&!k.$isRootOrShadowRoot(a);){let b=a.getLatest(),d=a.getParent();0===b.getChildrenSize()&&a.remove(!0);a=d}} | ||
function F(a,b,d,f,c=null){if(0!==b.length){var g=b[0],h=new Map,e=[];g=k.$isElementNode(g)?g:g.getParentOrThrow();g.isInline()&&(g=g.getParentOrThrow());for(var l=!1;null!==g;){var m=g.getPreviousSibling();if(null!==m){g=m;l=!0;break}g=g.getParentOrThrow();if(k.$isRootOrShadowRoot(g))break}m=new Set;for(var p=0;p<d;p++){var n=b[p];k.$isElementNode(n)&&0===n.getChildrenSize()&&m.add(n.getKey())}var r=new Set;for(p=0;p<d;p++){n=b[p];var q=n.getParent();null!==q&&q.isInline()&&(q=q.getParent());if(null!== | ||
q&&k.$isLeafNode(n)&&!r.has(n.getKey())){if(n=q.getKey(),void 0===h.get(n)){let t=f();t.setFormat(q.getFormatType());t.setIndent(q.getIndent());e.push(t);h.set(n,t);q.getChildren().forEach(x=>{t.append(x);r.add(x.getKey());k.$isElementNode(x)&&x.getChildrenKeys().forEach(I=>r.add(I))});E(q)}}else m.has(n.getKey())&&(q=f(),q.setFormat(n.getFormatType()),q.setIndent(n.getIndent()),e.push(q),n.remove(!0))}if(null!==c)for(b=0;b<e.length;b++)c.append(e[b]);b=null;if(k.$isRootOrShadowRoot(g))if(l)if(null!== | ||
c)g.insertAfter(c);else for(c=e.length-1;0<=c;c--)g.insertAfter(e[c]);else if(l=g.getFirstChild(),k.$isElementNode(l)&&(g=l),null===l)if(c)g.append(c);else for(c=0;c<e.length;c++)l=e[c],g.append(l),b=l;else if(null!==c)l.insertBefore(c);else for(g=0;g<e.length;g++)c=e[g],l.insertBefore(c),b=c;else if(c)g.insertAfter(c);else for(c=e.length-1;0<=c;c--)l=e[c],g.insertAfter(l),b=l;e=k.$getPreviousSelection();k.$isRangeSelection(e)&&e.anchor.getNode().isAttached()&&e.focus.getNode().isAttached()?k.$setSelection(e.clone()): | ||
null!==b?b.selectEnd():a.dirty=!0}}function G(a,b,d,f){a.modify(b?"extend":"move",d,f)}function H(a){a=a.anchor.getNode();return"rtl"===(k.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}exports.$addNodeStyle=function(a){a=a.getStyle();let b=z(a);v.set(a,b)}; | ||
exports.$cloneWithProperties=function(a){let b=a.constructor.clone(a);b.__parent=a.__parent;b.__next=a.__next;b.__prev=a.__prev;if(k.$isElementNode(a)&&k.$isElementNode(b))return b.__first=a.__first,b.__last=a.__last,b.__size=a.__size,b.__format=a.__format,b.__indent=a.__indent,b.__dir=a.__dir,b;k.$isTextNode(a)&&k.$isTextNode(b)&&(b.__format=a.__format,b.__style=a.__style,b.__mode=a.__mode,b.__detail=a.__detail);return b}; | ||
exports.$getSelectionStyleValueForProperty=function(a,b,c=""){let e=null,d=a.getNodes();var f=a.anchor,h=a.focus,g=a.isBackward();let l=g?h.offset:f.offset;f=g?h.getNode():f.getNode();if(""!==a.style&&(a=z(a.style),null!==a&&b in a))return a[b];for(a=0;a<d.length;a++){var m=d[a];if((0===a||0!==l||!m.is(f))&&k.$isTextNode(m))if(h=b,g=c,m=m.getStyle(),m=z(m),h=null!==m?m[h]||g:g,null===e)e=h;else if(e!==h){e="";break}}return null===e?c:e}; | ||
exports.$isAtNodeEnd=function(a){return"text"===a.type?a.offset===a.getNode().getTextContentSize():a.offset===a.getNode().getChildrenSize()};exports.$isParentElementRTL=G;exports.$moveCaretSelection=F;exports.$moveCharacter=function(a,b,c){let e=G(a);F(a,b,c?!e:e,"character")};exports.$patchStyleText=C; | ||
exports.$selectAll=function(a){let b=a.anchor;a=a.focus;var c=b.getNode().getTopLevelElementOrThrow().getParentOrThrow();let e=c.getFirstDescendant();c=c.getLastDescendant();let d="element",f="element",h=0;k.$isTextNode(e)?d="text":k.$isElementNode(e)||null===e||(e=e.getParentOrThrow());k.$isTextNode(c)?(f="text",h=c.getTextContentSize()):k.$isElementNode(c)||null===c||(c=c.getParentOrThrow());e&&c&&(b.set(e.getKey(),0,d),a.set(c.getKey(),h,f))}; | ||
exports.$setBlocksType=function(a,b){if("root"===a.anchor.key){b=b();var c=k.$getRoot();(a=c.getFirstChild())?a.replace(b,!0):c.append(b)}else for(c=a.getNodes(),a=a.anchor.getNode().getParentOrThrow(),-1===c.indexOf(a)&&c.push(a),a.isInline()&&(a=a.getParentOrThrow(),-1===c.indexOf(a)&&c.push(a)),a=0;a<c.length;a++){let f=c[a];var e=f;if(!k.$isElementNode(e)||k.$isRootOrShadowRoot(e))e=!1;else{var d=e.getFirstChild();d=null===d||k.$isLineBreakNode(d)||k.$isTextNode(d)||d.isInline();e=!e.isInline()&& | ||
!1!==e.canBeEmpty()&&d}e&&(e=b(),e.setFormat(f.getFormatType()),e.setIndent(f.getIndent()),f.replace(e,!0))}};exports.$shouldOverrideDefaultCharacterSelection=function(a,b){a=k.$getAdjacentNode(a.focus,b);return k.$isDecoratorNode(a)&&!a.isIsolated()||k.$isElementNode(a)&&!a.isInline()&&!a.canBeEmpty()}; | ||
exports.$sliceSelectedTextNodeContent=function(a,b){if(b.isSelected()&&!b.isSegmented()&&!b.isToken()&&(k.$isRangeSelection(a)||k.DEPRECATED_$isGridSelection(a))){var c=a.anchor.getNode(),e=a.focus.getNode(),d=b.is(c),f=b.is(e);if(d||f){d=a.isBackward();let [h,g]=a.getCharacterOffsets();a=c.is(e);f=b.is(d?e:c);e=b.is(d?c:e);c=0;let l=void 0;a?(c=h>g?g:h,l=h>g?h:g):f?(c=d?g:h,l=void 0):e&&(d=d?h:g,c=0,l=d);b.__text=b.__text.slice(c,l)}}return b}; | ||
exports.$wrapNodes=function(a,b,c=null){var e=a.getNodes();let d=e.length;var f=a.anchor;if(0===d||1===d&&"element"===f.type&&0===f.getNode().getChildrenSize()){a="text"===f.type?f.getNode().getParentOrThrow():f.getNode();e=a.getChildren();let g=b();g.setFormat(a.getFormatType());g.setIndent(a.getIndent());e.forEach(l=>g.append(l));c&&(g=c.append(g));a.replace(g)}else{f=null;var h=[];for(let g=0;g<d;g++){let l=e[g];k.$isRootOrShadowRoot(l)?(E(a,h,h.length,b,c),h=[],f=l):null===f||null!==f&&k.$hasAncestor(l, | ||
f)?h.push(l):(E(a,h,h.length,b,c),h=[l])}E(a,h,h.length,b,c)}}; | ||
exports.createDOMRange=function(a,b,c,e,d){let f=b.getKey(),h=e.getKey(),g=document.createRange(),l=a.getElementByKey(f);a=a.getElementByKey(h);k.$isTextNode(b)&&(l=v(l));k.$isTextNode(e)&&(a=v(a));if(void 0===b||void 0===e||null===l||null===a)return null;"BR"===l.nodeName&&([l,c]=w(l));"BR"===a.nodeName&&([a,d]=w(a));b=l.firstChild;l===a&&null!=b&&"BR"===b.nodeName&&0===c&&0===d&&(d=1);try{g.setStart(l,c),g.setEnd(a,d)}catch(m){return null}!g.collapsed||c===d&&f===h||(g.setStart(a,d),g.setEnd(l, | ||
c));return g};exports.createRectsFromDOMRange=function(a,b){var c=a.getRootElement();if(null===c)return[];a=c.getBoundingClientRect();c=getComputedStyle(c);c=parseFloat(c.paddingLeft)+parseFloat(c.paddingRight);b=Array.from(b.getClientRects());let e=b.length;b.sort((f,h)=>{let g=f.top-h.top;return 3>=Math.abs(g)?f.left-h.left:g});let d;for(let f=0;f<e;f++){let h=b[f],g=h.width+c===a.width;d&&d.top<=h.top&&d.top+d.height>h.top&&d.left+d.width>h.left||g?(b.splice(f--,1),e--):d=h}return b}; | ||
exports.getStyleObjectFromCSS=z; | ||
exports.trimTextContentFromAnchor=function(a,b,c){let e=b.getNode();if(k.$isElementNode(e)){var d=e.getDescendantByIndex(b.offset);null!==d&&(e=d)}for(;0<c&&null!==e;){var f=e.getPreviousSibling(),h=0;if(null===f){d=e.getParentOrThrow();for(var g=d.getPreviousSibling();null===g;){d=d.getParent();if(null===d){f=null;break}g=d.getPreviousSibling()}null!==d&&(h=d.isInline()?0:2,f=k.$isElementNode(g)?g.getLastDescendant():g)}g=e.getTextContent();""===g&&k.$isElementNode(e)&&!e.isInline()&&(g="\n\n"); | ||
d=g.length;if(!k.$isTextNode(e)||c>=d)g=e.getParent(),e.remove(),null==g||0!==g.getChildrenSize()||k.$isRootNode(g)||g.remove(),c-=d+h,e=f;else{let l=e.getKey();h=a.getEditorState().read(()=>{const p=k.$getNodeByKey(l);return k.$isTextNode(p)&&p.isSimpleText()?p.getTextContent():null});f=d-c;let m=g.slice(0,f);null!==h&&h!==g?(c=k.$getPreviousSelection(),d=e,e.isSimpleText()?e.setTextContent(h):(d=k.$createTextNode(h),e.replace(d)),k.$isRangeSelection(c)&&c.isCollapsed()&&(c=c.anchor.offset,d.select(c, | ||
c))):e.isSimpleText()?(h=b.key===l,g=b.offset,g<c&&(g=d),c=h?g-c:0,d=h?g:f,h&&0===c?([c]=e.splitText(c,d),c.remove()):([,c]=e.splitText(c,d),c.remove())):(c=k.$createTextNode(m),e.replace(c));c=0}}} | ||
exports.$getSelectionStyleValueForProperty=function(a,b,d=""){let f=null,c=a.getNodes();var g=a.anchor,h=a.focus,e=a.isBackward();let l=e?h.offset:g.offset;g=e?h.getNode():g.getNode();if(a.isCollapsed()&&""!==a.style&&(a=A(a.style),null!==a&&b in a))return a[b];for(a=0;a<c.length;a++){var m=c[a];if((0===a||0!==l||!m.is(g))&&k.$isTextNode(m))if(h=b,e=d,m=m.getStyle(),m=A(m),h=null!==m?m[h]||e:e,null===f)f=h;else if(f!==h){f="";break}}return null===f?d:f}; | ||
exports.$isAtNodeEnd=function(a){return"text"===a.type?a.offset===a.getNode().getTextContentSize():a.offset===a.getNode().getChildrenSize()};exports.$isParentElementRTL=H;exports.$moveCaretSelection=G;exports.$moveCharacter=function(a,b,d){let f=H(a);G(a,b,d?!f:f,"character")};exports.$patchStyleText=D; | ||
exports.$selectAll=function(a){let b=a.anchor;a=a.focus;var d=b.getNode().getTopLevelElementOrThrow().getParentOrThrow();let f=d.getFirstDescendant();d=d.getLastDescendant();let c="element",g="element",h=0;k.$isTextNode(f)?c="text":k.$isElementNode(f)||null===f||(f=f.getParentOrThrow());k.$isTextNode(d)?(g="text",h=d.getTextContentSize()):k.$isElementNode(d)||null===d||(d=d.getParentOrThrow());f&&d&&(b.set(f.getKey(),0,c),a.set(d.getKey(),h,g))}; | ||
exports.$setBlocksType=function(a,b){if("root"===a.anchor.key){b=b();var d=k.$getRoot();(a=d.getFirstChild())?a.replace(b,!0):d.append(b)}else for(d=a.getNodes(),(a=u.$getAncestor(a.anchor.getNode(),u.INTERNAL_$isBlock))&&-1===d.indexOf(a)&&d.push(a),a=0;a<d.length;a++){let f=d[a];if(!u.INTERNAL_$isBlock(f))continue;let c=b();c.setFormat(f.getFormatType());c.setIndent(f.getIndent());f.replace(c,!0)}}; | ||
exports.$shouldOverrideDefaultCharacterSelection=function(a,b){a=k.$getAdjacentNode(a.focus,b);return k.$isDecoratorNode(a)&&!a.isIsolated()||k.$isElementNode(a)&&!a.isInline()&&!a.canBeEmpty()}; | ||
exports.$sliceSelectedTextNodeContent=function(a,b){if(b.isSelected()&&!b.isSegmented()&&!b.isToken()&&(k.$isRangeSelection(a)||k.DEPRECATED_$isGridSelection(a))){var d=a.anchor.getNode(),f=a.focus.getNode(),c=b.is(d),g=b.is(f);if(c||g){c=a.isBackward();let [h,e]=a.getCharacterOffsets();a=d.is(f);g=b.is(c?f:d);f=b.is(c?d:f);d=0;let l=void 0;a?(d=h>e?e:h,l=h>e?h:e):g?(d=c?e:h,l=void 0):f&&(c=c?h:e,d=0,l=c);b.__text=b.__text.slice(d,l)}}return b}; | ||
exports.$wrapNodes=function(a,b,d=null){var f=a.getNodes();let c=f.length;var g=a.anchor;if(0===c||1===c&&"element"===g.type&&0===g.getNode().getChildrenSize()){a="text"===g.type?g.getNode().getParentOrThrow():g.getNode();f=a.getChildren();let e=b();e.setFormat(a.getFormatType());e.setIndent(a.getIndent());f.forEach(l=>e.append(l));d&&(e=d.append(e));a.replace(e)}else{g=null;var h=[];for(let e=0;e<c;e++){let l=f[e];k.$isRootOrShadowRoot(l)?(F(a,h,h.length,b,d),h=[],g=l):null===g||null!==g&&k.$hasAncestor(l, | ||
g)?h.push(l):(F(a,h,h.length,b,d),h=[l])}F(a,h,h.length,b,d)}}; | ||
exports.createDOMRange=function(a,b,d,f,c){let g=b.getKey(),h=f.getKey(),e=document.createRange(),l=a.getElementByKey(g);a=a.getElementByKey(h);k.$isTextNode(b)&&(l=w(l));k.$isTextNode(f)&&(a=w(a));if(void 0===b||void 0===f||null===l||null===a)return null;"BR"===l.nodeName&&([l,d]=y(l));"BR"===a.nodeName&&([a,c]=y(a));b=l.firstChild;l===a&&null!=b&&"BR"===b.nodeName&&0===d&&0===c&&(c=1);try{e.setStart(l,d),e.setEnd(a,c)}catch(m){return null}!e.collapsed||d===c&&g===h||(e.setStart(a,c),e.setEnd(l, | ||
d));return e};exports.createRectsFromDOMRange=function(a,b){var d=a.getRootElement();if(null===d)return[];a=d.getBoundingClientRect();d=getComputedStyle(d);d=parseFloat(d.paddingLeft)+parseFloat(d.paddingRight);b=Array.from(b.getClientRects());let f=b.length;b.sort((g,h)=>{let e=g.top-h.top;return 3>=Math.abs(e)?g.left-h.left:e});let c;for(let g=0;g<f;g++){let h=b[g],e=h.width+d===a.width;c&&c.top<=h.top&&c.top+c.height>h.top&&c.left+c.width>h.left||e?(b.splice(g--,1),f--):c=h}return b}; | ||
exports.getStyleObjectFromCSS=A; | ||
exports.trimTextContentFromAnchor=function(a,b,d){let f=b.getNode();if(k.$isElementNode(f)){var c=f.getDescendantByIndex(b.offset);null!==c&&(f=c)}for(;0<d&&null!==f;){k.$isElementNode(f)&&(c=f.getLastDescendant(),null!==c&&(f=c));var g=f.getPreviousSibling(),h=0;if(null===g){c=f.getParentOrThrow();for(var e=c.getPreviousSibling();null===e;){c=c.getParent();if(null===c){g=null;break}e=c.getPreviousSibling()}null!==c&&(h=c.isInline()?0:2,g=e)}e=f.getTextContent();""===e&&k.$isElementNode(f)&&!f.isInline()&& | ||
(e="\n\n");c=e.length;if(!k.$isTextNode(f)||d>=c)e=f.getParent(),f.remove(),null==e||0!==e.getChildrenSize()||k.$isRootNode(e)||e.remove(),d-=c+h,f=g;else{let l=f.getKey();h=a.getEditorState().read(()=>{const p=k.$getNodeByKey(l);return k.$isTextNode(p)&&p.isSimpleText()?p.getTextContent():null});g=c-d;let m=e.slice(0,g);null!==h&&h!==e?(d=k.$getPreviousSelection(),c=f,f.isSimpleText()?f.setTextContent(h):(c=k.$createTextNode(h),f.replace(c)),k.$isRangeSelection(d)&&d.isCollapsed()&&(d=d.anchor.offset, | ||
c.select(d,d))):f.isSimpleText()?(h=b.key===l,e=b.offset,e<d&&(e=c),d=h?e-d:0,c=h?e:g,h&&0===d?([d]=f.splitText(d,c),d.remove()):([,d]=f.splitText(d,c),d.remove())):(d=k.$createTextNode(m),f.replace(d));d=0}}} |
@@ -12,6 +12,6 @@ { | ||
"license": "MIT", | ||
"version": "0.12.2", | ||
"version": "0.12.3", | ||
"main": "LexicalSelection.js", | ||
"peerDependencies": { | ||
"lexical": "0.12.2" | ||
"lexical": "0.12.3" | ||
}, | ||
@@ -18,0 +18,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1166
62624