@@ -4,7 +4,7 @@ import { useTheme } from '@/hooks';
44import * as monaco from 'monaco-editor/esm/vs/editor/editor.api' ;
55import { language } from 'monaco-editor/esm/vs/basic-languages/sql/sql' ;
66const { keywords : SQLKeys } = language ;
7- import { editorDefaultOptions , ThemeType } from '@/constants' ;
7+ import { editorDefaultOptions , EditorThemeType , ThemeType } from '@/constants' ;
88import styles from './index.less' ;
99
1010export type IEditorIns = monaco . editor . IStandaloneCodeEditor ;
@@ -21,7 +21,7 @@ interface IProps {
2121 // onChange?: (v: string, e?: IEditorContentChangeEvent) => void;
2222 didMount ?: ( editor : IEditorIns ) => any ;
2323 onSave ?: ( value : string ) => void ; // 快捷键保存的回调
24- onExecute ?: ( value : string ) => void ; // 快捷键执行的回调
24+ onExecute ?: ( value : string ) => void ; // 快捷键执行的回调
2525}
2626
2727export interface IExportRefFunction {
@@ -31,32 +31,33 @@ export interface IExportRefFunction {
3131}
3232
3333function MonacoEditor ( props : IProps , ref : ForwardedRef < IExportRefFunction > ) {
34- const {
35- id,
36- className,
37- language = 'sql' ,
38- didMount,
39- options,
40- isActive,
41- onSave,
42- onExecute
43- } = props ;
34+ const { id, className, language = 'sql' , didMount, options, isActive, onSave, onExecute } = props ;
4435 const editorRef = useRef < IEditorIns > ( ) ;
4536 const [ appTheme ] = useTheme ( ) ;
4637
4738 // init
4839 useEffect ( ( ) => {
4940 const editorIns = monaco . editor . create ( document . getElementById ( `monaco-editor-${ id } ` ) ! , {
5041 ...editorDefaultOptions ,
51- ...options ,
5242 value : '' ,
5343 language : language ,
54- theme : appTheme . backgroundColor === ThemeType . Light ? 'Default' : 'BlackTheme' ,
44+ theme : appTheme . backgroundColor === ThemeType . Light ? EditorThemeType . Default : EditorThemeType . BlackTheme ,
45+ ...options ,
5546 } ) ;
5647 editorRef . current = editorIns ;
57- didMount && didMount ( editorIns ) ; // incase parent component wanna handle editor
48+ didMount && didMount ( editorIns ) ;
5849
59- monaco . editor . defineTheme ( 'BlackTheme' , {
50+ monaco . editor . defineTheme ( EditorThemeType . Default , {
51+ base : 'vs' ,
52+ inherit : true ,
53+ rules : [ { background : '#15161a' } ] as any ,
54+ colors : {
55+ 'editor.foreground' : '#000000' ,
56+ 'editor.background' : '#fff' , //背景色
57+ } ,
58+ } ) ;
59+
60+ monaco . editor . defineTheme ( EditorThemeType . BlackTheme , {
6061 base : 'vs-dark' ,
6162 inherit : true ,
6263 rules : [ { background : '#15161a' } ] as any ,
@@ -67,13 +68,23 @@ function MonacoEditor(props: IProps, ref: ForwardedRef<IExportRefFunction>) {
6768 } ,
6869 } ) ;
6970
70- monaco . editor . defineTheme ( 'Default' , {
71+ monaco . editor . defineTheme ( EditorThemeType . DashboardLightTheme , {
7172 base : 'vs' ,
7273 inherit : true ,
7374 rules : [ { background : '#15161a' } ] as any ,
7475 colors : {
7576 'editor.foreground' : '#000000' ,
76- 'editor.background' : '#fff' , //背景色
77+ 'editor.background' : '#f8f9fa' , //背景色
78+ } ,
79+ } ) ;
80+
81+ monaco . editor . defineTheme ( EditorThemeType . DashboardBlackTheme , {
82+ base : 'vs-dark' ,
83+ inherit : true ,
84+ rules : [ { background : '#15161a' } ] as any ,
85+ colors : {
86+ 'editor.foreground' : '#ffffff' ,
87+ 'editor.background' : '#131418' , //背景色
7788 } ,
7889 } ) ;
7990
@@ -94,16 +105,21 @@ function MonacoEditor(props: IProps, ref: ForwardedRef<IExportRefFunction>) {
94105 } ) ;
95106
96107 editorRef . current . addCommand ( monaco . KeyMod . CtrlCmd | monaco . KeyCode . Enter , ( event : Event ) => {
97- const value = getCurrentSelectContent ( )
108+ const value = getCurrentSelectContent ( ) ;
98109 onExecute ?.( value ) ;
99110 } ) ;
100111 }
101- } , [ editorRef . current , isActive ] )
112+ } , [ editorRef . current , isActive ] ) ;
102113
103114 // 监听主题色变化切换编辑器主题色
104115 useEffect ( ( ) => {
105- monaco . editor . setTheme ( appTheme . backgroundColor === ThemeType . Dark ? 'BlackTheme' : 'Default' ) ;
106- } , [ appTheme . backgroundColor ] ) ;
116+ const isDark = appTheme . backgroundColor === ThemeType . Dark ;
117+ if ( options ?. theme ) {
118+ monaco . editor . setTheme ( options . theme ) ;
119+ } else {
120+ monaco . editor . setTheme ( isDark ? 'BlackTheme' : 'Default' ) ;
121+ }
122+ } , [ appTheme . backgroundColor , options ?. theme ] ) ;
107123
108124 // useEffect(() => {
109125 // const _ref = editorRef.current?.onDidChangeModelContent((e) => {
@@ -116,12 +132,12 @@ function MonacoEditor(props: IProps, ref: ForwardedRef<IExportRefFunction>) {
116132 useImperativeHandle ( ref , ( ) => ( {
117133 getCurrentSelectContent,
118134 getAllContent,
119- setValue
135+ setValue,
120136 } ) ) ;
121137
122138 const setValue = ( text : any , range ?: IRangeType ) => {
123- appendMonacoValue ( editorRef . current , text , range )
124- }
139+ appendMonacoValue ( editorRef . current , text , range ) ;
140+ } ;
125141
126142 /**
127143 * 获取当前选中的内容
@@ -243,20 +259,20 @@ function MonacoEditor(props: IProps, ref: ForwardedRef<IExportRefFunction>) {
243259
244260// text 需要添加的文本
245261// range 添加到的位置
246- // 'end' 末尾
262+ // 'end' 末尾
247263// 'front' 开头
248264// 'cover' 覆盖掉原有的文字
249265// 自定义位置数组 new monaco.Range []
250266export type IRangeType = 'end' | 'front' | 'cover' | any ;
251267
252268export const appendMonacoValue = ( editor : any , text : any , range : IRangeType = 'end' ) => {
253269 if ( ! editor ) {
254- return
270+ return ;
255271 }
256272 const model = editor ?. getModel && editor . getModel ( editor ) ;
257273 // 创建编辑操作,将当前文档内容替换为新内容
258274 let newRange : IRangeType = range ;
259- text = `${ text } \n`
275+ text = `${ text } \n` ;
260276 switch ( range ) {
261277 case 'cover' :
262278 newRange = model . getFullModelRange ( ) ;
@@ -279,6 +295,6 @@ export const appendMonacoValue = (editor: any, text: any, range: IRangeType = 'e
279295 // decorations?: IModelDeltaDecoration[]: 一个数组类型的参数,用于指定插入的文本的装饰。可以用来设置文本的样式、颜色、背景色等。如果不需要设置装饰,可以忽略此参数。
280296 const decorations = [ { } ] ; // 解决新增的文本默认背景色为灰色
281297 editor . executeEdits ( 'setValue' , [ op ] , decorations ) ;
282- }
298+ } ;
283299
284300export default forwardRef ( MonacoEditor ) ;
0 commit comments