Highlight all the matched nodes after performing search

This commit is contained in:
Burak Ozarslan 2022-04-28 22:48:18 +03:00
parent d9bd674607
commit e671358c01
4 changed files with 24620 additions and 4563 deletions

20067
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,12 @@
import React from "react";
import { useConfig } from "src/hocs/config";
import {
searchQuery,
cleanupHighlight,
highlightMatchedNodes,
} from "src/utils/search";
type Content = { value: string; debounced: string };
export const useFocusNode = () => {
@ -25,29 +31,26 @@ export const useFocusNode = () => {
if (!settings.zoomPanPinch) return;
const zoomPanPinch = settings.zoomPanPinch.instance.wrapperComponent;
const node = document.querySelector(
const matchedNodes: NodeListOf<Element> = searchQuery(
`span[data-key*='${content.debounced}' i]`
);
const firstMatchedNode: Element | null = matchedNodes[0] || null;
document
.querySelector("foreignObject.searched")
?.classList.remove("searched");
cleanupHighlight();
if (zoomPanPinch && node && node.parentElement) {
if (zoomPanPinch && firstMatchedNode && firstMatchedNode.parentElement) {
const newScale = 1;
const x = Number(node.getAttribute("data-x"));
const y = Number(node.getAttribute("data-y"));
const x = Number(firstMatchedNode.getAttribute("data-x"));
const y = Number(firstMatchedNode.getAttribute("data-y"));
const newPositionX =
(zoomPanPinch.offsetLeft - x) * newScale +
node.getBoundingClientRect().width;
firstMatchedNode.getBoundingClientRect().width;
const newPositionY =
(zoomPanPinch.offsetTop - y) * newScale +
node.getBoundingClientRect().height;
firstMatchedNode.getBoundingClientRect().height;
node.parentElement.parentElement
?.closest("foreignObject")
?.classList.toggle("searched");
highlightMatchedNodes(matchedNodes);
settings.zoomPanPinch?.setTransform(newPositionX, newPositionY, newScale);
}

19
src/utils/search.ts Normal file
View File

@ -0,0 +1,19 @@
export const searchQuery = (param: string) => {
return document.querySelectorAll(`${param}`);
};
export const cleanupHighlight = () => {
const nodes = document.querySelectorAll("foreignObject.searched");
nodes?.forEach((node) => {
node.classList.remove("searched");
});
};
export const highlightMatchedNodes = (nodes: NodeListOf<Element>) => {
nodes?.forEach((node) => {
node.parentElement?.parentElement
?.closest("foreignObject")
?.classList.add("searched");
});
};

9070
yarn.lock

File diff suppressed because it is too large Load Diff