Skip to content

Commit e32b5b3

Browse files
committed
make it colorful
1 parent 767c103 commit e32b5b3

14 files changed

+191
-87
lines changed

.npmignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
/build/
3+
/data/
4+
/images/
5+
/src/
6+
/webpack.*
7+
/gulpfile.*
8+
/index.html
9+
/template.*

README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
1+
2+
React Lite JSON Viewer
13
----
2-
### Usage
34

4-
### Develop
5+
Demo http://ui.talk.ai/react-lite-json-viewer/
56

6-
```text
7-
npm i
8-
```
7+
Component for exploring Store in JSON.
98

10-
You need a static file server for the HTML files. Personally I suggest using Nginx.
11-
12-
Develop:
9+
### Usage
1310

1411
```bash
15-
gulp html # regenerate index.html
16-
webpack-dev-server --hot # enable live-reloading
12+
npm i --save react-lite-json-viewer
1713
```
1814

19-
Build (Pack and optimize js, reivision js and add entry in `index.html`):
15+
```coffee
16+
LiteJSONViewer = require 'react-lite-json-viewer'
2017

21-
```bash
22-
gulp build
18+
LiteJSONViewer
19+
height: 500
20+
data: Immutable.fromJS(jianliaoData)
21+
path: @state.path # Immutable.List()
22+
onChange: @onPathChange
2323
```
2424

25+
Read more in `src/`.
26+
27+
### Develop
28+
29+
https://github.com/teambition/coffee-webpack-starter
30+
31+
### Background
32+
33+
http://fotonin.com/data_images/out/15/892020-space-wallpaper-hd.jpg
34+
2535
### License
2636

2737
MIT

gulpfile.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ gulp.task 'script', ->
1111
coffee = require('gulp-coffee')
1212
gulp
1313
.src('src/**/*.coffee')
14-
.pipe coffee()
14+
.pipe coffee(bare: true)
1515
.pipe gulp.dest('lib/')
1616

1717
gulp.task 'rsync', (cb) ->
@@ -21,7 +21,7 @@ gulp.task 'rsync', (cb) ->
2121
src: ['index.html', 'build', 'src']
2222
recursive: true
2323
args: ['--verbose']
24-
dest: 'talk-ui:/teambition/server/talk-ui/json-viewer'
24+
dest: 'talk-ui:/teambition/server/talk-ui/react-lite-json-viewer'
2525
deleteAll: true
2626
, (error, stdout, stderr, cmd) ->
2727
if error?

images/space.jpg

602 KB
Loading

package.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
2-
"name": "coffee-webpack-starter",
2+
"name": "react-lite-json-viewer",
33
"version": "0.0.1",
4-
"description": "Component starter kit for Talk by Teambition",
5-
"main": "lib/index.js",
4+
"description": "Component for exploring store in JSON",
5+
"main": "lib/json-viewer.js",
66
"dependencies": {
7-
"classnames": "^1.2.0",
7+
"hsl": "^0.1.1",
88
"immutable": "^3.7.5",
9-
"react": "^0.13.1"
9+
"object-assign": "^4.0.1",
10+
"react": ">=0.13.1"
1011
},
1112
"devDependencies": {
1213
"autoprefixer-loader": "^2.0.0",
@@ -21,13 +22,13 @@
2122
"json-loader": "^0.5.3",
2223
"less": "^2.5.1",
2324
"less-loader": "^2.2.0",
25+
"react-dom": "^0.14.0",
2426
"react-hot-loader": "^1.3.0",
2527
"rsyncwrapper": "^0.4.3",
2628
"run-sequence": "^1.0.2",
2729
"stir-template": "^0.1.4",
2830
"style-loader": "^0.10.2",
2931
"url-loader": "^0.5.6",
30-
"volubile-ui": "0.0.10",
3132
"webpack": "^1.8.4",
3233
"webpack-dev-server": "^1.8.0"
3334
},
@@ -41,10 +42,10 @@
4142
"license": "MIT",
4243
"repository": {
4344
"type": "git",
44-
"url": "https://github.com/teambition/coffee-webpack-starter.git"
45+
"url": "https://github.com/teambition/react-lite-json-viewer.git"
4546
},
4647
"bugs": {
47-
"url": "https://github.com/teambition/coffee-webpack-starter/issues"
48+
"url": "https://github.com/teambition/react-lite-json-viewer/issues"
4849
},
49-
"homepage": "https://github.com/teambition/coffee-webpack-starter"
50+
"homepage": "https://github.com/teambition/react-lite-json-viewer"
5051
}

src/app/value.coffee

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
hsl = require 'hsl'
3+
React = require 'react'
4+
assign = require 'object-assign'
5+
Immutable = require 'immutable'
6+
7+
reset = require '../util/reset'
8+
9+
{div, span} = React.DOM
10+
11+
stringify = JSON.stringify
12+
13+
module.exports = React.createClass
14+
displayName: 'view-value'
15+
16+
propTypes:
17+
value: React.PropTypes.any
18+
19+
renderString: ->
20+
span style: @styleString(), stringify(@props.value)
21+
22+
renderNumber: ->
23+
span style: @styleNumber(), @props.value
24+
25+
renderBoolean: ->
26+
span style: @styleBoolean(), stringify(@props.value)
27+
28+
renderUndefined: ->
29+
span style: @styleUndefined(), 'undefined'
30+
31+
renderNull: ->
32+
span style: @styleNull(), 'null'
33+
34+
renderMap: ->
35+
span style: @styleMap(), "Map #{@props.value.size}"
36+
37+
renderList: ->
38+
span style: @styleList(), "List #{@props.value.size}"
39+
40+
render: ->
41+
value = @props.value
42+
type = typeof value
43+
44+
switch
45+
when type is 'string' then @renderString()
46+
when type is 'number' then @renderNumber()
47+
when type is 'boolean' then @renderBoolean()
48+
when value is undefined then @renderUndefined()
49+
when value is null then @renderNull()
50+
when Immutable.Map.isMap value then @renderMap()
51+
when Immutable.List.isList value then @renderList()
52+
else @renderUnknown()
53+
54+
styleBase: (more) ->
55+
baseStyle =
56+
lineHeight: '24px'
57+
height: '24px'
58+
fontSize: '12px'
59+
padding: '0 10px'
60+
fontFamily: reset.codeFonts
61+
maxWidth: 300
62+
overflowX: 'auto'
63+
whiteSpace: 'nowrap'
64+
color: hsl 0, 0, 90
65+
assign more, baseStyle
66+
67+
styleString: ->
68+
@styleBase backgroundColor: hsl 0, 0, 20, 0.5
69+
70+
styleNumber: ->
71+
@styleBase backgroundColor: hsl 240, 30, 40, 0.5
72+
73+
styleBoolean: ->
74+
@styleBase backgroundColor: hsl 240, 50, 30, 0.5
75+
76+
styleNull: ->
77+
@styleBase backgroundColor: hsl 240, 90, 40, 0.5
78+
79+
styleUndefined: ->
80+
@styleBase backgroundColor: hsl 240, 90, 40, 0.5
81+
82+
styleMap: ->
83+
@styleBase backgroundColor: hsl 200, 70, 60, 0.5
84+
85+
styleList: ->
86+
@styleBase backgroundColor: hsl 40, 70, 70, 0.5

src/json-viewer.coffee

Lines changed: 33 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,38 @@
1+
2+
hsl = require 'hsl'
13
React = require 'react'
24
Immutable = require 'immutable'
5+
6+
reset = require './util/reset'
37
isSafari = require './util/is-safari'
48

5-
{div, span, pre} = React.DOM
9+
Value = React.createFactory require './app/value'
610

7-
typeColor =
8-
list: 'rgba(245, 222, 179, .8)'
9-
map: 'rgba(223, 245, 179, .8)'
11+
{div} = React.DOM
1012

1113
T = React.PropTypes
1214

1315
module.exports = React.createClass
14-
displayName: 'json-viewer'
16+
displayName: 'lite-json-viewer'
1517

1618
propTypes:
17-
data: T.any
18-
height: T.number
19-
path: T.instanceOf(Immutable.List)
20-
onChange: T.func
19+
data: T.instanceOf(Immutable.Collection).isRequired
20+
height: T.number.isRequired
21+
path: T.instanceOf(Immutable.List).isRequired
22+
onChange: T.func.isRequired
2123

22-
filterType: (value) ->
23-
if value instanceof Immutable.Map
24-
'map'
25-
else if value instanceof Immutable.List
26-
'list'
27-
else
28-
JSON.stringify value
29-
30-
renderKeys: (stage) ->
24+
renderEntries: (stage, index) ->
3125
value = @props.data.getIn(stage)
3226
if value instanceof Immutable.Collection
33-
div style: @styleBlock(),
27+
div style: @styleBlock(), key: index,
3428
value.keySeq().sort().map (entry, index) =>
3529
onClick = => @props.onChange stage.concat(entry)
3630
isActive = @props.path.get(stage.size) is entry
37-
div key: index, style: @styleEntries(isActive), onClick: onClick,
38-
span style: @styleKey(), entry
39-
span style: @styleValue(stage.concat(entry)), @filterType value.get(entry)
31+
div key: index, style: @styleEntry(isActive), onClick: onClick,
32+
Value value: entry
33+
Value value: value.get(entry)
34+
else
35+
Value value: value
4036

4137
render: ->
4238
div style: @styleRoot(),
@@ -46,11 +42,11 @@ module.exports = React.createClass
4642
@props.path[..index]
4743
.unshift Immutable.List()
4844
div style: @styleTable(),
49-
pathList.map (path) =>
50-
@renderKeys path
45+
pathList.map (path, index) =>
46+
@renderEntries path, index
5147
else
52-
pre style: @styleBlock(),
53-
if @props.data? then @props.data else 'null'
48+
div style: @styleBlock(),
49+
Value value: @props.data
5450

5551
styleRoot: ->
5652
flex: 1
@@ -64,40 +60,23 @@ module.exports = React.createClass
6460
WebkitFlex: 1
6561
height: @props.height
6662

67-
styleEntries: (isActive) ->
63+
styleEntry: (isActive) ->
6864
display: if isSafari then '-webkit-flex' else 'flex'
6965
flexDirection: 'row'
7066
WebkitFlexDirection: 'row'
67+
alignItems: 'center'
68+
WebkitAlignItems: 'center'
7169
width: 'auto'
72-
overflowY: 'auto'
73-
overflowX: 'visible'
74-
padding: '10px'
75-
color: if isActive then '#D45502' else '#FFF'
70+
padding: '4px 0px'
71+
backgroundColor: if isActive then hsl(80,80,90,0.1)
72+
color: 'white'
7673
cursor: 'pointer'
7774

78-
styleKey: ->
79-
flex: 1
80-
WebkitFlex: 1
81-
padding: '0 10px'
82-
lineHeight: '24px'
83-
fontSize: '12px'
84-
display: 'inline-block'
85-
marginRight: '10px'
86-
marginBottom: '6px'
87-
88-
styleValue: (path) ->
89-
flex: '0 0 auto'
90-
WebkitFlex: '0 0 auto'
91-
margin: 0
92-
overflowY: 'auto'
93-
fontSize: '12px'
94-
padding: '0 10px'
95-
fontFamily: 'Menlo, Consolas, Ubuntu Mono, monospace'
96-
backgroundColor: typeColor[@filterType(@props.data.getIn(path))]
97-
9875
styleBlock: ->
99-
fontFamily: 'Menlo, Consolas, Ubuntu Mono, monospace'
76+
fontFamily: reset.codeFonts
10077
overflowY: 'scroll'
101-
backgroundColor: 'rgba(245, 245, 245, 0.2)'
102-
minWidth: 200
78+
backgroundColor: hsl 0, 0, 100, 0.1
79+
minWidth: 100
10380
marginRight: 10
81+
paddingTop: 100
82+
paddingBottom: 200

src/main.coffee

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11

22
React = require 'react'
3-
4-
require('volubile-ui/ui/index.less')
3+
ReactDOM = require 'react-dom'
54

65
Page = React.createFactory require './page'
76

8-
React.render Page(), document.querySelector('.demo')
7+
ReactDOM.render Page(), document.querySelector('.demo')

src/main.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
body {
33
padding: 100px;
4+
background-image: url('../images/space.jpg');
45
}
56

67
.demo {

src/page.coffee

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11

2+
hsl = require 'hsl'
23
React = require 'react'
34
Immutable = require 'immutable'
45

6+
reset = require './util/reset'
57
jianliao = require '../data/jianliao.json'
68

79
JsonViewer = React.createFactory require './json-viewer'
8-
div = React.createFactory 'div'
10+
{div, span, a} = React.DOM
11+
12+
link = 'https://github.com/teambition/react-lite-json-viewer'
913

1014
module.exports = React.createClass
1115
displayName: 'app-page'
@@ -18,8 +22,20 @@ module.exports = React.createClass
1822

1923
render: ->
2024
div className: 'app-page',
25+
div style: @styleNote(),
26+
span null, 'JSON view in React, try clicks! Found more on '
27+
a style: @styleLink(), href: link, 'GitHub'
2128
JsonViewer
2229
height: 500
2330
data: Immutable.fromJS(jianliao)
2431
path: @state.path
2532
onChange: @onPathChange
33+
34+
styleNote: ->
35+
color: 'white'
36+
fontFamily: reset.contentFonts
37+
marginBottom: 40
38+
fontSize: '14px'
39+
40+
styleLink: ->
41+
color: hsl 240, 80, 80

0 commit comments

Comments
 (0)