Skip to content

Commit

Permalink
chore: rename variables
Browse files Browse the repository at this point in the history
  • Loading branch information
prevwong committed Jun 20, 2020
1 parent 164972c commit c8f3226
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions packages/core/src/editor/NodeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,50 +54,53 @@ export function NodeHelpers(state: EditorState, id: NodeId) {
get() {
return node;
},
ancestors(deep = false) {
ancestors(deep = false): NodeId[] {
function appendParentNode(
id: NodeId,
result: NodeId[] = [],
ancestors: NodeId[] = [],
depth: number = 0
) {
const node = state.nodes[id];
if (!node) {
return result;
return ancestors;
}

result.push(id);
ancestors.push(id);

if (!node.data.parent) {
return result;
return ancestors;
}

if (deep || (!deep && depth === 0)) {
result = appendParentNode(node.data.parent, result, depth + 1);
ancestors = appendParentNode(node.data.parent, ancestors, depth + 1);
}
return result;
return ancestors;
}
return appendParentNode(node.data.parent);
},
descendants(deep = false, includeOnly?: "linkedNodes" | "childNodes") {
descendants(
deep = false,
includeOnly?: "linkedNodes" | "childNodes"
): NodeId[] {
function appendChildNode(
id: NodeId,
result: NodeId[] = [],
descendants: NodeId[] = [],
depth: number = 0
) {
if (deep || (!deep && depth === 0)) {
const node = state.nodes[id];

if (!node) {
return result;
return descendants;
}

if (includeOnly !== "childNodes") {
// Include linkedNodes if any
const linkedNodes = nodeHelpers(id).linkedNodes();

linkedNodes.forEach((nodeId) => {
result.push(nodeId);
result = appendChildNode(nodeId, result, depth + 1);
descendants.push(nodeId);
descendants = appendChildNode(nodeId, descendants, depth + 1);
});
}

Expand All @@ -107,15 +110,15 @@ export function NodeHelpers(state: EditorState, id: NodeId) {
// Include child Nodes if any
if (childNodes) {
childNodes.forEach((nodeId) => {
result.push(nodeId);
result = appendChildNode(nodeId, result, depth + 1);
descendants.push(nodeId);
descendants = appendChildNode(nodeId, descendants, depth + 1);
});
}
}

return result;
return descendants;
}
return result;
return descendants;
}
return appendChildNode(id);
},
Expand Down

0 comments on commit c8f3226

Please sign in to comment.