SqlEditor.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React from 'react'
  21. import debounce from 'lodash/debounce'
  22. import CodeMirror from 'codemirror/lib/codemirror'
  23. import 'codemirror/lib/codemirror.css'
  24. import 'assets/override/codemirror_theme.css'
  25. import 'codemirror/addon/hint/show-hint.css'
  26. import 'codemirror/addon/edit/matchbrackets'
  27. import 'codemirror/mode/sql/sql'
  28. import 'codemirror/addon/hint/show-hint'
  29. import 'codemirror/addon/hint/sql-hint'
  30. import 'codemirror/addon/display/placeholder'
  31. import Styles from '../View.less'
  32. interface ISqlEditorProps {
  33. hints: {
  34. [name: string]: []
  35. }
  36. value: string
  37. onSqlChange: (sql: string) => void
  38. onSqlEnter: () => void
  39. }
  40. export class SqlEditor extends React.PureComponent<ISqlEditorProps> {
  41. private sqlEditorContainer = React.createRef<HTMLTextAreaElement>()
  42. private sqlEditor
  43. private debouncedSqlChange = debounce((val: string) => { this.props.onSqlChange(val) }, 500)
  44. constructor (props) {
  45. super(props)
  46. }
  47. public componentDidMount () {
  48. this.initEditor(CodeMirror, this.props.value)
  49. }
  50. public componentDidUpdate () {
  51. if (this.sqlEditor) {
  52. const { value } = this.props
  53. const localValue = this.sqlEditor.doc.getValue()
  54. if (value !== localValue) {
  55. this.sqlEditor.doc.setValue(this.props.value)
  56. }
  57. }
  58. }
  59. private initEditor = (codeMirror, value: string) => {
  60. const { fromTextArea } = codeMirror
  61. const config = {
  62. mode: 'text/x-sql',
  63. theme: '3024-day',
  64. lineNumbers: true,
  65. lineWrapping: false,
  66. autoCloseBrackets: true,
  67. matchBrackets: true,
  68. foldGutter: true,
  69. extraKeys: {
  70. 'Cmd-Enter': () => { this.props.onSqlEnter() },
  71. 'Ctrl-Enter': () => { this.props.onSqlEnter() }
  72. }
  73. }
  74. this.sqlEditor = fromTextArea(this.sqlEditorContainer.current, config)
  75. this.sqlEditor.doc.setValue(value)
  76. this.sqlEditor.on('change', (_: CodeMirror.Editor, change: CodeMirror.EditorChange) => {
  77. this.debouncedSqlChange(_.getDoc().getValue())
  78. if (change.origin === '+input'
  79. && change.text[0] !== ';'
  80. && change.text[0].trim() !== ''
  81. && change.text[1] !== '') {
  82. this.sqlEditor.showHint({
  83. completeSingle: false,
  84. tables: this.props.hints
  85. })
  86. }
  87. })
  88. }
  89. public render () {
  90. return (
  91. <div className={Styles.sqlEditor}>
  92. <textarea ref={this.sqlEditorContainer} />
  93. </div>
  94. )
  95. }
  96. }
  97. export default SqlEditor