|
| 1 | +import React, { memo, useEffect, useContext } from 'react'; |
| 2 | +import classnames from 'classnames'; |
| 3 | +import styles from './index.less'; |
| 4 | +import Iconfont from '@/components/Iconfont'; |
| 5 | +import SingleFileMonacoEditor from '@/components/SingleFileMonacoEditor'; |
| 6 | +import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'; |
| 7 | +import { IExecuteSqlParams } from '@/service/sql'; |
| 8 | +import { Context } from '@/components/SearchResult'; |
| 9 | + |
| 10 | +interface IProps { |
| 11 | + className?: string; |
| 12 | + promptWord: any[]; |
| 13 | + getTableData: (params?: Partial<IExecuteSqlParams>) => void; |
| 14 | +} |
| 15 | + |
| 16 | +const keywordHintList = [ |
| 17 | + 'AND', |
| 18 | + 'OR', |
| 19 | + 'NOT', |
| 20 | + 'IS', |
| 21 | + 'NULL', |
| 22 | + 'IN', |
| 23 | + 'IS NOT NULL', |
| 24 | + 'IS NULL', |
| 25 | + 'IS NOT', |
| 26 | + 'NOT IN', |
| 27 | + 'EXISTS', |
| 28 | + 'BETWEEN', |
| 29 | + 'LIKE', |
| 30 | + 'ASC', |
| 31 | + 'DESC', |
| 32 | +] |
| 33 | + |
| 34 | +export default memo<IProps>((props) => { |
| 35 | + const { promptWord, getTableData } = props; |
| 36 | + const { notChangedSql } = useContext(Context); |
| 37 | + const [isActive, setIsActive] = React.useState(false); |
| 38 | + const keywordHintRef = React.useRef<any>(null); |
| 39 | + const fieldHintRef = React.useRef<any>(null); |
| 40 | + const whereSingleFileMonacoEditorRef = React.useRef<any>(null); |
| 41 | + const orderBySingleFileMonacoEditorRef = React.useRef<any>(null); |
| 42 | + |
| 43 | + useEffect(() => { |
| 44 | + keywordHintRef.current && keywordHintRef.current.dispose(); |
| 45 | + fieldHintRef.current && fieldHintRef.current.dispose(); |
| 46 | + if (isActive) { |
| 47 | + registerPromptWord(); |
| 48 | + } |
| 49 | + }, [promptWord, isActive]); |
| 50 | + |
| 51 | + const registerPromptWord = () => { |
| 52 | + const suggestions = promptWord.slice(1).map((item) => { |
| 53 | + return { |
| 54 | + insertText: item.name, |
| 55 | + kind: monaco.languages.CompletionItemKind.Field, |
| 56 | + label: item.name, |
| 57 | + }; |
| 58 | + }); |
| 59 | + |
| 60 | + const provideCompletionItems: any = () => { |
| 61 | + return { |
| 62 | + suggestions, |
| 63 | + }; |
| 64 | + }; |
| 65 | + |
| 66 | + keywordHintRef.current = monaco.languages.registerCompletionItemProvider('sql', { |
| 67 | + provideCompletionItems, |
| 68 | + triggerCharacters: [], |
| 69 | + }); |
| 70 | + |
| 71 | + keywordHintRef.current = monaco.languages.registerCompletionItemProvider('sql', { |
| 72 | + provideCompletionItems: () => { |
| 73 | + return { |
| 74 | + suggestions: keywordHintList.map((item) => { |
| 75 | + return { |
| 76 | + insertText: item, |
| 77 | + kind: monaco.languages.CompletionItemKind.Keyword, |
| 78 | + label: item, |
| 79 | + }; |
| 80 | + }), |
| 81 | + }; |
| 82 | + }, |
| 83 | + triggerCharacters: [], |
| 84 | + }); |
| 85 | + }; |
| 86 | + |
| 87 | + const search = () => { |
| 88 | + const whereValue = whereSingleFileMonacoEditorRef.current?.getAllContent().trim() || ''; |
| 89 | + const orderByValue = orderBySingleFileMonacoEditorRef.current?.getAllContent().trim() || ''; |
| 90 | + let sql = whereValue ? notChangedSql + ' WHERE ' + whereValue : notChangedSql; |
| 91 | + sql = orderByValue ? sql + ' ORDER BY ' + orderByValue : sql; |
| 92 | + getTableData({ sql }); |
| 93 | + }; |
| 94 | + |
| 95 | + const focusChange = (_isActive: boolean) => { |
| 96 | + setIsActive(_isActive); |
| 97 | + }; |
| 98 | + |
| 99 | + return ( |
| 100 | + <div className={styles.screeningResult}> |
| 101 | + <div className={styles.whereBox}> |
| 102 | + <div className={styles.titleBox}> |
| 103 | + <Iconfont box boxSize={20} classNameBox={styles.titleIcon} code="" /> |
| 104 | + <div |
| 105 | + className={classnames(styles.title, { |
| 106 | + [styles.activeTitle]: true, |
| 107 | + })} |
| 108 | + > |
| 109 | + WHERE |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + <SingleFileMonacoEditor |
| 113 | + ref={whereSingleFileMonacoEditorRef} |
| 114 | + focusChange={focusChange} |
| 115 | + handelEnter={search} |
| 116 | + className={styles.monacoEditor} |
| 117 | + /> |
| 118 | + </div> |
| 119 | + <div className={styles.orderByBox}> |
| 120 | + <div className={styles.titleBox}> |
| 121 | + <Iconfont box boxSize={20} classNameBox={styles.titleIcon} code="" /> |
| 122 | + <div |
| 123 | + className={classnames(styles.title, { |
| 124 | + [styles.activeTitle]: true, |
| 125 | + })} |
| 126 | + > |
| 127 | + ORDER BY |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + <SingleFileMonacoEditor |
| 131 | + ref={orderBySingleFileMonacoEditorRef} |
| 132 | + focusChange={focusChange} |
| 133 | + handelEnter={search} |
| 134 | + className={styles.monacoEditor} |
| 135 | + /> |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + ); |
| 139 | +}); |
0 commit comments