Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some wonky internal logic + use __key internally #1646

Merged
merged 1 commit into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix some wonky internal logic + use __key internally
  • Loading branch information
trueadm committed Apr 8, 2022
commit e88661664937cb47bf2b5e95bedf12477a828f71
6 changes: 3 additions & 3 deletions packages/lexical/src/LexicalNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ export class LexicalNode {
if (object == null) {
return false;
}
return this.getKey() === object.getKey();
return this.__key === object.__key;
}

isBefore(targetNode: LexicalNode): boolean {
Expand Down Expand Up @@ -669,7 +669,7 @@ export class LexicalNode {
internalMarkSiblingsAsDirty(writableNodeToInsert);

if ($isRangeSelection(selection)) {
const oldParentKey = oldParent.getKey();
const oldParentKey = oldParent.__key;
const oldIndex = nodeToInsert.getIndexWithinParent();
const anchor = selection.anchor;
const focus = selection.focus;
Expand Down Expand Up @@ -700,7 +700,7 @@ export class LexicalNode {
writableParent,
index + 1,
);
const writableParentKey = writableParent.getKey();
const writableParentKey = writableParent.__key;
if (elementAnchorSelectionOnNode) {
selection.anchor.set(writableParentKey, index + 2, 'element');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/lexical/src/LexicalParsing.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export function internalCreateNodeFromParse(
key,
state,
);
const newChildKey = child.getKey();
const newChildKey = child.__key;
node.__children.push(newChildKey);
}
}
Expand Down
34 changes: 17 additions & 17 deletions packages/lexical/src/LexicalSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function $createPoint(
}

function selectPointOnNode(point: PointType, node: LexicalNode): void {
const key = node.getKey();
const key = node.__key;
let offset = point.offset;
let type = 'element';
if ($isTextNode(node)) {
Expand Down Expand Up @@ -201,9 +201,9 @@ function $transferStartingElementPointToTextPoint(
}
// Transfer the element point to a text point.
if (start.is(end)) {
end.set(textNode.getKey(), 0, 'text');
end.set(textNode.__key, 0, 'text');
}
start.set(textNode.getKey(), 0, 'text');
start.set(textNode.__key, 0, 'text');
}

function $setPointValues(
Expand Down Expand Up @@ -807,12 +807,12 @@ export class RangeSelection implements BaseSelection {
lastNode = textNode;
}
lastNode = lastNode.spliceText(0, endOffset, '');
markedNodeKeysForKeep.add(lastNode.getKey());
markedNodeKeysForKeep.add(lastNode.__key);
} else {
lastNode.remove();
}
} else {
markedNodeKeysForKeep.add(lastNode.getKey());
markedNodeKeysForKeep.add(lastNode.__key);
}

// Either move the remaining nodes of the last parent to after
Expand Down Expand Up @@ -866,7 +866,7 @@ export class RangeSelection implements BaseSelection {
childrenLength === 0 ||
children[childrenLength - 1].is(lastRemovedParent)
) {
markedNodeKeysForKeep.delete(parent.getKey());
markedNodeKeysForKeep.delete(parent.__key);
lastRemovedParent = parent;
}
parent = parent.getParent();
Expand Down Expand Up @@ -899,7 +899,7 @@ export class RangeSelection implements BaseSelection {
// Remove all selected nodes that haven't already been removed.
for (let i = 1; i < selectedNodesLength; i++) {
const selectedNode = selectedNodes[i];
const key = selectedNode.getKey();
const key = selectedNode.__key;
if (!markedNodeKeysForKeep.has(key)) {
selectedNode.remove();
}
Expand Down Expand Up @@ -1018,11 +1018,11 @@ export class RangeSelection implements BaseSelection {
// deal with all the nodes in between
for (let i = 1; i < lastIndex; i++) {
const selectedNode = selectedNodes[i];
const selectedNodeKey = selectedNode.getKey();
const selectedNodeKey = selectedNode.__key;
if (
$isTextNode(selectedNode) &&
selectedNodeKey !== firstNode.getKey() &&
selectedNodeKey !== lastNode.getKey() &&
selectedNodeKey !== firstNode.__key &&
selectedNodeKey !== lastNode.__key &&
!selectedNode.isToken()
) {
const selectedNextFormat = selectedNode.getFormatFlags(
Expand Down Expand Up @@ -1463,11 +1463,11 @@ export class RangeSelection implements BaseSelection {
let elementKey;

if ($isElementNode(sibling)) {
elementKey = sibling.getKey();
elementKey = sibling.__key;
offset = isBackward ? sibling.getChildrenSize() : 0;
} else {
offset = possibleNode.getIndexWithinParent();
elementKey = parent.getKey();
elementKey = parent.__key;
if (!isBackward) {
offset++;
}
Expand Down Expand Up @@ -2070,7 +2070,7 @@ export function $updateElementSelectionOnCreateDeleteNode(
if (!parentNode.is(anchorNode) && !parentNode.is(focusNode)) {
return;
}
const parentKey = parentNode.getKey();
const parentKey = parentNode.__key;
// Single node. We shift selection but never redimension it
if (selection.isCollapsed()) {
const selectionOffset = anchor.offset;
Expand Down Expand Up @@ -2130,8 +2130,8 @@ function $updateSelectionResolveTextNodes(selection: RangeSelection): void {
if (anchorOffsetAtEnd) {
newOffset = child.getTextContentSize();
}
anchor.set(child.getKey(), newOffset, 'text');
focus.set(child.getKey(), newOffset, 'text');
anchor.set(child.__key, newOffset, 'text');
focus.set(child.__key, newOffset, 'text');
}
return;
}
Expand All @@ -2146,7 +2146,7 @@ function $updateSelectionResolveTextNodes(selection: RangeSelection): void {
if (anchorOffsetAtEnd) {
newOffset = child.getTextContentSize();
}
anchor.set(child.getKey(), newOffset, 'text');
anchor.set(child.__key, newOffset, 'text');
}
}
if ($isElementNode(focusNode)) {
Expand All @@ -2160,7 +2160,7 @@ function $updateSelectionResolveTextNodes(selection: RangeSelection): void {
if (focusOffsetAtEnd) {
newOffset = child.getTextContentSize();
}
focus.set(child.getKey(), newOffset, 'text');
focus.set(child.__key, newOffset, 'text');
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/lexical/src/nodes/LexicalElementNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ export class ElementNode extends LexicalNode {
return textContent;
}
getDirection(): 'ltr' | 'rtl' | null {
return this.__dir;
const self = this.getLatest();
return self.__dir;
}
hasFormat(type: ElementFormatType): boolean {
const formatFlag = ELEMENT_TYPE_TO_FORMAT[type];
Expand Down
7 changes: 5 additions & 2 deletions packages/lexical/src/nodes/LexicalTextNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,14 +443,14 @@ export class TextNode extends LexicalNode {
setFormat(format: number): this {
errorOnReadOnly();
const self = this.getWritable();
this.getWritable().__format = format;
self.__format = format;
return self;
}

setStyle(style: string): this {
errorOnReadOnly();
const self = this.getWritable();
this.getWritable().__style = style;
self.__style = style;
return self;
}

Expand All @@ -460,18 +460,21 @@ export class TextNode extends LexicalNode {
}

toggleDirectionless(): this {
errorOnReadOnly();
const self = this.getWritable();
self.__detail ^= IS_DIRECTIONLESS;
return self;
}

toggleUnmergeable(): this {
errorOnReadOnly();
const self = this.getWritable();
self.__detail ^= IS_UNMERGEABLE;
return self;
}

setMode(type: TextModeType): this {
errorOnReadOnly();
const mode = TEXT_MODE_TO_TYPE[type];
const self = this.getWritable();
self.__mode = mode;
Expand Down