Skip to content

Commit

Permalink
feat: colorize text nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
AykutSarac committed Sep 9, 2023
1 parent 7940d45 commit b8ae69f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
17 changes: 7 additions & 10 deletions src/components/Graph/CustomNode/ObjectNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ type RowProps = {
};

const Row = ({ val, x, y, index }: RowProps) => {
const key = JSON.stringify(val);
const rowKey = JSON.stringify(val[0]).replaceAll('"', "");
const rowValue = JSON.stringify(val[1]);

return (
<Styled.StyledRow
data-key={JSON.stringify(val)}
data-x={x}
data-y={y + index * 17.8}
$type={JSON.stringify(val[1])}
>
<Styled.StyledKey $type="object">
{JSON.stringify(val[0]).replaceAll('"', "")}:{" "}
</Styled.StyledKey>
<TextRenderer>{JSON.stringify(val[1])}</TextRenderer>
<Styled.StyledRow $value={rowValue} data-key={key} data-x={x} data-y={y + index * 17.8}>
<Styled.StyledKey $type="object">{rowKey}: </Styled.StyledKey>
<TextRenderer>{rowValue}</TextRenderer>
</Styled.StyledRow>
);
};
Expand Down
5 changes: 3 additions & 2 deletions src/components/Graph/CustomNode/TextNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const Node: React.FC<CustomNodeProps> = ({ node, x, y, hasCollapse = false }) =>
const collapseNodes = useGraph(state => state.collapseNodes);
const isExpanded = useGraph(state => state.collapsedParents.includes(id));
const isImage = imagePreview && isContentImage(text as string);
const value = JSON.stringify(text).replaceAll('"', "");

const handleExpand = (e: React.MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
Expand All @@ -81,8 +82,8 @@ const Node: React.FC<CustomNodeProps> = ({ node, x, y, hasCollapse = false }) =>
data-key={JSON.stringify(text)}
$hasCollapse={isParent && hideCollapse}
>
<Styled.StyledKey $parent={isParent} $type={type}>
<TextRenderer>{JSON.stringify(text).replaceAll('"', "")}</TextRenderer>
<Styled.StyledKey $value={value} $parent={isParent} $type={type}>
<TextRenderer>{value}</TextRenderer>
</Styled.StyledKey>
{isParent && childrenCount > 0 && showChildrenCount && (
<Styled.StyledChildrenCount>({childrenCount})</Styled.StyledChildrenCount>
Expand Down
45 changes: 26 additions & 19 deletions src/components/Graph/CustomNode/styles.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import styled, { DefaultTheme } from "styled-components";
import { NodeType } from "jsonc-parser";
import { LinkItUrl } from "react-linkify-it";
import { firaMono } from "src/constants/fonts";

function getTypeColor(value: string, theme: DefaultTheme) {
if (!Number.isNaN(+value)) return theme.NODE_COLORS.INTEGER;
if (value === "true") return theme.NODE_COLORS.BOOL.TRUE;
if (value === "false") return theme.NODE_COLORS.BOOL.FALSE;
if (value === "null") return theme.NODE_COLORS.NULL;
return theme.NODE_COLORS.NODE_VALUE;
}
type TextColorFn = {
theme: DefaultTheme;
$type?: string;
$value?: string;
$parent?: boolean;
};

function getKeyColor(theme: DefaultTheme, parent: boolean, type: NodeType) {
if (parent) {
if (type === "array") return theme.NODE_COLORS.PARENT_ARR;
return theme.NODE_COLORS.PARENT_OBJ;
}
if (type === "object") return theme.NODE_COLORS.NODE_KEY;
return theme.NODE_COLORS.TEXT;
function getTextColor({ $value, $type, $parent, theme }: TextColorFn) {
// per type
if ($parent && $type === "array") return theme.NODE_COLORS.PARENT_ARR;
if ($parent && $type === "object") return theme.NODE_COLORS.PARENT_OBJ;
if ($type === "object") return theme.NODE_COLORS.NODE_KEY;
if ($type === "array") return theme.NODE_COLORS.NODE_VALUE;

// per value
if ($value && !Number.isNaN(+$value)) return theme.NODE_COLORS.INTEGER;
if ($value === "true") return theme.NODE_COLORS.BOOL.TRUE;
if ($value === "false") return theme.NODE_COLORS.BOOL.FALSE;
if ($value === "null") return theme.NODE_COLORS.NULL;

// default
return theme.NODE_COLORS.NODE_VALUE;
}

export const StyledLinkItUrl = styled(LinkItUrl)`
Expand Down Expand Up @@ -57,20 +63,21 @@ export const StyledForeignObject = styled.foreignObject<{ $isObject?: boolean }>
}
`;

export const StyledKey = styled.span<{ $parent?: boolean; $type?: NodeType }>`
export const StyledKey = styled.span<{ $parent?: boolean; $type: string; $value?: string }>`
display: inline;
flex: 1;
color: ${({ theme, $type = "null", $parent = false }) => getKeyColor(theme, $parent, $type)};
color: ${({ theme, $type, $parent = false, $value = "" }) =>
getTextColor({ $parent, $type, $value, theme })};
font-size: ${({ $parent }) => $parent && "14px"};
overflow: hidden;
text-overflow: ellipsis;
padding: ${({ $type }) => $type !== "object" && "10px"};
white-space: nowrap;
`;

export const StyledRow = styled.span<{ $type: string }>`
export const StyledRow = styled.span<{ $value: string }>`
padding: 0 10px;
color: ${({ theme, $type }) => getTypeColor($type, theme)};
color: ${({ theme, $value }) => getTextColor({ $value, theme })};
display: block;
overflow: hidden;
text-overflow: ellipsis;
Expand Down

0 comments on commit b8ae69f

Please sign in to comment.