-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathInspector.js
154 lines (128 loc) · 2.73 KB
/
Inspector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*eslint-disable */
import React from 'react'
import ViewerStore from './debug/ViewerStore'
import connectToStores from './connectToStores'
const Styles = {
root: {
font: '14px/1.4 Consolas, monospace',
},
line: {
cursor: 'pointer',
paddingLeft: '1em',
},
key: {
color: '#656865',
},
string: {
color: '#87af5f',
cursor: 'text',
marginLeft: '0.1em',
},
boolean: {
color: '#f55e5f',
cursor: 'text',
marginLeft: '0.1em',
},
number: {
color: '#57b3df',
cursor: 'text',
marginLeft: '0.1em',
},
helper: {
color: '#b0b0b0',
marginLeft: '0.1em',
},
}
class Leaf extends React.Component {
constructor(props) {
super(props)
this.state = {
hidden: this.props.hidden
}
this.toggle = this._toggle.bind(this)
}
renderValue() {
if (typeof this.props.data === 'object' && this.props.data) {
if (this.state.hidden) {
return null
}
return Object.keys(this.props.data).map((node, i) => {
return (
<Leaf
key={i}
label={node}
data={this.props.data[node]}
level={this.props.level + 1}
hidden={this.props.level > 0}
/>
)
})
} else {
const jstype = typeof this.props.data
return <span style={Styles[jstype]}>{String(this.props.data)}</span>
}
}
renderPluralCount(n) {
return n === 0
? ''
: n === 1 ? '1 item' : `${n} items`
}
renderLabel() {
const label = this.props.label || 'dispatch'
const jstype = typeof this.props.data
const type = jstype !== 'object'
? ''
: Array.isArray(this.props.data) ? '[]' : '{}'
const length = jstype === 'object' && this.props.data != null
? Object.keys(this.props.data).length
: 0
return (
<span>
<span style={Styles.key}>
{label}:
</span>
<span style={Styles.helper}>
{type}
{' '}
{this.renderPluralCount(length)}
</span>
</span>
)
}
_toggle() {
this.setState({
hidden: !this.state.hidden
})
}
render() {
return (
<div style={Styles.line}>
<span onClick={this.toggle}>
{this.renderLabel()}
</span>
{this.renderValue()}
</div>
)
}
}
Leaf.defaultProps = { hidden: true }
class Inspector extends React.Component {
constructor() {
super()
}
render() {
return (
<div styles={Styles.root}>
<Leaf data={this.props.selectedData} hidden={false} level={0} />
</div>
)
}
}
export default connectToStores({
getPropsFromStores() {
return ViewerStore.getState()
},
getStores() {
return [ViewerStore]
}
}, Inspector)