mirror of
https://github.com/a-nyx/maputnik-with-pmtiles.git
synced 2024-12-31 00:20:32 +01:00
Moved away from react-codemirror2 to interacting with codemirror directly.
This commit is contained in:
parent
f088788246
commit
563a78ed42
1 changed files with 69 additions and 28 deletions
|
@ -1,9 +1,9 @@
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
|
|
||||||
import {Controlled as CodeMirror} from 'react-codemirror2'
|
|
||||||
import InputBlock from '../inputs/InputBlock'
|
import InputBlock from '../inputs/InputBlock'
|
||||||
import StringInput from '../inputs/StringInput'
|
import StringInput from '../inputs/StringInput'
|
||||||
|
import CodeMirror from 'codemirror';
|
||||||
|
|
||||||
import 'codemirror/mode/javascript/javascript'
|
import 'codemirror/mode/javascript/javascript'
|
||||||
import 'codemirror/addon/lint/lint'
|
import 'codemirror/addon/lint/lint'
|
||||||
|
@ -25,36 +25,82 @@ class JSONEditor extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props)
|
super(props)
|
||||||
this.state = {
|
this.state = {
|
||||||
code: JSON.stringify(props.layer, null, 2)
|
isEditing: false,
|
||||||
}
|
prevValue: this.getValue(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
getValue () {
|
||||||
|
return JSON.stringify(this.props.layer, null, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount () {
|
||||||
|
this._doc = CodeMirror(this._el, {
|
||||||
|
value: this.getValue(),
|
||||||
|
mode: {
|
||||||
|
name: "javascript",
|
||||||
|
json: true
|
||||||
|
},
|
||||||
|
tabSize: 2,
|
||||||
|
theme: 'maputnik',
|
||||||
|
viewportMargin: Infinity,
|
||||||
|
lineNumbers: true,
|
||||||
|
lint: true,
|
||||||
|
gutters: ["CodeMirror-lint-markers"],
|
||||||
|
scrollbarStyle: "null",
|
||||||
|
});
|
||||||
|
|
||||||
|
this._doc.on('change', this.onChange);
|
||||||
|
this._doc.on('focus', this.onFocus);
|
||||||
|
this._doc.on('blur', this.onBlur);
|
||||||
|
}
|
||||||
|
|
||||||
|
onFocus = () => {
|
||||||
|
this.setState({
|
||||||
|
isEditing: true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
onBlur = () => {
|
||||||
|
this.setState({
|
||||||
|
isEditing: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillUnMount () {
|
||||||
|
this._doc.off('change', this.onChange);
|
||||||
|
this._doc.off('focus', this.onFocus);
|
||||||
|
this._doc.off('blur', this.onBlur);
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate(prevProps) {
|
componentDidUpdate(prevProps) {
|
||||||
if (prevProps.layer !== this.props.layer) {
|
if (!this.state.isEditing && prevProps.layer !== this.props.layer) {
|
||||||
this.setState({
|
this._cancelNextChange = true;
|
||||||
code: JSON.stringify(this.props.layer, null, 2)
|
this._doc.setValue(
|
||||||
})
|
this.getValue(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onCodeUpdate(newCode) {
|
onChange = (e) => {
|
||||||
try {
|
if (this._cancelNextChange) {
|
||||||
const parsedLayer = JSON.parse(newCode)
|
this._cancelNextChange = false;
|
||||||
this.props.onChange(parsedLayer)
|
return;
|
||||||
} catch(err) {
|
}
|
||||||
console.warn(err)
|
const newCode = this._doc.getValue();
|
||||||
} finally {
|
|
||||||
this.setState({
|
if (this.state.prevValue !== newCode) {
|
||||||
code: newCode
|
try {
|
||||||
})
|
const parsedLayer = JSON.parse(newCode)
|
||||||
|
this.props.onChange(parsedLayer)
|
||||||
|
} catch(err) {
|
||||||
|
console.warn(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
resetValue() {
|
|
||||||
console.log('reset')
|
|
||||||
this.setState({
|
this.setState({
|
||||||
code: JSON.stringify(this.props.layer, null, 2)
|
prevValue: newCode,
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
@ -69,12 +115,7 @@ class JSONEditor extends React.Component {
|
||||||
scrollbarStyle: "null",
|
scrollbarStyle: "null",
|
||||||
}
|
}
|
||||||
|
|
||||||
return <CodeMirror
|
return <div ref={(el) => this._el = el} />
|
||||||
value={this.state.code}
|
|
||||||
onBeforeChange={(editor, data, value) => this.onCodeUpdate(value)}
|
|
||||||
onFocusChange={focused => focused ? true : this.resetValue()}
|
|
||||||
options={codeMirrorOptions}
|
|
||||||
/>
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue