Skip to content

✨ Innovative and open-source visualization application that transforms various data formats, such as JSON, YAML, XML and CSV into interactive graphs.

improve rerendering of collapse/expand

AykutSaracbc69ea78d7b5
diff --git a/src/components/Graph/CustomNode/TextNode.tsx b/src/components/Graph/CustomNode/TextNode.tsx
index 2e9aa601..4eba966d 100644
--- a/src/components/Graph/CustomNode/TextNode.tsx
+++ b/src/components/Graph/CustomNode/TextNode.tsx
@@ -2,6 +2,7 @@ import React from "react";
import styled from "styled-components";
import { MdLink, MdLinkOff } from "react-icons/md";
import { CustomNodeProps } from "src/components/Graph/CustomNode";
+import useToggleHide from "src/hooks/useToggleHide";
import { isContentImage } from "src/lib/utils/graph/calculateNodeSize";
import useGraph from "src/store/useGraph";
import useStored from "src/store/useStored";
@@ -50,6 +51,7 @@ const Node: React.FC<CustomNodeProps> = ({ node, x, y, hasCollapse = false }) =>
height,
data: { isParent, childrenCount, type },
} = node;
+ const { validateHiddenNodes } = useToggleHide();
const hideCollapse = useStored(state => state.hideCollapse);
const showChildrenCount = useStored(state => state.childrenCount);
const imagePreview = useStored(state => state.imagePreview);
@@ -63,6 +65,7 @@ const Node: React.FC<CustomNodeProps> = ({ node, x, y, hasCollapse = false }) =>
if (!isExpanded) collapseNodes(id);
else expandNodes(id);
+ validateHiddenNodes();
};
return (
diff --git a/src/containers/Editor/LiveEditor/Tools.tsx b/src/containers/Editor/LiveEditor/Tools.tsx
index ab49db93..ac06ef4d 100644
--- a/src/containers/Editor/LiveEditor/Tools.tsx
+++ b/src/containers/Editor/LiveEditor/Tools.tsx
@@ -19,6 +19,7 @@ import {
VscSettingsGear,
} from "react-icons/vsc";
import { SearchInput } from "src/components/SearchInput";
+import useToggleHide from "src/hooks/useToggleHide";
import { JSONCrackLogo } from "src/layout/JsonCrackLogo";
import { getNextDirection } from "src/lib/utils/graph/getNextDirection";
import { isIframe } from "src/lib/utils/widget";
@@ -85,36 +86,18 @@ function fullscreenBrowser() {
}
}
-export const Tools: React.FC<{ isWidget?: boolean }> = ({ isWidget = false }) => {
- const getJson = useJson(state => state.getJson);
- const setVisible = useModal(state => state.setVisible);
- const direction = useGraph(state => state.direction);
- const fullscreen = useGraph(state => state.fullscreen);
- const toggleFullscreen = useGraph(state => state.toggleFullscreen);
- const zoomIn = useGraph(state => state.zoomIn);
- const zoomOut = useGraph(state => state.zoomOut);
- const centerView = useGraph(state => state.centerView);
- const foldNodes = useGraph(state => state.foldNodes);
+const ViewMenu = () => {
+ const { validateHiddenNodes } = useToggleHide();
+ const [coreKey, setCoreKey] = React.useState("CTRL");
const toggleFold = useGraph(state => state.toggleFold);
const setDirection = useGraph(state => state.setDirection);
- const graphCollapsed = useGraph(state => state.graphCollapsed);
const expandGraph = useGraph(state => state.expandGraph);
const collapseGraph = useGraph(state => state.collapseGraph);
- const setFormat = useFile(state => state.setFormat);
- const format = useFile(state => state.format);
- const [coreKey, setCoreKey] = React.useState("CTRL");
- const [logoURL, setLogoURL] = React.useState("CTRL");
-
- React.useEffect(() => {
- if (typeof window !== "undefined") {
- const url = !isIframe()
- ? "https://jsoncrack.com"
- : window.location.href.replace("widget", "editor");
-
- setCoreKey(navigator.userAgent.indexOf("Mac OS X") ? "⌘" : "CTRL");
- setLogoURL(url);
- }
- }, []);
+ const toggleFullscreen = useGraph(state => state.toggleFullscreen);
+ const foldNodes = useGraph(state => state.foldNodes);
+ const graphCollapsed = useGraph(state => state.graphCollapsed);
+ const direction = useGraph(state => state.direction);
+ const fullscreen = useGraph(state => state.fullscreen);
const toggleEditor = () => toggleFullscreen(!fullscreen);
@@ -134,15 +117,7 @@ export const Tools: React.FC<{ isWidget?: boolean }> = ({ isWidget = false }) =>
else collapseGraph();
toast(`${graphCollapsed ? "Expanded" : "Collapsed"} graph.`);
- };
-
- const handleSave = () => {
- const a = document.createElement("a");
- const file = new Blob([getJson()], { type: "text/plain" });
-
- a.href = window.URL.createObjectURL(file);
- a.download = "jsoncrack.json";
- a.click();
+ validateHiddenNodes();
};
useHotkeys([
@@ -160,6 +135,133 @@ export const Tools: React.FC<{ isWidget?: boolean }> = ({ isWidget = false }) =>
],
]);
+ React.useEffect(() => {
+ if (typeof window !== "undefined") {
+ setCoreKey(navigator.userAgent.indexOf("Mac OS X") ? "⌘" : "CTRL");
+ }
+ }, []);
+
+ return (
+ <Menu shadow="md" closeOnItemClick={false}>
+ <Menu.Target>
+ <StyledToolElement>
+ <Flex align="center" gap={3}>
+ View <CgChevronDown />
+ </Flex>
+ </StyledToolElement>
+ </Menu.Target>
+ <Menu.Dropdown>
+ <Menu.Item
+ fz={12}
+ onClick={() => {
+ toggleEditor();
+ ReactGA.event({
+ action: "toggle_hide_editor",
+ category: "User",
+ label: "Tools",
+ });
+ }}
+ icon={fullscreen ? <VscLayoutSidebarLeft /> : <VscLayoutSidebarLeftOff />}
+ rightSection={
+ <Text ml="md" fz={10} color="dimmed">
+ {coreKey} Shift E
+ </Text>
+ }
+ >
+ {fullscreen ? "Show" : "Hide"} Editor
+ </Menu.Item>
+ <Menu.Item
+ fz={12}
+ onClick={() => {
+ toggleDirection();
+ ReactGA.event({
+ action: "toggle_layout_direction",
+ category: "User",
+ label: "Tools",
+ });
+ }}
+ icon={<StyledFlowIcon rotate={rotateLayout(direction)} />}
+ rightSection={
+ <Text ml="md" fz={10} color="dimmed">
+ {coreKey} Shift D
+ </Text>
+ }
+ >
+ Rotate Layout
+ </Menu.Item>
+ <Menu.Item
+ fz={12}
+ onClick={() => {
+ toggleFoldNodes();
+ ReactGA.event({
+ action: "toggle_fold_nodes",
+ category: "User",
+ label: "Tools",
+ });
+ }}
+ icon={foldNodes ? <CgArrowsShrinkH /> : <CgArrowsMergeAltH />}
+ rightSection={
+ <Text ml="md" fz={10} color="dimmed">
+ {coreKey} Shift F
+ </Text>
+ }
+ >
+ {foldNodes ? "Unfold" : "Fold"} Nodes
+ </Menu.Item>
+ <Menu.Item
+ fz={12}
+ onClick={() => {
+ toggleExpandCollapseGraph();
+ ReactGA.event({
+ action: "toggle_collapse_nodes",
+ category: "User",
+ label: "Tools",
+ });
+ }}
+ icon={graphCollapsed ? <VscExpandAll /> : <VscCollapseAll />}
+ rightSection={
+ <Text ml="md" fz={10} color="dimmed">
+ {coreKey} Shift C
+ </Text>
+ }
+ >
+ {graphCollapsed ? "Expand" : "Collapse"} Nodes
+ </Menu.Item>
+ </Menu.Dropdown>
+ </Menu>
+ );
+};
+
+export const Tools: React.FC<{ isWidget?: boolean }> = ({ isWidget = false }) => {
+ const getJson = useJson(state => state.getJson);
+ const setVisible = useModal(state => state.setVisible);
+ const zoomIn = useGraph(state => state.zoomIn);
+ const zoomOut = useGraph(state => state.zoomOut);
+ const centerView = useGraph(state => state.centerView);
+
+ const setFormat = useFile(state => state.setFormat);
+ const format = useFile(state => state.format);
+ const [logoURL, setLogoURL] = React.useState("CTRL");
+
+ React.useEffect(() => {
+ if (typeof window !== "undefined") {
+ const url = !isIframe()
+ ? "https://jsoncrack.com"
+ : window.location.href.replace("widget", "editor");
+
+ setLogoURL(url);
+ }
+ }, []);
+
+ const handleSave = () => {
+ const a = document.createElement("a");
+ const file = new Blob([getJson()], { type: "text/plain" });
+
+ a.href = window.URL.createObjectURL(file);
+ a.download = "jsoncrack.json";
+ a.click();
+ };
+
return (
<StyledTools>
{isWidget && (
@@ -199,93 +301,7 @@ export const Tools: React.FC<{ isWidget?: boolean }> = ({ isWidget = false }) =>
<StyledToolElement title="Import File" onClick={() => setVisible("import")(true)}>
Import
</StyledToolElement>
- <Menu shadow="md" closeOnItemClick={false}>
- <Menu.Target>
- <StyledToolElement>
- <Flex align="center" gap={3}>
- View <CgChevronDown />
- </Flex>
- </StyledToolElement>
- </Menu.Target>
- <Menu.Dropdown>
- <Menu.Item
- fz={12}
- onClick={() => {
- toggleEditor();
- ReactGA.event({
- action: "toggle_hide_editor",
- category: "User",
- label: "Tools",
- });
- }}
- icon={fullscreen ? <VscLayoutSidebarLeft /> : <VscLayoutSidebarLeftOff />}
- rightSection={
- <Text ml="md" fz={10} color="dimmed">
- {coreKey} Shift E
- </Text>
- }
- >
- {fullscreen ? "Show" : "Hide"} Editor
- </Menu.Item>
- <Menu.Item
- fz={12}
- onClick={() => {
- toggleDirection();
- ReactGA.event({
- action: "toggle_layout_direction",
- category: "User",
- label: "Tools",
- });
- }}
- icon={<StyledFlowIcon rotate={rotateLayout(direction)} />}
- rightSection={
- <Text ml="md" fz={10} color="dimmed">
- {coreKey} Shift D
- </Text>
- }
- >
- Rotate Layout
- </Menu.Item>
- <Menu.Item
- fz={12}
- onClick={() => {
- toggleFoldNodes();
- ReactGA.event({
- action: "toggle_fold_nodes",
- category: "User",
- label: "Tools",
- });
- }}
- icon={foldNodes ? <CgArrowsShrinkH /> : <CgArrowsMergeAltH />}
- rightSection={
- <Text ml="md" fz={10} color="dimmed">
- {coreKey} Shift F
- </Text>
- }
- >
- {foldNodes ? "Unfold" : "Fold"} Nodes
- </Menu.Item>
- <Menu.Item
- fz={12}
- onClick={() => {
- toggleExpandCollapseGraph();
- ReactGA.event({
- action: "toggle_collapse_nodes",
- category: "User",
- label: "Tools",
- });
- }}
- icon={graphCollapsed ? <VscExpandAll /> : <VscCollapseAll />}
- rightSection={
- <Text ml="md" fz={10} color="dimmed">
- {coreKey} Shift C
- </Text>
- }
- >
- {graphCollapsed ? "Expand" : "Collapse"} Nodes
- </Menu.Item>
- </Menu.Dropdown>
- </Menu>
+ <ViewMenu />
<Menu shadow="md">
<Menu.Target>
<StyledToolElement>
diff --git a/src/hooks/useToggleHide.ts b/src/hooks/useToggleHide.ts
index 06e9abe7..392b0e97 100644
--- a/src/hooks/useToggleHide.ts
+++ b/src/hooks/useToggleHide.ts
@@ -2,19 +2,12 @@ import React from "react";
import useGraph from "src/store/useGraph";
const useToggleHide = () => {
- const collapsedNodes = useGraph(state => state.collapsedNodes);
- const collapsedEdges = useGraph(state => state.collapsedEdges);
-
- const nodeList = React.useMemo(
- () => collapsedNodes.map(id => `[id$="node-${id}"]`),
- [collapsedNodes]
- );
- const edgeList = React.useMemo(
- () => collapsedEdges.map(id => `[class$="edge-${id}"]`),
- [collapsedEdges]
- );
+ const getCollapsedNodeIds = useGraph(state => state.getCollapsedNodeIds);
+ const getCollapsedEdgeIds = useGraph(state => state.getCollapsedEdgeIds);
const validateHiddenNodes = React.useCallback(() => {
+ const nodeList = getCollapsedNodeIds().map(id => `[id$="node-${id}"]`);
+ const edgeList = getCollapsedEdgeIds().map(id => `[class$="edge-${id}"]`);
const hiddenItems = document.body.querySelectorAll(".hide");
hiddenItems.forEach(item => item.classList.remove("hide"));
@@ -30,7 +23,7 @@ const useToggleHide = () => {
selectedEdges.forEach(edge => edge.classList.add("hide"));
}
- }, [nodeList, edgeList]);
+ }, [getCollapsedEdgeIds, getCollapsedNodeIds]);
React.useEffect(() => {
validateHiddenNodes();
diff --git a/src/store/useGraph.ts b/src/store/useGraph.ts
index 596027f8..594e8ca7 100644
--- a/src/store/useGraph.ts
+++ b/src/store/useGraph.ts
@@ -49,6 +49,8 @@ interface GraphActions {
expandGraph: () => void;
collapseNodes: (nodeId: string) => void;
collapseGraph: () => void;
+ getCollapsedNodeIds: () => string[];
+ getCollapsedEdgeIds: () => string[];
toggleFold: (value: boolean) => void;
toggleFullscreen: (value: boolean) => void;
zoomIn: () => void;
@@ -60,6 +62,8 @@ interface GraphActions {
const useGraph = create<Graph & GraphActions>((set, get) => ({
...initialStates,
clearGraph: () => set({ nodes: [], edges: [], loading: false }),
+ getCollapsedNodeIds: () => get().collapsedNodes,
+ getCollapsedEdgeIds: () => get().collapsedEdges,
setSelectedNode: nodeData => set({ selectedNode: nodeData }),
setGraph: (data, options) => {
const { nodes, edges } = parser(data ?? useJson.getState().json);