-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonEdit.tsx
227 lines (221 loc) · 8.95 KB
/
jsonEdit.tsx
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { diff } from "json-diff";
import { NextPage } from "next";
import React, { ReactElement, useState } from "react";
import { IAceOptions } from "react-ace";
import { Editor } from "../components/index";
import BackUp from "../layouts/BackUp";
import { LayoutType } from "../types";
interface InputCodeProps {
onChange: (val: string) => void;
value: string;
options?: IAceOptions;
}
// 格式化json
const formatCode = (str: string) => {
try {
return JSON.stringify(JSON.parse(str), null, 4);
} catch (error) {
console.error(error);
return str;
}
};
// 输入组件
const InputCode = (props: InputCodeProps) => {
const { onChange, value, options } = props;
return (
<div className="mockup-code h-full text-center bg-[#1d1f21]">
<Editor
options={options}
value={value}
onChange={onChange}
placeholder="请输入JSON字符串"
/>
</div>
);
};
interface DiffOptionProps {
// Compare only the keys, ignore the differences in values
onlyKey?: boolean;
// Display raw JSON encoding of the diff
rawJson?: boolean;
}
const JsonEdit: NextPage<any> & LayoutType = () => {
// json
const [one, setOne] = useState("");
// json
const [two, setTwo] = useState("");
// 输出不同
const [diffRes, setDiffRes] = useState<string>();
// ace配置
const [editorOptions, setEditorOptions] = useState<IAceOptions>({
showLineNumbers: true,
showGutter: true,
useWorker: false,
});
const [diffOptions, setDiffOptions] = useState<DiffOptionProps>({
onlyKey: false,
rawJson: true,
});
return (
<div className="flex bg-white">
<div className="h-screen w-1/12 text-center flex flex-col items-center shadow-2xl">
<button
className="btn w-4/5 mt-5"
onClick={() => {
if (!one.trim().length || !two.trim().length) {
alert("JSON不能为空");
return;
}
try {
const _one = JSON.parse(one);
const _two = JSON.parse(two);
const diffRes = diff(_one, _two, {
keysOnly: diffOptions.onlyKey,
full: true,
// raw: true,
});
setDiffRes(diffRes);
} catch (error) {
console.log(error);
alert("检查json格式");
}
}}>
diff
</button>
<button
className="btn w-4/5 mt-5"
onClick={() => {
one && setOne(formatCode(one));
two && setTwo(formatCode(two));
}}>
format
</button>
<button
className="btn w-4/5 mt-5"
onClick={() => {
setOne("");
setTwo("");
}}>
clear
</button>
<div className="dropdown dropdown-right ">
<label tabIndex={0} className="btn m-1 w-4/5 mt-5">
Editor Setting
</label>
<ul
tabIndex={0}
className="dropdown-content menu p-2 shadow bg-base-100 rounded-box w-52">
<li>
<div className="form-control">
<label className="label cursor-pointer">
<span className="label-text mr-1">
Line Num
</span>
<input
type="checkbox"
checked={editorOptions.showLineNumbers}
className="checkbox"
onChange={() => {
setEditorOptions((val) => {
return {
...val,
showLineNumbers:
!val.showLineNumbers,
};
});
}}
/>
</label>
</div>
</li>
<li>
<div className="form-control">
<label className="label cursor-pointer">
<span className="label-text mr-1">
Show Gutter
</span>
<input
type="checkbox"
checked={editorOptions.showGutter}
className="checkbox"
onChange={(val) => {
setEditorOptions((val) => {
return {
...val,
showGutter: !val.showGutter,
};
});
}}
/>
</label>
</div>
</li>
</ul>
</div>
<div className="dropdown dropdown-right ">
<label tabIndex={0} className="btn m-1 w-4/5 mt-5">
Diff Setting
</label>
<ul
tabIndex={0}
className="dropdown-content menu p-2 shadow bg-base-100 rounded-box w-52">
<li>
<div className="form-control">
<label className="label cursor-pointer">
<span className="label-text mr-1">
Compare Key
</span>
<input
type="checkbox"
checked={diffOptions.onlyKey}
className="checkbox"
onClick={() => {
setDiffOptions((val) => {
return {
...val,
onlyKey: !val.onlyKey,
};
});
}}
/>
</label>
</div>
</li>
</ul>
</div>
</div>
<div className="grid grid-rows-6 grid-flow-col gap-5 h-screen w-screen p-4 relative">
<div className="row-span-3 col-span-1">
<InputCode
options={editorOptions}
value={one}
onChange={(val) => {
setOne(val.trim());
}}
/>
</div>
<div className="row-span-3 col-span-1 text-center">
<InputCode
options={editorOptions}
value={two}
onChange={(val) => {
setTwo(val.trim());
}}
/>
</div>
<div className="row-span-6 col-span-1">
<div className="mockup-code h-full w-full break-all bg-[#1d1f21]">
<Editor
value={formatCode(JSON.stringify(diffRes))}
options={editorOptions}
/>
</div>
</div>
</div>
</div>
);
};
export default JsonEdit;
JsonEdit.getLayout = (page: ReactElement) => {
return <BackUp>{page}</BackUp>;
};