Skip to content

Commit 369439e

Browse files
update json tree view
1 parent bf2dfee commit 369439e

4 files changed

Lines changed: 97 additions & 7 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
export * from "./json-visualization";
2+
export * from "./node-visualization";
13
export * as visualize_node from "./node-visualization";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./json-tree";

editor-packages/editor-devtools/components/visualization/json-visualization/json-tree.tsx

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import { Figma } from "@design-sdk/figma";
44
import { Widget as ReflectWidget } from "@reflect-ui/core";
55
import React from "react";
66
import JSONTree from "react-json-tree";
7+
import type { Theme } from "react-base16-styling";
8+
import assert from "assert";
79

810
interface CompactNodeTree {
911
id: string;
1012
name: string;
1113
children?: CompactNodeTree[];
1214
}
1315

14-
const theme = {
16+
const monokai: Theme = {
1517
scheme: "monokai",
1618
author: "wimer hazenberg (http://www.monokai.nl)",
1719
base00: "#272822",
@@ -30,15 +32,79 @@ const theme = {
3032
base0D: "#66d9ef",
3133
base0E: "#ae81ff",
3234
base0F: "#cc6633",
33-
"background-color": "transparent",
3435
};
3536

36-
export function JsonTree(props: { data: any; hideRoot?: boolean }) {
37+
export function JsonTree({
38+
data,
39+
hideRoot,
40+
expandRoot = false,
41+
expandParent = false,
42+
theme = monokai,
43+
backgroundColor,
44+
sortkeys = false,
45+
omitkeys = [],
46+
}: {
47+
data: any;
48+
hideRoot?: boolean;
49+
expandRoot?: boolean;
50+
expandParent?: boolean;
51+
theme?: Theme;
52+
backgroundColor?: React.CSSProperties["backgroundColor"];
53+
sortkeys?: ReadonlyArray<string> | boolean;
54+
// not used
55+
omitkeys?: ReadonlyArray<string>;
56+
}) {
57+
const sorter = (a: string, b: string) => {
58+
assert(sortkeys instanceof Array, "keysort must be an array");
59+
const aindex = sortkeys.indexOf(a);
60+
const bindex = sortkeys.indexOf(b);
61+
// the sortkeys may not contain all keys.
62+
63+
// if a is not in sortkeys, it should be placed after b
64+
if (aindex === -1) {
65+
return 1;
66+
}
67+
68+
// if b is not in sortkeys, it should be placed after a
69+
if (bindex === -1) {
70+
return -1;
71+
}
72+
73+
// if both are not in sortkeys, they should be placed in the order of appearance
74+
if (aindex === -1 && bindex === -1) {
75+
return 0;
76+
}
77+
78+
// if both are in sortkeys, they should be placed in the order of sortkeys
79+
return aindex - bindex;
80+
};
81+
3782
return (
3883
<JSONTree
39-
data={props.data}
40-
theme={theme}
41-
hideRoot={props.hideRoot}
84+
data={data}
85+
theme={{
86+
...(theme as object),
87+
...(backgroundColor ? { base00: backgroundColor } : {}),
88+
tree: ({ style }) => ({
89+
style: {
90+
...style,
91+
fontFamily: "Monaco, monospace",
92+
fontSize: 14,
93+
},
94+
}),
95+
}}
96+
invertTheme={false}
97+
hideRoot={hideRoot}
98+
sortObjectKeys={typeof sortkeys === "boolean" ? sortkeys : sorter}
99+
shouldExpandNode={(keypath, data, level) => {
100+
if (level === 0) {
101+
return expandRoot;
102+
}
103+
if (keypath[keypath.length - 1] === "parent") {
104+
return expandParent;
105+
}
106+
return true;
107+
}}
42108
getItemString={(type, data, itemType, itemString) => {
43109
return (
44110
<span>
@@ -85,7 +151,7 @@ export function WidgetTree(props: {
85151
return (
86152
<JSONTree
87153
data={props.data}
88-
theme={theme}
154+
theme={monokai}
89155
hideRoot={props.hideRoot}
90156
getItemString={(type, data: WidgetDataLike, itemType, itemString) => {
91157
return (
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2015",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"strict": false,
8+
"forceConsistentCasingInFileNames": true,
9+
"noEmit": true,
10+
"esModuleInterop": true,
11+
"module": "esnext",
12+
"moduleResolution": "node",
13+
"resolveJsonModule": true,
14+
"isolatedModules": true,
15+
"jsx": "preserve",
16+
"incremental": true,
17+
"baseUrl": "."
18+
},
19+
"include": ["**/*.ts", "**/*.tsx"],
20+
"exclude": ["node_modules"]
21+
}

0 commit comments

Comments
 (0)