From d9272a7b24144d9b3c6bb37faa2914adf8a3c365 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Sun, 24 Apr 2022 15:46:08 +0800 Subject: [PATCH] fix: some items are missing from search results --- src/components/searchModes/regexMode.tsx | 5 +++- src/utils/VisibleNodesGenerator/index.ts | 37 +++++++++--------------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/components/searchModes/regexMode.tsx b/src/components/searchModes/regexMode.tsx index 93714b5..95cd473 100644 --- a/src/components/searchModes/regexMode.tsx +++ b/src/components/searchModes/regexMode.tsx @@ -9,7 +9,10 @@ export const regexMode: ModeShape = { const regexp = searchKeyToRegexp(searchKey) if (regexp) { return { - matchNode: node => regexp.test(node.name), + matchNode: node => { + regexp.lastIndex = 0 + return regexp.test(node.name) + }, } } diff --git a/src/utils/VisibleNodesGenerator/index.ts b/src/utils/VisibleNodesGenerator/index.ts index 575d236..941b321 100644 --- a/src/utils/VisibleNodesGenerator/index.ts +++ b/src/utils/VisibleNodesGenerator/index.ts @@ -6,41 +6,30 @@ function search( match: (node: TreeNode) => boolean, onChildMatch: (node: TreeNode) => void, ): TreeNode | null { - // go traverse no matter root matches or not to make sure find all nodes + // go traverse no matter whether root matches to make sure find & expand the related nodes + // The `related nodes` are the nodes that either itself matches or any of direct or indirect children match const contents = [] if (root.type === 'tree' && root.contents) { - let childMatch = false - for (const node of root.contents) { - if (match(node)) { - childMatch = true - break - } - } - for (const node of root.contents) { const $node = search(node, match, onChildMatch) - if ($node) { - if ($node !== node) childMatch = true - contents.push($node) - } + if ($node) contents.push($node) } - if (childMatch) { - onChildMatch(root) - } + if (contents.length) onChildMatch(root) + } - if (contents?.length) { - return { - ...root, - contents, - } + // Return root if itself matches + if (match(root)) return root + + // Otherwise, but when deeper nodes match, return partial root + if (contents.length) { + return { + ...root, + contents, } } - if (match(root)) { - return root - } return null }