-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.js
140 lines (124 loc) · 3.91 KB
/
browser.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
/**
* @file: browser
* @author: Cuttle Cong
* @date: 2017/11/11
* @description:
*/
import React from 'picidae/exports/react';
import {utils} from 'picidae/exports/html-to-react';
const ID = 'transformer-react-doc'
function visit(root, vistor) {
root && root.children && root.children.forEach(function (node, i, children) {
vistor && vistor(node, i, root)
visit(node, vistor)
})
}
/**
* [1, 2, 3] -> 1 | 2 | 3
* [1, [3, 4], 3] -> 1 | [3, 4] | 3
* @param data
*/
function stringify(data, space = 2) {
if (Array.isArray(data)) {
return data.map(v =>
Array.isArray(v)
? '[' + stringify(v, null) + ']'
: stringify(v, null)
).join(' | ').toString()
}
if (typeof data === 'string') {
return data
}
return JSON.stringify(data, null, space)
}
function findParent(root, check) {
var p = root
while (p = p.parent) {
if (check(p)) {
return true
}
}
return false
}
export class DetailView extends React.Component {
state = {
hide: true
}
onMouseEnter = evt => {
this.setState({hide: false})
}
onMouseLeave = evt => {
this.setState({hide: true})
}
render() {
const {hide} = this.state
const {children, content, style, popupStyle} = this.props
return (
<div
className="react-doc-hide"
style={{
position: 'relative'
}}
onMouseEnter={this.onMouseEnter} onMouseLeave={this.onMouseLeave}>
<div
className="react-doc-hide-popover"
style={{
position: 'absolute', backgroundColor: '#fff',
zIndex: 1, padding: 10, border: '1px solid #e5e5e5',
bottom: 2, right: 3, ...popupStyle,
display: hide ? 'none' : ''
}}
>{content}</div>
<div
className="react-doc-hide-children"
style={{
color: '#3795e5',
...style
}}
>
{children}
</div>
</div>
)
}
}
module.exports = function (opt) {
const style = opt.style || {}
const popupStyle = opt.popupStyle || {}
return function (data) {
const {callbackCollect, unmountCallbackCollect} = this
return [
{
replaceChildren: false,
shouldProcessNode(node) {
return node.attribs
&& node.attribs['data-hide-extra']
&& findParent(node, p => p.attribs && p.attribs['class'] === ID)
},
processNode(node, children, index) {
let data = node.attribs['data-hide-extra']
try {
data = JSON.parse(node.attribs['data-hide-extra'])
} catch (e) {}
const stringified = stringify(data)
if (data && stringified) {
children = (
<DetailView
key={index}
style={style}
popupStyle={popupStyle}
content={
<code style={{whiteSpace: 'pre'}}>{stringified}</code>
}
>
{children}
</DetailView>
)
}
// return null; //React.createElement(node.name, {}, children)
return utils.createElement(node, index, node.data, children)
}
}
]
}
}