forked from vankop/jsoneditor-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Editor.jsx
256 lines (227 loc) · 7.2 KB
/
Editor.jsx
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import JSONEditor from 'jsoneditor/dist/jsoneditor-minimalist';
import 'jsoneditor/dist/jsoneditor.css';
import './fixAce.css';
/**
* @typedef {{
* tree: string,
* view: string,
* form: string,
* code: string,
* text: string,
* allValues: Array<string>
* }} TJsonEditorModes
*/
const modes = {
tree: 'tree',
view: 'view',
form: 'form',
code: 'code',
text: 'text'
};
const values = Object.values(modes);
modes.allValues = values;
/**
* @type {object}
* @property {object} [value]
* @property {string} [mode='tree'] - Set the editor mode.
* @property {string} [name=undefined] - Initial field name for the root node
* @property {object} [schema] - Validate the JSON object against a JSON schema.
* @property {object} [schemaRefs] - Schemas that are referenced using
* the $ref property
* @property {Function} [onChange] - Set a callback function
* triggered when the contents of the JSONEditor change.
* Called without parameters. Will only be triggered on changes made by the user.
* Return new json.
* @property {Function} [onError] - Set a callback function triggered when an error occurs.
* Invoked with the error as first argument.
* The callback is only invoked for errors triggered by a users action,
* like switching from code mode to tree mode or clicking
* the Format button whilst the editor doesn't contain valid JSON.
* @property {Function} [onModeChange] - Set a callback function
* triggered right after the mode is changed by the user.
* @property {object} [ace] - Provide a version of the Ace editor.
* Only applicable when mode is code
* @property {object} [ajv] - Provide a instance of ajv,
* the library used for JSON schema validation.
* @property {string} [theme] - Set the Ace editor theme,
* uses included 'ace/theme/jsoneditor' by default.
* @property {boolean} [history=false] - Enables history,
* adds a button Undo and Redo to the menu of the JSONEditor. Only applicable when
* mode is 'tree' or 'form'
* @property {boolean} [navigationBar=true] - Adds navigation bar to the menu
* the navigation bar visualize the current position on the
* tree structure as well as allows breadcrumbs navigation.
* @property {boolean} [statusBar=true] - Adds status bar to the buttom of the editor
* the status bar shows the cursor position and a count of the selected characters.
* Only applicable when mode is 'code' or 'text'.
* @property {boolean} [search=true] - Enables a search box in
* the upper right corner of the JSONEditor.
* @property {Array<string>} [allowedModes] - Create a box in the editor menu where
* the user can switch between the specified modes.
* @property {string} [tag='div'] - Html element to render
* @property {object} [htmlElementProps] - html element custom props
* @property {Function} [innerRef] - callback to get html element reference
*/
export default class Editor extends Component {
static propTypes = {
// jsoneditor props
value: PropTypes.object,
mode: PropTypes.oneOf(values),
name: PropTypes.string,
schema: PropTypes.object,
schemaRefs: PropTypes.object,
onChange: PropTypes.func,
onError: PropTypes.func,
onModeChange: PropTypes.func,
ace: PropTypes.object,
ajv: PropTypes.object,
theme: PropTypes.string,
history: PropTypes.bool,
navigationBar: PropTypes.bool,
statusBar: PropTypes.bool,
search: PropTypes.bool,
allowedModes: PropTypes.arrayOf(PropTypes.oneOf(values)),
// custom props
tag: PropTypes.string,
htmlElementProps: PropTypes.object,
innerRef: PropTypes.func
};
static defaultProps = {
tag: 'div',
mode: modes.tree,
history: false,
search: true,
navigationBar: true,
statusBar: true
};
/**
* @type TJsonEditorModes
*/
static modes = modes;
constructor(props) {
super(props);
this.htmlElementRef = null;
this.jsonEditor = null;
this.handleChange = this.handleChange.bind(this);
this.setRef = this.setRef.bind(this);
this.collapseAll = this.collapseAll.bind(this);
this.expandAll = this.expandAll.bind(this);
this.focus = this.focus.bind(this);
}
componentDidMount() {
const {
allowedModes,
innerRef,
htmlElementProps,
tag,
onChange,
...rest
} = this.props;
this.createEditor({
...rest,
modes: allowedModes
});
}
componentWillReceiveProps({
allowedModes,
schema,
name,
theme,
schemaRefs,
innerRef,
htmlElementProps,
tag,
onChange,
...rest
}) {
if (this.jsonEditor) {
if (theme !== this.props.theme) {
this.createEditor({
...rest,
theme,
modes: allowedModes
});
} else {
if (schema !== this.props.schema
|| schemaRefs !== this.props.schemaRefs
) {
this.jsonEditor.setSchema(schema, schemaRefs);
}
if (name !== this.jsonEditor.getName()) {
this.jsonEditor.setName(name);
}
}
}
}
shouldComponentUpdate({ htmlElementProps }) {
return htmlElementProps !== this.props.htmlElementProps;
}
componentWillUnmount() {
if (this.jsonEditor) {
this.jsonEditor.destroy();
this.jsonEditor = null;
}
}
setRef(element) {
this.htmlElementRef = element;
if (this.props.innerRef) {
this.props.innerRef(element);
}
}
createEditor({ value, ...rest }) {
if (this.jsonEditor) {
this.jsonEditor.destroy();
}
this.jsonEditor = new JSONEditor(this.htmlElementRef, {
onChange: this.handleChange,
...rest
});
this.jsonEditor.set(value);
}
handleChange() {
if (this.props.onChange) {
try {
const text = this.jsonEditor.getText();
if (text === '') {
this.props.onChange(null);
}
const currentJson = this.jsonEditor.get();
if (this.props.value !== currentJson) {
this.props.onChange(currentJson);
}
} catch (err) {
this.err = err;
}
}
}
collapseAll() {
if (this.jsonEditor) {
this.jsonEditor.collapseAll();
}
}
expandAll() {
if (this.jsonEditor) {
this.jsonEditor.expandAll();
}
}
focus() {
if (this.jsonEditor) {
this.jsonEditor.focus();
}
}
render() {
const {
htmlElementProps,
tag
} = this.props;
return React.createElement(
tag,
{
...htmlElementProps,
ref: this.setRef
}
);
}
}