@@ -15,8 +15,10 @@ import { Popover, Radio, Menu } from 'antd';
1515import 'antd/dist/antd.css' ;
1616import odcTheme from '@alipay/alex/extensions/alex.odc-theme' ;
1717import { SQLRender } from './sqlRender' ;
18-
19- setMonacoEnvironment ( ) ;
18+ import * as monaco from '@opensumi/monaco-editor-core/esm/vs/editor/editor.api' ;
19+ import { ODPSTokens } from '@alipay/alex-sql-service/lib/config' ;
20+ import { ILanguageService } from '@opensumi/monaco-editor-core/esm/vs/editor/common/languages/language' ;
21+ import { StandaloneServices } from '@opensumi/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices' ;
2022
2123const App = ( ) => {
2224
@@ -118,7 +120,6 @@ const App = () => {
118120 const [ current , setCurrent ] = useState ( '1' ) ;
119121
120122 const menuCick = ( e ) => {
121- console . log ( 'click ' , e ) ;
122123 setCurrent ( e . key ) ;
123124 // 每次切换tab 打开对应的文件
124125 if ( e . key === '1' ) {
@@ -128,14 +129,132 @@ const App = () => {
128129 }
129130 } ;
130131
132+ // 渲染 monaco 原生编辑器
133+ const WhiteTheme : any = {
134+ base : 'vs' ,
135+ inherit : true ,
136+ colors : {
137+ 'editor.background' : '#FFFFFF' , // 背景色
138+ 'editor.lineHighlightBackground' : '#e8f3ff' , // 行高亮色
139+ 'editorCursor.foreground' : '#171617' , // 光标颜色
140+ 'editor.selectionBackground' : '#8AA7F8' , // 选中文本框颜色
141+ 'editorIndentGuide.background' : '#e8f3ff' , // 层级提示颜色
142+ 'editorBracketMatch.background' : '#8AA7F8' , // 选中括号的背景色
143+ 'editor.selectionHighlightBackground' : '#CBD4F2' , // 已选中的其他内容的高亮颜色
144+ 'editor.findMatchBackground' : '#FFA011' ,
145+
146+ } ,
147+ rules : [
148+ { token : '' , foreground : '171617' , background : 'EBEAEF' } , // 默认字体颜色,以及右侧 minimap 背景色
149+ { token : 'keywords' , foreground : '770088' , fontStyle : 'bold' } , // 关键词颜色
150+ { token : 'customKeywords' , foreground : '196FD8' } , // 自定义关键词颜色
151+ { token : 'number' , foreground : '2E7F01' } , // 数值颜色
152+ { token : 'comment' , foreground : 'ab5808' } , // 注释颜色
153+ { token : 'builtinFunctions' , foreground : 'CB3BC1' } , // 内置函数
154+ { token : 'function' , foreground : 'CB3BC1' } , // 函数颜色
155+ { token : 'operator' , foreground : '232226' } , // 运算符颜色
156+ { token : 'string' , foreground : 'D45E00' } , // 字符串颜色
157+ ] ,
158+ encodedTokensColors : [
159+ null ,
160+ '#333333' ,
161+ '#FFFFFF' ,
162+ '#CB3BC1' ,
163+ '#AB5808' ,
164+ '#0000FF' ,
165+ '#811F3F' ,
166+ '#FF0000' ,
167+ '#09885A' ,
168+ '#0451A5' ,
169+ '#196FD8' ,
170+ '#267F99' ,
171+ '#795E26' ,
172+ '#800000' ,
173+ '#001080' ,
174+ '#CD3131' ,
175+ '#770088' ,
176+ '#AF00DB' ,
177+ '#000000' ,
178+ '#D16969' ,
179+ '#000080' ,
180+ '#A31515' ,
181+ '#2E7F01' ,
182+ '#232226' ,
183+ '#D45E00' ,
184+ ] ,
185+ } ;
186+
187+
188+ function initMonaco ( ) {
189+ initMonacoTheme ( ) ;
190+ createMonao ( ) ;
191+ initMonacoLanguage ( ) ;
192+ }
193+ function initMonacoTheme ( ) {
194+ monaco . editor . defineTheme ( 'monaco-light' , WhiteTheme ) ;
195+ monaco . editor . setTheme ( 'monaco-light' ) ;
196+ monaco . languages . setMonarchTokensProvider ( 'odc-sql' , ODPSTokens ( ) ) ;
197+ }
198+
199+ function initMonacoLanguage ( ) {
200+ const LanguageService = StandaloneServices . get ( ILanguageService )
201+ LanguageService [ '_registry' ] [ '_registerLanguages' ] ( [ {
202+ id : 'odc-sql'
203+ } ] )
204+ monaco . languages . registerCompletionItemProvider ( 'odc-sql' , {
205+ provideCompletionItems : ( model , position , context , token ) => {
206+ // TODO 关键词排序
207+ const suggestions = [ 'select' , 'from' ] . map ( ( key ) => {
208+ return {
209+ label : key ,
210+ kind : monaco . languages . CompletionItemKind . Keyword ,
211+ insertText : key ,
212+ range : {
213+ startLineNumber : position . lineNumber ,
214+ endLineNumber : position . lineNumber ,
215+ startColumn : position . column - 1 ,
216+ endColumn : position . column ,
217+ } ,
218+ } as monaco . languages . CompletionItem ;
219+ } ) ;
220+ return {
221+ suggestions : suggestions ,
222+ } ;
223+ } ,
224+ } ) ;
225+ }
226+
227+
228+ function createMonao ( ) {
229+ monaco . editor . create ( document . getElementById ( 'monaco' ) as HTMLElement , {
230+ language : 'odc-sql' ,
231+ value : 'select * from' ,
232+ folding : true ,
233+ theme : 'monaco-light' ,
234+ lineNumbersMinChars : 3.5 ,
235+ scrollbar : {
236+ verticalScrollbarSize : 8 ,
237+ horizontalScrollbarSize : 8 ,
238+ } ,
239+ minimap : {
240+ enabled : false ,
241+ } ,
242+ formatOnPaste : true ,
243+ renderValidationDecorations : 'on' ,
244+ } ) ;
245+
246+ }
247+
131248 return (
132249 < div style = { { height : '100%' , overflow : 'scroll' } } >
133250 < div style = { { height : '300px' } } >
134- < div style = { { margin : '20px' } } >
251+ < div style = { { margin : '20px' } } onContextMenu = { ( ) => { console . log ( 'ContextMenu' ) } } onClick = { ( ) => console . log ( 'click' ) } >
135252 < Button onClick = { ( ) => format ( ) } > 格式化</ Button >
136253 < Button onClick = { ( ) => addLine ( ) } > 添加行</ Button >
137254 < Button onClick = { ( ) => openFile ( ) } > 打开文件</ Button >
138255 < Button onClick = { ( ) => editor ( ) } > 获取当前内容</ Button >
256+ < Button onClick = { ( ) => initMonaco ( ) } > 原生编辑器</ Button >
257+
139258 { /* <Button onClick={() => changeTables()}>change suggest Tables</Button> */ }
140259 < Popover content = { content } placement = "top" >
141260 < Button > 设置</ Button >
@@ -147,7 +266,7 @@ const App = () => {
147266 < div style = { { display : `${ current === '1' ? 'block' : 'none' } ` } } >
148267 < SQLRender id = { current } visible = { current === '1' } />
149268 </ div >
150- < div style = { { display : `${ current === '2' ? 'block' : 'none' } ` } } >
269+ < div style = { { display : `${ current === '2' ? 'block' : 'none' } ` } } >
151270 < SQLRender id = { current } visible = { current === '2' } />
152271 </ div >
153272 </ div >
@@ -331,6 +450,9 @@ const App = () => {
331450 }}
332451 /> */ }
333452 </ div >
453+ < div style = { { height : '300px' , margin : '20px' , border : '2px soldi' } } id = "monaco" >
454+
455+ </ div >
334456 </ div >
335457 ) ;
336458} ;
0 commit comments