| 1 |
import React, { Component } from 'react'; |
| 2 |
import CodeMirror from '@uiw/react-codemirror'; |
| 3 |
import * as events from '@uiw/codemirror-extensions-events'; |
| 4 |
import { css } from '@codemirror/lang-css'; |
| 5 |
|
| 6 |
export default class SettingCode extends Component { |
| 7 |
|
| 8 |
constructor(props) { |
| 9 |
super(props); |
| 10 |
|
| 11 |
this.state = { |
| 12 |
initialValue: props.value, |
| 13 |
value: props.value, |
| 14 |
indicatedChange: false, |
| 15 |
}; |
| 16 |
|
| 17 |
this.onChange = this.onChange.bind(this); |
| 18 |
this.updateParent = this.updateParent.bind(this); |
| 19 |
} |
| 20 |
|
| 21 |
shouldComponentUpdate(nextProps, nextState) { |
| 22 |
return nextProps.value !== this.props.value |
| 23 |
|| nextState.value !== this.state.value |
| 24 |
|| nextState.indicatedChange !== this.state.indicatedChange |
| 25 |
|| nextState.initialValue !== this.state.initialValue; |
| 26 |
} |
| 27 |
|
| 28 |
componentDidUpdate(prevProps) { |
| 29 |
if ( prevProps.value !== this.props.value && this.props.value !== this.state.value ) { |
| 30 |
this.setState({ |
| 31 |
initialValue: this.props.value, |
| 32 |
value: this.props.value, |
| 33 |
indicatedChange: false, |
| 34 |
}); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
onChange( value ) { |
| 39 |
let newState = { |
| 40 |
value: value, |
| 41 |
}; |
| 42 |
|
| 43 |
let updateParent = false; |
| 44 |
if ( value !== this.state.initialValue ) { |
| 45 |
updateParent = true; |
| 46 |
newState.indicatedChange = true; |
| 47 |
} else { |
| 48 |
if ( this.state.indicatedChange ) { |
| 49 |
updateParent = true; |
| 50 |
newState.indicatedChange = false; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
this.setState( newState, () => { |
| 55 |
if ( updateParent ) { |
| 56 |
this.updateParent(); |
| 57 |
} |
| 58 |
} ); |
| 59 |
} |
| 60 |
|
| 61 |
updateParent() { |
| 62 |
this.props.onValueChange( this.state.value ); |
| 63 |
} |
| 64 |
|
| 65 |
render() { |
| 66 |
return ( |
| 67 |
<CodeMirror |
| 68 |
className="bvs-setting-input" |
| 69 |
value={ this.state.value } |
| 70 |
onChange={ this.onChange } |
| 71 |
extensions={[ |
| 72 |
css(), |
| 73 |
events.content({ |
| 74 |
blur: () => { |
| 75 |
this.updateParent(); |
| 76 |
}, |
| 77 |
}), |
| 78 |
]} |
| 79 |
/> |
| 80 |
); |
| 81 |
} |
| 82 |
} |
| 83 |
|