@lexical/offset
Advanced tools
Comparing version 0.7.8 to 0.7.9
@@ -18,10 +18,7 @@ /** | ||
} | ||
createSelectionFromOffsets(originalStart, originalEnd, diffOffsetView) { | ||
const firstNode = this._firstNode; | ||
if (firstNode === null) { | ||
return null; | ||
} | ||
let start = originalStart; | ||
@@ -31,3 +28,2 @@ let end = originalEnd; | ||
let endOffsetNode = $searchForNodeWithOffset(firstNode, end, this._blockOffsetSize); | ||
if (diffOffsetView !== undefined) { | ||
@@ -39,7 +35,5 @@ start = $getAdjustedOffsetFromDiff(start, startOffsetNode, diffOffsetView, this, this._blockOffsetSize); | ||
} | ||
if (startOffsetNode === null || endOffsetNode === null) { | ||
return null; | ||
} | ||
let startKey = startOffsetNode.key; | ||
@@ -49,7 +43,5 @@ let endKey = endOffsetNode.key; | ||
const endNode = lexical.$getNodeByKey(endKey); | ||
if (startNode === null || endNode === null) { | ||
return null; | ||
} | ||
let startOffset = 0; | ||
@@ -59,11 +51,9 @@ let endOffset = 0; | ||
let endType = 'element'; | ||
if (startOffsetNode.type === 'text') { | ||
startOffset = start - startOffsetNode.start; | ||
startType = 'text'; // If we are at the edge of a text node and we | ||
startType = 'text'; | ||
// If we are at the edge of a text node and we | ||
// don't have a collapsed selection, then let's | ||
// try and correct the offset node. | ||
const sibling = startNode.getNextSibling(); | ||
if (start !== end && startOffset === startNode.getTextContentSize() && lexical.$isTextNode(sibling)) { | ||
@@ -77,3 +67,2 @@ startOffset = 0; | ||
} | ||
if (endOffsetNode.type === 'text') { | ||
@@ -86,9 +75,6 @@ endOffset = end - endOffsetNode.start; | ||
} | ||
const selection = lexical.$createRangeSelection(); | ||
if (selection === null) { | ||
return null; | ||
} | ||
selection.anchor.set(startKey, startOffset, startType); | ||
@@ -98,3 +84,2 @@ selection.focus.set(endKey, endOffset, endType); | ||
} | ||
getOffsetsFromSelection(selection) { | ||
@@ -108,6 +93,4 @@ const anchor = selection.anchor; | ||
let end = -1; | ||
if (anchor.type === 'text') { | ||
const offsetNode = offsetMap.get(anchor.key); | ||
if (offsetNode !== undefined) { | ||
@@ -118,6 +101,4 @@ start = offsetNode.start + anchorOffset; | ||
const node = anchor.getNode().getDescendantByIndex(anchorOffset); | ||
if (node !== null) { | ||
const offsetNode = offsetMap.get(node.getKey()); | ||
if (offsetNode !== undefined) { | ||
@@ -129,6 +110,4 @@ const isAtEnd = node.getIndexWithinParent() !== anchorOffset; | ||
} | ||
if (focus.type === 'text') { | ||
const offsetNode = offsetMap.get(focus.key); | ||
if (offsetNode !== undefined) { | ||
@@ -139,6 +118,4 @@ end = offsetNode.start + focus.offset; | ||
const node = focus.getNode().getDescendantByIndex(focusOffset); | ||
if (node !== null) { | ||
const offsetNode = offsetMap.get(node.getKey()); | ||
if (offsetNode !== undefined) { | ||
@@ -150,8 +127,5 @@ const isAtEnd = node.getIndexWithinParent() !== focusOffset; | ||
} | ||
return [start, end]; | ||
} | ||
} | ||
function $getAdjustedOffsetFromDiff(offset, offsetNode, prevOffsetView, offsetView, blockOffsetSize) { | ||
@@ -163,3 +137,2 @@ const prevOffsetMap = prevOffsetView._offsetMap; | ||
let currentNode = offsetNode; | ||
while (currentNode !== null) { | ||
@@ -170,3 +143,2 @@ const key = currentNode.key; | ||
visited.add(key); | ||
if (prevNode === undefined) { | ||
@@ -176,3 +148,2 @@ adjustedOffset += diff; | ||
const prevDiff = prevNode.end - prevNode.start; | ||
if (prevDiff !== diff) { | ||
@@ -182,5 +153,3 @@ adjustedOffset += diff - prevDiff; | ||
} | ||
const sibling = currentNode.prev; | ||
if (sibling !== null) { | ||
@@ -190,8 +159,5 @@ currentNode = sibling; | ||
} | ||
let parent = currentNode.parent; | ||
while (parent !== null) { | ||
let parentSibling = parent.prev; | ||
if (parentSibling !== null) { | ||
@@ -202,3 +168,2 @@ const parentSiblingKey = parentSibling.key; | ||
visited.add(parentSiblingKey); | ||
if (prevParentSibling === undefined) { | ||
@@ -208,3 +173,2 @@ adjustedOffset += parentDiff; | ||
const prevParentDiff = prevParentSibling.end - prevParentSibling.start; | ||
if (prevParentDiff !== parentDiff) { | ||
@@ -214,21 +178,16 @@ adjustedOffset += parentDiff - prevParentDiff; | ||
} | ||
parentSibling = parentSibling.prev; | ||
} | ||
parent = parent.parent; | ||
} | ||
break; | ||
} | ||
break; | ||
} // Now traverse through the old offsets nodes and find any nodes we missed | ||
// Now traverse through the old offsets nodes and find any nodes we missed | ||
// above, because they were not in the latest offset node view (they have been | ||
// deleted). | ||
const prevFirstNode = prevOffsetView._firstNode; | ||
if (prevFirstNode !== null) { | ||
currentNode = $searchForNodeWithOffset(prevFirstNode, offset, blockOffsetSize); | ||
let alreadyVisitedParentOfCurrentNode = false; | ||
while (currentNode !== null) { | ||
@@ -239,14 +198,10 @@ if (!visited.has(currentNode.key)) { | ||
} | ||
currentNode = currentNode.parent; | ||
} | ||
if (!alreadyVisitedParentOfCurrentNode) { | ||
while (currentNode !== null) { | ||
const key = currentNode.key; | ||
if (!visited.has(key)) { | ||
const node = offsetMap.get(key); | ||
const prevDiff = currentNode.end - currentNode.start; | ||
if (node === undefined) { | ||
@@ -256,3 +211,2 @@ adjustedOffset -= prevDiff; | ||
const diff = node.end - node.start; | ||
if (prevDiff !== diff) { | ||
@@ -263,3 +217,2 @@ adjustedOffset += diff - prevDiff; | ||
} | ||
currentNode = currentNode.prev; | ||
@@ -269,15 +222,10 @@ } | ||
} | ||
return adjustedOffset; | ||
} | ||
function $searchForNodeWithOffset(firstNode, offset, blockOffsetSize) { | ||
let currentNode = firstNode; | ||
while (currentNode !== null) { | ||
const end = currentNode.end + (currentNode.type !== 'element' || blockOffsetSize === 0 ? 1 : 0); | ||
if (offset < end) { | ||
const child = currentNode.child; | ||
if (child !== null) { | ||
@@ -287,18 +235,12 @@ currentNode = child; | ||
} | ||
return currentNode; | ||
} | ||
const sibling = currentNode.next; | ||
if (sibling === null) { | ||
break; | ||
} | ||
currentNode = sibling; | ||
} | ||
return null; | ||
} | ||
function $createInternalOffsetNode(child, type, start, end, key, parent) { | ||
@@ -316,6 +258,4 @@ return { | ||
} | ||
function $createOffsetNode(state, key, parent, nodeMap, offsetMap, blockOffsetSize) { | ||
const node = nodeMap.get(key); | ||
if (node === undefined) { | ||
@@ -326,11 +266,10 @@ { | ||
} | ||
const start = state.offset; | ||
if (lexical.$isElementNode(node)) { | ||
const childKeys = createChildrenArray(node, nodeMap); | ||
const blockIsEmpty = childKeys.length === 0; | ||
const child = blockIsEmpty ? null : $createOffsetChild(state, childKeys, null, nodeMap, offsetMap, blockOffsetSize); // If the prev node was not a block or the block is empty, we should | ||
const child = blockIsEmpty ? null : $createOffsetChild(state, childKeys, null, nodeMap, offsetMap, blockOffsetSize); | ||
// If the prev node was not a block or the block is empty, we should | ||
// account for the user being able to selection the block (due to the \n). | ||
if (!state.prevIsBlock || blockIsEmpty) { | ||
@@ -340,9 +279,6 @@ state.prevIsBlock = true; | ||
} | ||
const offsetNode = $createInternalOffsetNode(child, 'element', start, start, key, parent); | ||
if (child !== null) { | ||
child.parent = offsetNode; | ||
} | ||
const end = state.offset; | ||
@@ -353,3 +289,2 @@ offsetNode.end = end; | ||
} | ||
state.prevIsBlock = false; | ||
@@ -363,3 +298,2 @@ const isText = lexical.$isTextNode(node); | ||
} | ||
function $createOffsetChild(state, children, parent, nodeMap, offsetMap, blockOffsetSize) { | ||
@@ -369,7 +303,5 @@ let firstNode = null; | ||
const childrenLength = children.length; | ||
for (let i = 0; i < childrenLength; i++) { | ||
const childKey = children[i]; | ||
const offsetNode = $createOffsetNode(state, childKey, parent, nodeMap, offsetMap, blockOffsetSize); | ||
if (currentNode === null) { | ||
@@ -381,16 +313,11 @@ firstNode = offsetNode; | ||
} | ||
currentNode = offsetNode; | ||
} | ||
return firstNode; | ||
} | ||
function createChildrenArray(element, nodeMap) { | ||
const children = []; | ||
let nodeKey = element.__first; | ||
while (nodeKey !== null) { | ||
const node = nodeMap === null ? lexical.$getNodeByKey(nodeKey) : nodeMap.get(nodeKey); | ||
if (node === null || node === undefined) { | ||
@@ -401,7 +328,5 @@ { | ||
} | ||
children.push(nodeKey); | ||
nodeKey = node.__next; | ||
} | ||
return children; | ||
@@ -408,0 +333,0 @@ } |
@@ -11,6 +11,6 @@ { | ||
"license": "MIT", | ||
"version": "0.7.8", | ||
"version": "0.7.9", | ||
"main": "LexicalOffset.js", | ||
"peerDependencies": { | ||
"lexical": "0.7.8" | ||
"lexical": "0.7.9" | ||
}, | ||
@@ -17,0 +17,0 @@ "repository": { |
392
20243