acenturyandabit / acenturyandabit/workflowish

room for optimization here to omit the DFS if there is no search text, but be ca...

Aperta
#56 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
todo
Lingua principale
TypeScript
Stelle
0
Fork
1
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

https://api.github.com/acenturyandabit/workflowish/blob/59d27e5432f66790329e2aec60c56a985a2735a4/src/Workflowish/Subcomponents/SearchBar.tsx#L114

```javascript

return () => window.removeEventListener("keydown", listenForCtrlF);
}, [inputReference.current]);

const matchMessage = props.nMatches > 0 ? `${props.searchParams.searchSelectionIdx + 1} / ${props.nMatches} matches` : "No matches"

return


props.setSearchParams({ searchText: evt.target.value, searchSelectionIdx: 0 })}
onKeyDown={(evt) => {
if (evt.key == "ArrowUp") {
props.setSearchParams(searchParams => ({ ...searchParams, searchSelectionIdx: searchParams.searchSelectionIdx - 1 }))
window.setTimeout(props.scrollToCurrentItem,1);
} else if (evt.key == "ArrowDown") {
props.setSearchParams(searchParams => ({ ...searchParams, searchSelectionIdx: searchParams.searchSelectionIdx + 1 }));
window.setTimeout(props.scrollToCurrentItem,1);
} else if (evt.key == "Enter") {
props.focusOnCurrentItem();
}else{
// Search query was changed
window.setTimeout(props.scrollToCurrentItem,1);
}
}}
style={{ flex: "1 0 auto", padding: "2px" }}>
{props.searchParams.searchText.length > 0 ?
{matchMessage} :
null
}

}

export type HighlightStates = "SEARCH_UNCOLLAPSE" | "SEARCH_MATCH" | "SEARCH_SELECTED"

export const searchTransform = (rootNode: ItemTreeNode,
searchParams: SearchParams,
setSearchParams: React.Dispatch>
): {
rootNode: ItemTreeNode,
nMatches: number,
currentMatchId: string,
currentMatchParentChain: string[]
} => {
// TODO: room for optimization here to omit the DFS if there is no search text, but be careful!
const nodeStack: Array = [rootNode];
type DFSMetadata = {
passCount: number
node: ItemTreeNode,
isMatch: boolean,
dfsOrder: number,
parentChain: string[]
}
let dfsOrder = 0;
const dfsSeenList: Record = {
[rootNode.id]: {
passCount: 1,
node: rootNode,
isMatch: false,
dfsOrder,
parentChain: [rootNode.id]
}
};
rootNode.searchHighlight = [];
let nMatches = 0;
let currentMatchId = "";
let currentMatchParentChain: string[] = [];
while (nodeStack.length) {
const top: ItemTreeNode = nodeStack.pop() as ItemTreeNode;
if (dfsSeenList[top.id].passCount == 1) {
dfsSeenList[top.id].passCount++;
dfsSeenList[top.id].dfsOrder = dfsOrder;
dfsOrder++;
nodeStack.push(top);
// Emit all my children to the stack
top.children.forEach(child => {
dfsSeenList[child.id] = {
passCount: 1,
node: child,
isMatch: false,
dfsOrder: -1,
parentChain: [...dfsSeenList[top.id].parentChain, child.id]
}
child.searchHighlight = [];
});
const reversedChildrenForDFS = [...top.children].reverse();
nodeStack.push(...reversedChildrenForDFS);
} else if (dfsSeenList[top.id].passCount == 2) {
if (searchParams.searchText.length > 0 && top.data.toLowerCase().includes(searchParams.searchText.toLowerCase())) {
top.searchHighlight.push("SEARCH_MATCH");
if (top.id != virtualRootId) {
dfsSeenList[top.id].isMatch = true;
}
nMatches++;
}
const shouldUncollapse = top.children.reduce((shouldUncollapse, child) => shouldUncollapse
|| child.searchHighlight.includes("SEARCH_UNCOLLAPSE")
|| child.searchHighlight.includes("SEARCH_MATCH"),
false);
if (shouldUncollapse) {
top.searchHighlight.push("SEARCH_UNCOLLAPSE");
}
}
}
const searchMatchArray = Object.values(dfsSeenList)
.sort((a: DFSMetadata, b: DFSMetadata) => a.dfsOrder - b.dfsOrder)
.filter((i: DFSMetadata) => i.isMatch);
if (searchMatchArray.length > 0) {
if (searchParams.searchSelectionIdx < 0) {
setSearchParams({ ...searchParams, searchSelectionIdx: 0 });
}else if (searchParams.searchSelectionIdx > searchMatchArray.length - 1) {
setSearchParams({ ...searchParams, searchSelectionIdx: searchMatchArray.length - 1 });
} else {
searchMatchArray[searchParams.searchSelectionIdx].node.searchHighlight.push("SEARCH_SELECTED");
currentMatchId = searchMatchArray[searchParams.searchSelectionIdx].node.id;
currentMatchParentChain = searchMatchArray[searchParams.searchSelectionIdx].parentChain;
}
}

return {
rootNode: { ...rootNode },
nMatches,
currentMatchId,
currentMatchParentChain
};
}

```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.