@@ -132,7 +132,7 @@ function MonacoEditor(props: IProps, ref: ForwardedRef<IExportRefFunction>) {
132132 onSave ?.( value || '' ) ;
133133 } ) ;
134134
135- editorRef . current . addCommand ( monaco . KeyMod . CtrlCmd | monaco . KeyCode . Enter , ( event : Event ) => {
135+ editorRef . current . addCommand ( monaco . KeyMod . CtrlCmd | monaco . KeyCode . Enter , ( ) => {
136136 const value = getCurrentSelectContent ( ) ;
137137 onExecute ?.( value ) ;
138138 } ) ;
@@ -169,11 +169,11 @@ function MonacoEditor(props: IProps, ref: ForwardedRef<IExportRefFunction>) {
169169 * @returns
170170 */
171171 const getCurrentSelectContent = ( ) => {
172- let selection = editorRef . current ?. getSelection ( ) ;
172+ const selection = editorRef . current ?. getSelection ( ) ;
173173 if ( ! selection || selection . isEmpty ( ) ) {
174174 return '' ;
175175 } else {
176- var selectedText = editorRef . current ?. getModel ( ) ?. getValueInRange ( selection ) ;
176+ const selectedText = editorRef . current ?. getModel ( ) ?. getValueInRange ( selection ) ;
177177 return selectedText || '' ;
178178 }
179179 } ;
@@ -187,24 +187,24 @@ function MonacoEditor(props: IProps, ref: ForwardedRef<IExportRefFunction>) {
187187
188188 const createAction = ( editor : IEditorIns ) => {
189189 // 用于控制切换该菜单键的显示
190- const shouldShowSqlRunnerAction = editor . createContextKey ( 'shouldShowSqlRunnerAction' , true ) ;
190+ // const shouldShowSqlRunnerAction = editor.createContextKey('shouldShowSqlRunnerAction', true);
191191
192192 if ( ! props . addAction || ! props . addAction . length ) {
193193 return ;
194194 }
195195
196196 props . addAction . forEach ( ( action ) => {
197- const { id, label, action : runFn } = action ;
197+ const { id : _id , label, action : runFn } = action ;
198198 editor . addAction ( {
199- id,
199+ id : _id ,
200200 label,
201201 // 控制该菜单键显示
202202 precondition : 'shouldShowSqlRunnerAction' ,
203203 // 该菜单键位置
204204 contextMenuGroupId : 'navigation' ,
205205 contextMenuOrder : 1.5 ,
206206 // 点击该菜单键后运行
207- run : ( event : monaco . editor . ICodeEditor ) => {
207+ run : ( ) => {
208208 const selectedText = editor . getModel ( ) ?. getValueInRange ( editor . getSelection ( ) ! ) || '' ;
209209 runFn ( selectedText ) ;
210210 } ,
@@ -234,6 +234,10 @@ export const appendMonacoValue = (editor: any, text: any, range: IRangeType = 'e
234234 editor . setValue ( text ) ;
235235 return ;
236236 }
237+ let newText = text ;
238+ const lastLine = editor . getModel ( ) . getLineCount ( ) ;
239+ const lastLineLength = editor . getModel ( ) . getLineMaxColumn ( lastLine ) ;
240+
237241 switch ( range ) {
238242 // 覆盖所有内容
239243 case 'cover' :
@@ -246,7 +250,7 @@ export const appendMonacoValue = (editor: any, text: any, range: IRangeType = 'e
246250 editor . revealLine ( 1 ) ;
247251 break ;
248252 // 格式化选中区域的sql
249- case 'select' :
253+ case 'select' : {
250254 const selection = editor . getSelection ( ) ;
251255 if ( selection ) {
252256 newRange = new monaco . Range (
@@ -257,24 +261,29 @@ export const appendMonacoValue = (editor: any, text: any, range: IRangeType = 'e
257261 ) ;
258262 }
259263 break ;
264+ }
260265 // 在末尾添加内容
261266 case 'end' :
262- const lastLine = editor . getModel ( ) . getLineCount ( ) ;
263- const lastLineLength = editor . getModel ( ) . getLineMaxColumn ( lastLine ) ;
264267 newRange = new monaco . Range ( lastLine , lastLineLength , lastLine , lastLineLength ) ;
265- text = `${ text } ` ;
266- editor . revealLine ( lastLine ) ;
268+ newText = `${ text } ` ;
267269 break ;
268270 default :
269271 break ;
270272 }
273+
271274 const op = {
272275 range : newRange ,
273- text,
276+ text : newText ,
274277 } ;
278+
275279 // decorations?: IModelDeltaDecoration[]: 一个数组类型的参数,用于指定插入的文本的装饰。可以用来设置文本的样式、颜色、背景色等。如果不需要设置装饰,可以忽略此参数。
276280 const decorations = [ { } ] ; // 解决新增的文本默认背景色为灰色
277281 editor . executeEdits ( 'setValue' , [ op ] , decorations ) ;
282+ if ( range === 'end' ) {
283+ setTimeout ( ( ) => {
284+ editor . revealLine ( lastLine + 1 ) ;
285+ } , 0 ) ;
286+ }
278287} ;
279288
280289export default forwardRef ( MonacoEditor ) ;
0 commit comments