Skip to content

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

refactor: remove image handling logic and associated styles from TextNode and calculateNodeSize

AykutSarac4f949a56b352
diff --git a/apps/www/src/features/editor/views/GraphView/CustomNode/TextNode.tsx b/apps/www/src/features/editor/views/GraphView/CustomNode/TextNode.tsx
index 9c75b23e..d5afeb78 100644
--- a/apps/www/src/features/editor/views/GraphView/CustomNode/TextNode.tsx
+++ b/apps/www/src/features/editor/views/GraphView/CustomNode/TextNode.tsx
@@ -1,7 +1,6 @@
import React from "react";
import styled from "styled-components";
import type { CustomNodeProps } from ".";
-import { isContentImage } from "../lib/utils/calculateNodeSize";
import { TextRenderer } from "./TextRenderer";
import * as Styled from "./styles";
@@ -15,19 +14,8 @@ const StyledTextNodeWrapper = styled.span<{ $isParent: boolean }>`
padding: 0 10px;
`;
-const StyledImageWrapper = styled.div`
- padding: 5px;
-`;
-
-const StyledImage = styled.img`
- border-radius: 2px;
- object-fit: contain;
- background: ${({ theme }) => theme.BACKGROUND_MODIFIER_ACCENT};
-`;
-
const Node = ({ node, x, y }: CustomNodeProps) => {
const { text, width, height } = node;
- const isImage = isContentImage(JSON.stringify(text[0].value));
const value = text[0].value;
return (
@@ -38,22 +26,16 @@ const Node = ({ node, x, y }: CustomNodeProps) => {
x={0}
y={0}
>
- {isImage ? (
- <StyledImageWrapper>
- <StyledImage src={JSON.stringify(text[0].value)} width="70" height="70" loading="lazy" />
- </StyledImageWrapper>
- ) : (
- <StyledTextNodeWrapper
- data-x={x}
- data-y={y}
- data-key={JSON.stringify(text)}
- $isParent={false}
- >
- <Styled.StyledKey $value={value} $type={typeof text[0].value}>
- <TextRenderer>{value}</TextRenderer>
- </Styled.StyledKey>
- </StyledTextNodeWrapper>
- )}
+ <StyledTextNodeWrapper
+ data-x={x}
+ data-y={y}
+ data-key={JSON.stringify(text)}
+ $isParent={false}
+ >
+ <Styled.StyledKey $value={value} $type={typeof text[0].value}>
+ <TextRenderer>{value}</TextRenderer>
+ </Styled.StyledKey>
+ </StyledTextNodeWrapper>
</Styled.StyledForeignObject>
);
};
diff --git a/apps/www/src/features/editor/views/GraphView/lib/utils/calculateNodeSize.ts b/apps/www/src/features/editor/views/GraphView/lib/utils/calculateNodeSize.ts
index d4259f85..14f1efa9 100644
--- a/apps/www/src/features/editor/views/GraphView/lib/utils/calculateNodeSize.ts
+++ b/apps/www/src/features/editor/views/GraphView/lib/utils/calculateNodeSize.ts
@@ -3,15 +3,6 @@ import { NODE_DIMENSIONS } from "../../../../../../constants/graph";
type Text = number | string | [string, string][];
type Size = { width: number; height: number };
-export const isContentImage = (value: Text) => {
- if (typeof value !== "string") return false;
-
- const isImageURL = /(https?:\/\/.*\.(?:png|jpg|gif|svg))/i.test(value);
- const isBase64 = value.startsWith("data:image/") && value.includes("base64");
-
- return isImageURL || isBase64;
-};
-
const calculateLines = (text: Text): string => {
if (Array.isArray(text)) {
return text.map(([k, v]) => `${k}: ${JSON.stringify(v).slice(0, 80)}`).join("\n");
@@ -50,8 +41,6 @@ const sizeCache = new Map<Text, Size>();
setInterval(() => sizeCache.clear(), 120_000);
export const calculateNodeSize = (text: Text, isParent = false) => {
- const isImage = isContentImage(text);
-
const cacheKey = [text, isParent].toString();
// check cache if data already exists
@@ -63,11 +52,6 @@ export const calculateNodeSize = (text: Text, isParent = false) => {
const lines = calculateLines(text);
const sizes = calculateWidthAndHeight(lines, typeof text === "string");
- if (isImage) {
- sizes.width = 80;
- sizes.height = 80;
- }
-
if (isParent) sizes.width += 80;
if (sizes.width > 700) sizes.width = 700;
diff --git a/packages/jsoncrack-react/src/components/Node.module.css b/packages/jsoncrack-react/src/components/Node.module.css
index d3c4bf1a..c341fbfd 100644
--- a/packages/jsoncrack-react/src/components/Node.module.css
+++ b/packages/jsoncrack-react/src/components/Node.module.css
@@ -47,15 +47,6 @@
padding: 0 10px;
}
-.imageWrapper {
- padding: 5px;
-}
-
-.image {
- border-radius: 2px;
- object-fit: contain;
-}
-
.foreignObject:global(.searched) {
background: rgba(27, 255, 0, 0.1);
border: 1px solid var(--text-positive);
diff --git a/packages/jsoncrack-react/src/components/TextNode.tsx b/packages/jsoncrack-react/src/components/TextNode.tsx
index b544a705..3f428a98 100644
--- a/packages/jsoncrack-react/src/components/TextNode.tsx
+++ b/packages/jsoncrack-react/src/components/TextNode.tsx
@@ -1,6 +1,5 @@
import React from "react";
import type { NodeData } from "../types";
-import { isContentImage } from "../utils/calculateNodeSize";
import styles from "./Node.module.css";
import { TextRenderer } from "./TextRenderer";
import { getTextColor } from "./nodeStyles";
@@ -18,8 +17,6 @@ const TextNodeBase = ({ node, x, y }: TextNodeProps) => {
if (!firstRow) return null;
const value = firstRow.value;
- const normalizedValue = typeof value === "string" ? value : `${value}`;
- const isImage = isContentImage(normalizedValue);
return (
<foreignObject
@@ -30,33 +27,16 @@ const TextNodeBase = ({ node, x, y }: TextNodeProps) => {
x={0}
y={0}
>
- {isImage ? (
- <div className={styles.imageWrapper}>
- <img
- className={styles.image}
- style={{ background: "var(--background-modifier-accent)" }}
- src={normalizedValue}
- alt=""
- width="70"
- height="70"
- loading="lazy"
- />
- </div>
- ) : (
- <span
- className={styles.textNodeWrapper}
- data-x={x}
- data-y={y}
- data-key={JSON.stringify(text)}
- >
- <span
- className={styles.key}
- style={{ color: getTextColor({ value, type: typeof value }) }}
- >
- <TextRenderer>{value}</TextRenderer>
- </span>
+ <span
+ className={styles.textNodeWrapper}
+ data-x={x}
+ data-y={y}
+ data-key={JSON.stringify(text)}
+ >
+ <span className={styles.key} style={{ color: getTextColor({ value, type: typeof value }) }}>
+ <TextRenderer>{value}</TextRenderer>
</span>
- )}
+ </span>
</foreignObject>
);
};
diff --git a/packages/jsoncrack-react/src/utils/calculateNodeSize.ts b/packages/jsoncrack-react/src/utils/calculateNodeSize.ts
index 8ebe547f..969825ae 100644
--- a/packages/jsoncrack-react/src/utils/calculateNodeSize.ts
+++ b/packages/jsoncrack-react/src/utils/calculateNodeSize.ts
@@ -10,15 +10,6 @@ const CACHE_TTL_MS = 120_000;
const sizeCache = new Map<string, Size>();
let lastCacheClearAt = Date.now();
-export const isContentImage = (value: Text) => {
- if (typeof value !== "string") return false;
-
- const isImageURL = /(https?:\/\/.*\.(?:png|jpg|jpeg|gif|svg|webp))/i.test(value);
- const isBase64 = value.startsWith("data:image/") && value.includes("base64");
-
- return isImageURL || isBase64;
-};
-
const calculateLines = (text: Text): string => {
if (Array.isArray(text)) {
return text.map(([k, v]) => `${k}: ${JSON.stringify(v).slice(0, 80)}`).join("\n");
@@ -76,7 +67,6 @@ const maybeClearCache = () => {
export const calculateNodeSize = (text: Text, isParent = false) => {
maybeClearCache();
- const isImage = isContentImage(text);
const cacheKey = `${JSON.stringify(text)}-${isParent}`;
const cached = sizeCache.get(cacheKey);
@@ -85,11 +75,6 @@ export const calculateNodeSize = (text: Text, isParent = false) => {
const lines = calculateLines(text);
const sizes = calculateWidthAndHeight(lines, typeof text === "string");
- if (isImage) {
- sizes.width = 80;
- sizes.height = 80;
- }
-
if (isParent) sizes.width += 80;
if (sizes.width > 700) sizes.width = 700;