Skip to content

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

fix expand/collapse all

AykutSarac6456e41671e5
diff --git a/.prettierrc b/.prettierrc
index c5f39a8e..2e798406 100644
--- a/.prettierrc
+++ b/.prettierrc
@@ -2,7 +2,7 @@
"trailingComma": "es5",
"singleQuote": false,
"semi": true,
- "printWidth": 85,
+ "printWidth": 100,
"arrowParens": "avoid",
"importOrder": [
"^(react/(.*)$)|^(react$)",
diff --git a/src/components/Graph/index.tsx b/src/components/Graph/index.tsx
index a6a0fb0b..9fc4ed9c 100644
--- a/src/components/Graph/index.tsx
+++ b/src/components/Graph/index.tsx
@@ -82,26 +82,27 @@ const GraphComponent = ({
(areaSize * 100) / (size.width * size.height) - 100
);
+ setSize({
+ width: (layout.width as number) + 400,
+ height: (layout.height as number) + 400,
+ });
+
requestAnimationFrame(() => {
- setSize({
- width: (layout.width as number) + 400,
- height: (layout.height as number) + 400,
- });
setTimeout(() => {
setLoading(false);
- setTimeout(() => (changeRatio > 65 || isWidget) && centerView(), 0);
- }, 0);
+ setTimeout(() => {
+ if (changeRatio > 65 || isWidget) centerView();
+ });
+ });
});
}
},
- [size.width, size.height, setLoading, isWidget, centerView]
+ [centerView, isWidget, setLoading, size.height, size.width]
);
const onCanvasClick = React.useCallback(() => {
- requestAnimationFrame(() => {
- const input = document.querySelector("input:focus") as HTMLInputElement;
- if (input) input.blur();
- });
+ const input = document.querySelector("input:focus") as HTMLInputElement;
+ if (input) input.blur();
}, []);
if (nodes.length > 8_000) return <ErrorView />;
diff --git a/src/components/Modal/styles.tsx b/src/components/Modal/styles.tsx
index e76e9601..6880aa42 100644
--- a/src/components/Modal/styles.tsx
+++ b/src/components/Modal/styles.tsx
@@ -39,7 +39,7 @@ export const ModalInnerWrapper = styled.div<{ size: "sm" | "md" | "lg" }>`
export const Title = styled.h2`
color: ${({ theme }) => theme.INTERACTIVE_ACTIVE};
font-size: 20px !important;
- margin: 0;
+ margin: 0 !important;
`;
export const HeaderWrapper = styled.div`
diff --git a/src/containers/Editor/LiveEditor/GraphCanvas.tsx b/src/containers/Editor/LiveEditor/GraphCanvas.tsx
index 7b7a4af8..504f7352 100644
--- a/src/containers/Editor/LiveEditor/GraphCanvas.tsx
+++ b/src/containers/Editor/LiveEditor/GraphCanvas.tsx
@@ -7,11 +7,10 @@ export const GraphCanvas = ({ isWidget = false }: { isWidget?: boolean }) => {
const [isModalVisible, setModalVisible] = React.useState(false);
const [selectedNode, setSelectedNode] = React.useState<[string, string][]>([]);
- const openModal = React.useCallback(() => setModalVisible(true), []);
-
const collapsedNodes = useGraph(state => state.collapsedNodes);
const collapsedEdges = useGraph(state => state.collapsedEdges);
- const loading = useGraph(state => state.loading);
+
+ const openModal = React.useCallback(() => setModalVisible(true), []);
React.useEffect(() => {
const nodeList = collapsedNodes.map(id => `[id$="node-${id}"]`);
@@ -20,14 +19,16 @@ export const GraphCanvas = ({ isWidget = false }: { isWidget?: boolean }) => {
const hiddenItems = document.querySelectorAll(".hide");
hiddenItems.forEach(item => item.classList.remove("hide"));
- if (nodeList.length > 1) {
+ if (nodeList.length) {
const selectedNodes = document.querySelectorAll(nodeList.join(","));
- const selectedEdges = document.querySelectorAll(edgeList.join(","));
-
selectedNodes.forEach(node => node.classList.add("hide"));
+ }
+
+ if (edgeList.length) {
+ const selectedEdges = document.querySelectorAll(edgeList.join(","));
selectedEdges.forEach(edge => edge.classList.add("hide"));
}
- }, [collapsedNodes, collapsedEdges, loading]);
+ }, [collapsedNodes, collapsedEdges]);
return (
<>
diff --git a/src/hooks/useHideNodes.tsx b/src/hooks/useHideNodes.tsx
new file mode 100644
index 00000000..4d22b9d5
--- /dev/null
+++ b/src/hooks/useHideNodes.tsx
@@ -0,0 +1,33 @@
+import React from "react";
+import useGraph from "src/store/useGraph";
+
+const useHideNodes = () => {
+ 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 checkNodes = () => {
+ const hiddenItems = document.querySelectorAll(".hide");
+ hiddenItems.forEach(item => item.classList.remove("hide"));
+
+ if (nodeList.length > 1) {
+ const selectedNodes = document.querySelectorAll(nodeList.join(","));
+ const selectedEdges = document.querySelectorAll(edgeList.join(","));
+
+ selectedNodes.forEach(node => node.classList.add("hide"));
+ selectedEdges.forEach(edge => edge.classList.add("hide"));
+ }
+ };
+
+ return { checkNodes };
+};
+
+export default useHideNodes;
diff --git a/src/pages/widget.tsx b/src/pages/widget.tsx
index 5b22793a..c68c6577 100644
--- a/src/pages/widget.tsx
+++ b/src/pages/widget.tsx
@@ -80,11 +80,13 @@ const WidgetPage = () => {
const hiddenItems = document.querySelectorAll(".hide");
hiddenItems.forEach(item => item.classList.remove("hide"));
- if (nodeList.length > 1) {
+ if (nodeList.length) {
const selectedNodes = document.querySelectorAll(nodeList.join(","));
- const selectedEdges = document.querySelectorAll(edgeList.join(","));
-
selectedNodes.forEach(node => node.classList.add("hide"));
+ }
+
+ if (edgeList.length) {
+ const selectedEdges = document.querySelectorAll(edgeList.join(","));
selectedEdges.forEach(edge => edge.classList.add("hide"));
}
diff --git a/src/store/useGraph.tsx b/src/store/useGraph.tsx
index e6a02b60..7a9c6b97 100644
--- a/src/store/useGraph.tsx
+++ b/src/store/useGraph.tsx
@@ -5,6 +5,7 @@ import { getChildrenEdges } from "src/utils/getChildrenEdges";
import { getOutgoers } from "src/utils/getOutgoers";
import { parser } from "src/utils/jsonParser";
import create from "zustand";
+import useJson from "./useJson";
const initialStates = {
zoomPanPinch: undefined as ReactZoomPanPinchRef | undefined,
@@ -85,18 +86,12 @@ const useGraph = create<Graph & GraphActions>((set, get) => ({
const matchingNodesConnectedToParent = matchingNodes.filter(node =>
nodesConnectedToParent.includes(node)
);
- const nodeIds = childrenNodes
- .map(node => node.id)
- .concat(matchingNodesConnectedToParent);
+ const nodeIds = childrenNodes.map(node => node.id).concat(matchingNodesConnectedToParent);
const edgeIds = childrenEdges.map(edge => edge.id);
const collapsedParents = get().collapsedParents.filter(cp => cp !== nodeId);
- const collapsedNodes = get().collapsedNodes.filter(
- nodeId => !nodeIds.includes(nodeId)
- );
- const collapsedEdges = get().collapsedEdges.filter(
- edgeId => !edgeIds.includes(edgeId)
- );
+ const collapsedNodes = get().collapsedNodes.filter(nodeId => !nodeIds.includes(nodeId));
+ const collapsedEdges = get().collapsedEdges.filter(edgeId => !edgeIds.includes(edgeId));
set({
collapsedParents,
@@ -128,19 +123,19 @@ const useGraph = create<Graph & GraphActions>((set, get) => ({
.filter(edge => parentNodesIds.includes(edge.from))
.map(edge => edge.to);
+ const collapsedParents = get()
+ .nodes.filter(node => !parentNodesIds.includes(node.id) && node.data.parent)
+ .map(node => node.id);
+
+ const collapsedNodes = get()
+ .nodes.filter(
+ node => !parentNodesIds.includes(node.id) && !secondDegreeNodesIds.includes(node.id)
+ )
+ .map(node => node.id);
+
set({
- collapsedParents: get()
- .nodes.filter(
- node => !parentNodesIds.includes(node.id) && node.data.isParent
- )
- .map(node => node.id),
- collapsedNodes: get()
- .nodes.filter(
- node =>
- !parentNodesIds.includes(node.id) &&
- !secondDegreeNodesIds.includes(node.id)
- )
- .map(node => node.id),
+ collapsedParents,
+ collapsedNodes,
collapsedEdges: get()
.edges.filter(edge => !parentNodesIds.includes(edge.from))
.map(edge => edge.id),
@@ -181,7 +176,12 @@ const useGraph = create<Graph & GraphActions>((set, get) => ({
const canvas = document.querySelector(".jsoncrack-canvas") as HTMLElement;
if (zoomPanPinch && canvas) zoomPanPinch.zoomToElement(canvas);
},
- toggleFold: foldNodes => set({ foldNodes }),
+ toggleFold: foldNodes => {
+ const { json } = useJson.getState();
+ const { nodes, edges } = parser(json, foldNodes);
+
+ set({ nodes, edges, foldNodes });
+ },
togglePerfMode: performanceMode => set({ performanceMode }),
toggleFullscreen: fullscreen => set({ fullscreen }),
setZoomPanPinch: zoomPanPinch => set({ zoomPanPinch }),
diff --git a/src/typings/types.d.ts b/src/typings/types.d.ts
index 30e39e4e..9dc69eaa 100644
--- a/src/typings/types.d.ts
+++ b/src/typings/types.d.ts
@@ -1,11 +1,5 @@
type CanvasDirection = "LEFT" | "RIGHT" | "DOWN" | "UP";
-interface CustomNodeData {
- isParent: true;
- childrenCount: children.length;
- children: NodeData[];
-}
-
interface NodeData<T = any> {
id: string;
disabled?: boolean;