|
| 1 | +import { AppRenderer2, SlotLocation } from '@alipay/alex'; |
| 2 | +import React, { useRef, useState } from 'react'; |
| 3 | +import ReactDOM from 'react-dom'; |
| 4 | +import '@alipay/alex/languages/sql'; |
| 5 | +import { |
| 6 | + CompletionItemKind, |
| 7 | + SqlServiceModule, |
| 8 | + supportLanguage, |
| 9 | + setMonacoEnvironment, |
| 10 | +} from '@alipay/alex-sql-service'; |
| 11 | +import * as SQLPlugin from './sql.plugin'; |
| 12 | +import { Popover, Radio } from 'antd'; |
| 13 | +import 'antd/dist/antd.css'; |
| 14 | +import odcTheme from '@alipay/alex/extensions/alex.odc-theme'; |
| 15 | +import { Button } from '@opensumi/ide-components'; |
| 16 | + |
| 17 | + |
| 18 | +let id = 1 |
| 19 | + |
| 20 | +export const SQLRender: React.FC = (props) => { |
| 21 | + let tableID = 123; |
| 22 | + |
| 23 | + const suggestTables = [ |
| 24 | + { |
| 25 | + label: `sample_two_table_${tableID}`, |
| 26 | + type: 'SAMPLE_TYPE_TWO', |
| 27 | + insertText: 'LD.sample_one_table1', |
| 28 | + kind: CompletionItemKind.Method, |
| 29 | + sortText: 'a', |
| 30 | + }, |
| 31 | + ]; |
| 32 | + const layoutConfig = { |
| 33 | + [SlotLocation.main]: { |
| 34 | + modules: ['@opensumi/ide-editor'], |
| 35 | + }, |
| 36 | + }; |
| 37 | + |
| 38 | + const [editor, setEditor] = useState(true); |
| 39 | + |
| 40 | + id++ |
| 41 | + |
| 42 | + console.log('render sql ==>',id) |
| 43 | + |
| 44 | + return ( |
| 45 | + <div style={{ height: '300px', display: 'flex' }}> |
| 46 | + <Button style={{ zIndex: '100' }} onClick={() => setEditor(false)}>销毁editor</Button> |
| 47 | + {editor && ( |
| 48 | + <div style={{ border: '2px solid red', zIndex: '10', width: '100%'}}> |
| 49 | + <AppRenderer2 |
| 50 | + appConfig={{ |
| 51 | + // plugins: [SQLPlugin], |
| 52 | + modules: [ |
| 53 | + SqlServiceModule.Config({ |
| 54 | + onValidation: (ast: any, markers: any) => { |
| 55 | + // console.log(ast, markers); |
| 56 | + // const filterMarkers = markers.filter((item, index) => { |
| 57 | + // if (item.message == 'SEMICOLON expected.') { |
| 58 | + // const markerText = editorIns.current.editor.getModel().getValueInRange({ |
| 59 | + // startLineNumber: item.startLineNumber, |
| 60 | + // startColumn: item.startColumn, |
| 61 | + // endLineNumber: item.endLineNumber, |
| 62 | + // endColumn: item.endColumn, |
| 63 | + // }); |
| 64 | + // if (!['by', 'from'].includes(markerText)) { |
| 65 | + // return true; |
| 66 | + // } |
| 67 | + // } |
| 68 | + // }); |
| 69 | + return markers; |
| 70 | + }, |
| 71 | + // onWokerLoad: ({language, loadTime}) => { |
| 72 | + // // worker 加载完成 |
| 73 | + // console.log(language, loadTime); |
| 74 | + // }, |
| 75 | + lowerCaseComplete: true, |
| 76 | + marks: [ |
| 77 | + { |
| 78 | + message: '测试效果', |
| 79 | + startRow: 1, |
| 80 | + startCol: 1, |
| 81 | + }, |
| 82 | + ], |
| 83 | + formatRules: [ |
| 84 | + // @ts-ignore |
| 85 | + { |
| 86 | + // regex: /\w/g, |
| 87 | + // value: 'select', |
| 88 | + }, |
| 89 | + ], |
| 90 | + onSuggestTables: (keyword, options) => { |
| 91 | + console.log('suggest', keyword, options, suggestTables); |
| 92 | + return suggestTables; |
| 93 | + }, |
| 94 | + onSuggestFields: (prefix, options) => { |
| 95 | + console.log('files', prefix, options); |
| 96 | + |
| 97 | + return Promise.resolve([ |
| 98 | + { |
| 99 | + label: 'age', |
| 100 | + type: 'SAMPLE_TYPE_ONE', |
| 101 | + insertText: 'age', |
| 102 | + kind: CompletionItemKind.Field, |
| 103 | + sortText: 'b', |
| 104 | + }, |
| 105 | + { |
| 106 | + label: 'banana', |
| 107 | + type: 'SAMPLE_TYPE_ONE', |
| 108 | + insertText: 'banana', |
| 109 | + kind: CompletionItemKind.Field, |
| 110 | + sortText: 'b', |
| 111 | + }, |
| 112 | + { |
| 113 | + label: 'sample_one_table1', |
| 114 | + type: 'SAMPLE_TYPE_ONE', |
| 115 | + insertText: 'id_test', |
| 116 | + kind: CompletionItemKind.Field, |
| 117 | + sortText: 'b', |
| 118 | + }, |
| 119 | + ]); |
| 120 | + }, |
| 121 | + options: { |
| 122 | + language: supportLanguage.ODPSSQL, |
| 123 | + disableAsyncItemCache: true, |
| 124 | + }, |
| 125 | + sorter: (type) => { |
| 126 | + switch (type) { |
| 127 | + case 'TABLEALIAS': |
| 128 | + case 'TABLE': |
| 129 | + return 'c'; |
| 130 | + case 'FIELD': |
| 131 | + case 'FIELDALIAS': |
| 132 | + return 'd'; |
| 133 | + case 'KEYWORD': |
| 134 | + case 'CONSTS': |
| 135 | + return 'e'; |
| 136 | + case 'FUNCTION': |
| 137 | + return 'f'; |
| 138 | + default: |
| 139 | + return 'g'; |
| 140 | + } |
| 141 | + }, |
| 142 | + onChange: (data) => { |
| 143 | + // console.log('change',data); |
| 144 | + }, |
| 145 | + completionTypes: { |
| 146 | + KEYWORD: { |
| 147 | + text: '关键词', |
| 148 | + kind: CompletionItemKind.Keyword, |
| 149 | + }, |
| 150 | + CONSTS: { |
| 151 | + text: '常量', |
| 152 | + kind: CompletionItemKind.Snippet, |
| 153 | + }, |
| 154 | + FUNCTION: { |
| 155 | + text: '函数', |
| 156 | + kind: CompletionItemKind.Function, |
| 157 | + }, |
| 158 | + TABLE: { |
| 159 | + text: '表名', |
| 160 | + kind: CompletionItemKind.Method, |
| 161 | + }, |
| 162 | + TABLEALIAS: { |
| 163 | + text: '表别名', |
| 164 | + kind: CompletionItemKind.Method, |
| 165 | + }, |
| 166 | + SNIPPET: { |
| 167 | + text: '代码块', |
| 168 | + kind: CompletionItemKind.Snippet, |
| 169 | + }, |
| 170 | + FIELD: { |
| 171 | + text: '表字段', |
| 172 | + kind: CompletionItemKind.Field, |
| 173 | + }, |
| 174 | + FIELDALIAS: { |
| 175 | + text: '表字段别名', |
| 176 | + kind: CompletionItemKind.Field, |
| 177 | + }, |
| 178 | + }, |
| 179 | + }), |
| 180 | + ], |
| 181 | + extensionMetadata: [odcTheme], |
| 182 | + workspaceDir: `sql-service-${id}`, |
| 183 | + layoutConfig, |
| 184 | + defaultPreferences: { |
| 185 | + 'general.theme': 'odc-light', |
| 186 | + 'application.confirmExit': 'never', |
| 187 | + 'editor.autoSave': 'afterDelay', |
| 188 | + 'editor.guides.bracketPairs': false, |
| 189 | + 'editor.minimap': false, |
| 190 | + 'editor.autoSaveDelay': 1000, |
| 191 | + 'editor.fixedOverflowWidgets': true, // widget editor 默认改为 fixed |
| 192 | + 'files.encoding': 'utf8', // 默认编码 |
| 193 | + 'editor.fontSize': 12, |
| 194 | + }, |
| 195 | + }} |
| 196 | + runtimeConfig={{ |
| 197 | + biz: 'sql-service', |
| 198 | + // hideEditorTab: true, |
| 199 | + scenario: 'ALEX_TEST', |
| 200 | + // defaultOpenFile: 'test.sql', |
| 201 | + hideBreadcrumb: true, |
| 202 | + hideLeftTabBar: true, |
| 203 | + registerKeybindings: [ |
| 204 | + { |
| 205 | + command: '', |
| 206 | + keybinding: 'f8', |
| 207 | + }, |
| 208 | + ], |
| 209 | + workspace: { |
| 210 | + filesystem: { |
| 211 | + fs: 'FileIndexSystem', |
| 212 | + options: { |
| 213 | + // 初始全量文件索引 |
| 214 | + requestFileIndex() { |
| 215 | + return Promise.resolve({ |
| 216 | + 'test.sql': 'select * from', |
| 217 | + }); |
| 218 | + }, |
| 219 | + }, |
| 220 | + }, |
| 221 | + }, |
| 222 | + }} |
| 223 | + /> |
| 224 | + </div> |
| 225 | + )} |
| 226 | + </div> |
| 227 | + ); |
| 228 | +}; |
0 commit comments