Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit fccddac

Browse files
committed
perf
1 parent ce75aa8 commit fccddac

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

packages/core/src/VNodes/ContainerNode.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ export class ContainerNode extends AbstractNode {
1717
children(predicate?: Predicate): VNode[];
1818
children(predicate?: Predicate): VNode[] {
1919
return this.childVNodes.filter(child => {
20-
return child.tangible && child.test(predicate);
20+
return child.tangible && (!predicate || child.test(predicate));
2121
});
2222
}
2323
/**
2424
* See {@link AbstractNode.hasChildren}.
2525
*/
2626
hasChildren(): boolean {
27-
return this.children().length > 0;
27+
return !!this.childVNodes.find(child => child.tangible);
2828
}
2929
/**
3030
* See {@link AbstractNode.nthChild}.
@@ -39,7 +39,7 @@ export class ContainerNode extends AbstractNode {
3939
firstChild(predicate?: Predicate): VNode;
4040
firstChild(predicate?: Predicate): VNode {
4141
let child = this.childVNodes[0];
42-
while (child && !(child.tangible && child.test(predicate))) {
42+
while (child && !(child.tangible && (!predicate || child.test(predicate)))) {
4343
child = child.nextSibling();
4444
}
4545
return child;
@@ -51,7 +51,7 @@ export class ContainerNode extends AbstractNode {
5151
lastChild(predicate?: Predicate): VNode;
5252
lastChild(predicate?: Predicate): VNode {
5353
let child = this.childVNodes[this.childVNodes.length - 1];
54-
while (child && !(child.tangible && child.test(predicate))) {
54+
while (child && !(child.tangible && (!predicate || child.test(predicate)))) {
5555
child = child.previousSibling();
5656
}
5757
return child;
@@ -63,7 +63,7 @@ export class ContainerNode extends AbstractNode {
6363
firstLeaf(predicate?: Predicate): VNode;
6464
firstLeaf(predicate?: Predicate): VNode {
6565
const isValidLeaf = (node: VNode): boolean => {
66-
return isLeaf(node) && node.test(predicate);
66+
return isLeaf(node) && (!predicate || node.test(predicate));
6767
};
6868
if (isValidLeaf(this)) {
6969
return this;
@@ -78,7 +78,7 @@ export class ContainerNode extends AbstractNode {
7878
lastLeaf(predicate?: Predicate): VNode;
7979
lastLeaf(predicate?: Predicate): VNode {
8080
const isValidLeaf = (node: VNode): boolean => {
81-
return isLeaf(node) && node.test(predicate);
81+
return isLeaf(node) && (!predicate || node.test(predicate));
8282
};
8383
if (isValidLeaf(this)) {
8484
return this;
@@ -93,7 +93,7 @@ export class ContainerNode extends AbstractNode {
9393
firstDescendant(predicate?: Predicate): VNode;
9494
firstDescendant(predicate?: Predicate): VNode {
9595
let firstDescendant = this.firstChild();
96-
while (firstDescendant && !firstDescendant.test(predicate)) {
96+
while (firstDescendant && predicate && firstDescendant.test(predicate)) {
9797
firstDescendant = this._descendantAfter(firstDescendant);
9898
}
9999
return firstDescendant;
@@ -108,7 +108,7 @@ export class ContainerNode extends AbstractNode {
108108
while (lastDescendant && lastDescendant.hasChildren()) {
109109
lastDescendant = lastDescendant.lastChild();
110110
}
111-
while (lastDescendant && !lastDescendant.test(predicate)) {
111+
while (lastDescendant && predicate && !lastDescendant.test(predicate)) {
112112
lastDescendant = this._descendantBefore(lastDescendant);
113113
}
114114
return lastDescendant;
@@ -123,7 +123,7 @@ export class ContainerNode extends AbstractNode {
123123
const stack = [...this.childVNodes];
124124
while (stack.length) {
125125
const node = stack.shift();
126-
if (node.tangible && node.test(predicate)) {
126+
if (node.tangible && (!predicate || node.test(predicate))) {
127127
descendants.push(node);
128128
}
129129
if (node instanceof ContainerNode) {

packages/plugin-dom-layout/src/DomLayout.ts

+10-13
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,16 @@ export class DomLayout<T extends DomLayoutConfig = DomLayoutConfig> extends JWPl
156156
const pathChanges = new Map(this.editor.memory.getChangedVersionables());
157157
if (pathChanges.size) {
158158
const nodes = [];
159-
// for (const [root] of pathChanges) {
160-
// if (
161-
// !(root instanceof AbstractNode) ||
162-
// root === this.editor.selection.anchor ||
163-
// root === this.editor.selection.focus
164-
// ) {
165-
// // Filter not VNode changes and selection change.
166-
// pathChanges.delete(root);
167-
// }
168-
// }
159+
for (const [root] of pathChanges) {
160+
if (
161+
!(root instanceof AbstractNode) ||
162+
root === this.editor.selection.anchor ||
163+
root === this.editor.selection.focus
164+
) {
165+
// Filter not VNode changes and selection change.
166+
pathChanges.delete(root);
167+
}
168+
}
169169
let removedNode = false;
170170
for (const [root] of pathChanges) {
171171
if (root instanceof AbstractNode) {
@@ -195,9 +195,6 @@ export class DomLayout<T extends DomLayoutConfig = DomLayoutConfig> extends JWPl
195195
}
196196
}
197197
}
198-
199-
console.log(pathChanges);
200-
console.log(nodes.map(n => n.name));
201198
await domLayoutEngine.redraw(nodes);
202199
}
203200
}

0 commit comments

Comments
 (0)