|
19 | 19 | * 说明:当前odc编辑器在(只读)模式下,不具备可编辑的原生能力(格式化) |
20 | 20 | * 场景:仅需要查看格式化后的 SQL,不需要具备编辑能力的场景 |
21 | 21 | */ |
22 | | -import React from 'react'; |
23 | 22 | import MonacoEditor, { IEditor, IProps } from '../MonacoEditor'; |
24 | 23 |
|
25 | | -export class SQLCodePreviewer extends React.PureComponent<IProps> { |
26 | | - render() { |
27 | | - return ( |
28 | | - <> |
29 | | - <MonacoEditor |
30 | | - {...this.props} |
31 | | - onEditorCreated={(editor: IEditor) => { |
32 | | - const newEditor = Object.create(editor); |
33 | | - this.props.onEditorCreated?.( |
34 | | - Object.assign(newEditor, { |
35 | | - doFormat() { |
36 | | - import('@oceanbase-odc/ob-parser-js').then((module) => { |
37 | | - const doc = new module.SQLDocument({ |
38 | | - text: editor.getValue(), |
39 | | - }); |
40 | | - editor.setValue(doc.getFormatText()); |
41 | | - }); |
42 | | - }, |
43 | | - }), |
44 | | - ); |
45 | | - }} |
46 | | - /> |
47 | | - </> |
48 | | - ); |
49 | | - } |
| 24 | +export function SQLCodePreviewer(props: IProps) { |
| 25 | + return ( |
| 26 | + <MonacoEditor |
| 27 | + {...props} |
| 28 | + onEditorCreated={(editor: IEditor) => { |
| 29 | + const newEditor = Object.create(editor); |
| 30 | + props.onEditorCreated?.( |
| 31 | + Object.assign(newEditor, { |
| 32 | + doFormat() { |
| 33 | + import('@oceanbase-odc/ob-parser-js').then((module) => { |
| 34 | + const map = { |
| 35 | + obmysql: module.SQLType.OBMySQL, |
| 36 | + mysql: module.SQLType.MySQL, |
| 37 | + oboracle: module.SQLType.Oracle, |
| 38 | + }; |
| 39 | + const formatted = module.plugins.format({ |
| 40 | + sql: editor.getValue(), |
| 41 | + type: map[props.language], |
| 42 | + }); |
| 43 | + editor.setValue(formatted); |
| 44 | + }); |
| 45 | + }, |
| 46 | + }), |
| 47 | + ); |
| 48 | + }} |
| 49 | + /> |
| 50 | + ); |
50 | 51 | } |
0 commit comments