-
Notifications
You must be signed in to change notification settings - Fork 2
/
example-07.html
191 lines (165 loc) · 4.42 KB
/
example-07.html
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>JsonDiffComponent: Interactive example</title>
<style>
body {
background: white;
color: black;
}
.deletion {
position: relative;
color: red;
}
.addition {
position: relative;
color: green;
}
.deletion::before,
.addition::before {
display: block;
content: '';
position: absolute;
left: 0;
top: 0;
margin-left: -10px;
}
.deletion::before {
content: '-';
}
.addition::before {
content: '+';
}
.unchanged {
color: gray;
}
.diff,
textarea {
font-family: monospace;
white-space: pre;
border: 1px solid gray;
padding: 10px 10px 10px 20px;
}
textarea {
background: white;
color: black;
padding: 10px;
margin: 0;
}
.json-inputs {
display: flex;
margin-bottom: 10px;
}
.json-inputs > * {
flex: 50%;
height: 450px;
margin-right: 5px;
}
.json-inputs > * + * {
margin-left: 5px;
margin-right: 0;
}
.error {
color: red;
}
</style>
</head>
<body>
<div id="root"></div>
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script crossorigin src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="./json-diff-react.bundle.js"></script>
<script type="text/babel">
// These JSON example was taken from this page https://en.wikipedia.org/wiki/JSON
const wikipediaJsonExample = {
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [
"Catherine",
"Thomas",
"Trevor"
],
"spouse": null
};
const secondJsonExample = {
...wikipediaJsonExample,
"age": 30,
"phoneNumbers": [
{
"type": "cellphone",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [
"Catherine",
"John",
"Trevor"
]
};
const { JsonDiffComponent } = window['json-diff-react'];
const Diff = ({ a, b }) => <JsonDiffComponent
jsonA={a}
jsonB={b}
jsonDiffOptions={{
maxElisions: 2,
renderElision:
(n, max) => (n < max) ? [...Array(n)].map(() => '…') : `… (${n} entries)`
}}
/>
const App = () => {
const [jsonAInput, setJsonAInput] =
React.useState(JSON.stringify(wikipediaJsonExample, null, 2));
const [jsonBInput, setJsonBInput] =
React.useState(JSON.stringify(secondJsonExample, null, 2));
let error: string | null = null
let jsonA = null
try {
jsonA = JSON.parse(jsonAInput);
} catch (e) {
error = `Failed to parse JSON A: ${e?.message ?? JSON.stringify(e)}`;
}
let jsonB = null
try {
jsonB = JSON.parse(jsonBInput);
} catch (e) {
error = `Failed to parse JSON B: ${e?.message ?? JSON.stringify(e)}`;
}
return <>
<h1>JsonDiffComponent: Interactive example</h1>
<div className="json-inputs">
<textarea value={jsonAInput} onChange={e => setJsonAInput(e.target.value)} />
<textarea value={jsonBInput} onChange={e => setJsonBInput(e.target.value)} />
</div>
{error !== null ? <p className="error">{error}</p> : <Diff a={jsonA} b={jsonB} />}
</>;
};
const container = document.getElementById('root');
const root = ReactDOM.createRoot(container);
root.render(<App/>);
</script>
</body>
</html>