Skip to content

Commit

Permalink
perf: node sort by area
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Oct 25, 2024
1 parent f151765 commit c05a68f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/utils/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,18 @@ export const findNodesByXy = (
!equalRectNode(node, other),
);
});
results.sort((a, b) => {
return getNodeArea(a) - getNodeArea(b);
});
return results;
};

const getNodeArea = (node: RawNode) => {
const w = node.attr.width ?? node.attr.right - node.attr.left;
const h = node.attr.height ?? node.attr.bottom - node.attr.top;
return w * h;
};

export function* traverseNode(node: RawNode, skipKeys: number[] = []) {
const stack: RawNode[] = [];
stack.push(node);
Expand Down
2 changes: 2 additions & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export interface RawAttr {
top: number;
right: number;
bottom: number;
width: number;
height: number;
_id?: number;
_pid?: number;
}
Expand Down

0 comments on commit c05a68f

Please sign in to comment.