@@ -4,14 +4,16 @@ import { Figma } from "@design-sdk/figma";
44import { Widget as ReflectWidget } from "@reflect-ui/core" ;
55import React from "react" ;
66import JSONTree from "react-json-tree" ;
7+ import type { Theme } from "react-base16-styling" ;
8+ import assert from "assert" ;
79
810interface 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 (
0 commit comments