improve array text detection

This commit is contained in:
Aykut Saraç 2022-03-18 10:55:41 +03:00
parent 82320ad5d7
commit 0f94dc9046
7 changed files with 48 additions and 30 deletions

View File

@ -5,7 +5,7 @@ enum ButtonType {
PRIMARY = "SILVER_DARK",
SECONDARY = "BLURPLE",
DANGER = "DANGER",
SUCCESS = "SEAGREEN",
SUCCESS = "LIGHTGREEN",
WARNING = "ORANGE",
}

View File

@ -15,7 +15,7 @@ const ObjectNode: React.FC<CustomNodeProps<[string, unknown][]>> = ({
(val, idx) =>
val[1] && (
<Styled.StyledRow key={idx} width={width}>
<Styled.StyledKey>{val[0]}: </Styled.StyledKey>
<Styled.StyledKey objectKey>{val[0]}: </Styled.StyledKey>
{val[1]}
</Styled.StyledRow>
)

View File

@ -2,12 +2,17 @@ import React from "react";
import { CustomNodeProps } from ".";
import * as Styled from "./styles";
const BondNode: React.FC<CustomNodeProps<string>> = ({ width, height, value }) => {
const BondNode: React.FC<CustomNodeProps<string>> = ({
width,
height,
value,
isParent = false,
}) => {
return (
<Styled.StyledForeignObject width={width} height={height} x={0} y={0}>
<Styled.StyledTextWrapper>
<Styled.StyledText width={width} height={height}>
<Styled.StyledKey bond>{value}</Styled.StyledKey>
<Styled.StyledKey parent={isParent}>{value}</Styled.StyledKey>
</Styled.StyledText>
</Styled.StyledTextWrapper>
</Styled.StyledForeignObject>

View File

@ -1,13 +1,13 @@
import React from "react";
import { Label, Node, Port, NodeProps } from "reaflow";
import ArrayNode from "./ArrayNode";
import BondNode from "./BondNode";
import ObjectNode from "./ObjectNode";
import TextNode from "./TextNode";
export interface CustomNodeProps<T> {
width: number;
height: number;
value: T;
isParent?: boolean;
}
const baseLabelStyle = {
@ -33,16 +33,18 @@ export const CustomNode = (nodeProps: NodeProps) => {
const { width, height } = nodeProps;
if (data.text instanceof Object) {
if (Object.keys(data.text).every((val) => !isNaN(+val))) {
const text = Object.values(data.text).join("");
return <ArrayNode width={width} height={height} value={text} />;
}
const entries = Object.entries(data.text);
return <ObjectNode width={width} height={height} value={entries} />;
}
return <BondNode width={width} height={height} value={data.text} />;
return (
<TextNode
isParent={data.data.isParent}
width={width}
height={height}
value={data.text}
/>
);
}}
</Node>
);

View File

@ -22,6 +22,9 @@ export function getEdgeNodes(graph: any, isExpanded: boolean = true): any {
nodes.push({
id: el.id,
text: el.text,
data: {
isParent: el.parent,
},
width: isExpanded ? 35 + longestLine * 8 : 180,
height: isExpanded ? 30 + lines.length * 10 : 50,
});

View File

@ -19,7 +19,7 @@ declare module "styled-components" {
CRIMSON: string;
DARK_SALMON: string;
DANGER: string;
SEAGREEN: string;
LIGHTGREEN: string;
ORANGE: string;
SILVER: string;
SILVER_DARK: string;

View File

@ -14,23 +14,31 @@ export const parser = (input: string | string[]) => {
) => {
if (!os) return [];
return [os].flat().map((o) => ({
id: nextId(),
text: Object.fromEntries(
Object.entries(o).filter(
([k, v]) => !Array.isArray(v) && !(v instanceof Object)
)
),
children: Object.entries(o)
.filter(([k, v]) => Array.isArray(v) || typeof v === "object")
.flatMap(([k, v]) => [
{
id: nextId(),
text: k,
children: extract(v, nextId),
},
]),
}));
return [os].flat().map((o) => {
const isObject = o instanceof Object;
return {
id: nextId(),
text: !isObject
? o.toString()
: Object.fromEntries(
Object.entries(o).filter(
([k, v]) => !Array.isArray(v) && !(v instanceof Object)
)
),
parent: false,
children: Object.entries(o)
.filter(([k, v]) => Array.isArray(v) || typeof v === "object")
.flatMap(([k, v]) => [
{
id: nextId(),
text: k,
parent: true,
children: extract(v, nextId),
},
]),
};
});
};
const relationships = (xs: { id: string; children: never[] }[]) => {