Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Nodes focusing and highlighting to reflect JSON edits in Graph … #392

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/components/SearchInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { getHotkeyHandler } from "@mantine/hooks";
import ReactGA from "react-ga4";
import { AiOutlineSearch } from "react-icons/ai";
import { useFocusNode } from "src/hooks/useFocusNode";
import useGraph from "src/store/useGraph";

export const SearchInput: React.FC = () => {
const [searchValue, setValue, skip, nodeCount, currentNode] = useFocusNode();
const setSearchInputValue = useGraph(state => state.setSearchInputValue);

return (
<TextInput
Expand All @@ -15,7 +17,10 @@ export const SearchInput: React.FC = () => {
id="search-node"
w={180}
value={searchValue}
onChange={e => setValue(e.currentTarget.value)}
onChange={e => {
setValue(e.currentTarget.value);
setSearchInputValue(e.currentTarget.value);
}}
onFocus={() => ReactGA.event({ action: "focus_node_search", category: "User" })}
placeholder="Search Node"
onKeyDown={getHotkeyHandler([["Enter", skip]])}
Expand Down
9 changes: 7 additions & 2 deletions src/containers/Views/GraphView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ElkRoot } from "reaflow/dist/layout/useLayout";
import { useLongPress } from "use-long-press";
import { CustomNode } from "src/containers/Views/GraphView/CustomNode";
import { ViewMode } from "src/enums/viewMode.enum";
import { useFocusNode } from "src/hooks/useFocusNode";
import useToggleHide from "src/hooks/useToggleHide";
import { Loading } from "src/layout/Loading";
import useConfig from "src/store/useConfig";
Expand Down Expand Up @@ -163,7 +164,8 @@ export const Graph = ({ isWidget = false }: GraphProps) => {
const gesturesEnabled = useConfig(state => state.gesturesEnabled);
const rulersEnabled = useConfig(state => state.rulersEnabled);
const setViewMode = useConfig(state => state.setViewMode);

const searchInputValue = useGraph(state => state.searchInputValue);
const [, setValue] = useFocusNode();
const callback = React.useCallback(() => {
const canvas = document.querySelector(".jsoncrack-canvas") as HTMLDivElement | null;
canvas?.classList.add("dragging");
Expand Down Expand Up @@ -209,7 +211,10 @@ export const Graph = ({ isWidget = false }: GraphProps) => {
{...bindLongPress()}
>
<Space
onUpdated={() => debouncedOnZoomChangeHandler()}
onUpdated={() => {
setValue(searchInputValue);
debouncedOnZoomChangeHandler();
}}
onCreate={setViewPort}
onContextMenu={e => e.preventDefault()}
treatTwoFingerTrackPadGesturesLikeTouch={gesturesEnabled}
Expand Down
7 changes: 3 additions & 4 deletions src/hooks/useFocusNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const useFocusNode = () => {
const [debouncedValue] = useDebouncedValue(value, 600);

const skip = () => setSelectedNode(current => (current + 1) % nodeCount);

const matchedNodes: NodeListOf<Element> = searchQuery(`span[data-key*='${debouncedValue}' i]`);
const matchedNode: Element | null = matchedNodes[selectedNode] || null;
React.useEffect(() => {
if (!value) {
cleanupHighlight();
Expand All @@ -21,8 +22,6 @@ export const useFocusNode = () => {
}

if (!viewPort || !debouncedValue) return;
const matchedNodes: NodeListOf<Element> = searchQuery(`span[data-key*='${debouncedValue}' i]`);
const matchedNode: Element | null = matchedNodes[selectedNode] || null;

cleanupHighlight();

Expand All @@ -37,7 +36,7 @@ export const useFocusNode = () => {
setSelectedNode(0);
setNodeCount(0);
}
}, [selectedNode, debouncedValue, value, viewPort]);
}, [selectedNode, debouncedValue, value, viewPort, matchedNodes, matchedNode]);

return [value, setValue, skip, nodeCount, selectedNode] as const;
};
4 changes: 4 additions & 0 deletions src/store/useGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Graph {
collapsedParents: string[];
selectedNode: NodeData | null;
path: string;
searchInputValue: string;
}

const initialStates: Graph = {
Expand All @@ -37,6 +38,7 @@ const initialStates: Graph = {
collapsedParents: [],
selectedNode: null,
path: "",
searchInputValue: "",
};

interface GraphActions {
Expand All @@ -59,6 +61,7 @@ interface GraphActions {
centerView: () => void;
clearGraph: () => void;
setZoomFactor: (zoomFactor: number) => void;
setSearchInputValue: (searchInputValue: string) => void;
}

const useGraph = create<Graph & GraphActions>((set, get) => ({
Expand Down Expand Up @@ -214,6 +217,7 @@ const useGraph = create<Graph & GraphActions>((set, get) => ({
},
toggleFullscreen: fullscreen => set({ fullscreen }),
setViewPort: viewPort => set({ viewPort }),
setSearchInputValue: searchInputValue => set({ searchInputValue }),
}));

export default useGraph;
Loading