@@ -50,7 +50,11 @@ export interface Props {
50
50
}
51
51
52
52
const SYNTAX_CONTEXT_ID = 'syntaxWidgetContext'
53
+ const SYNTAX_WIDGET_ID = 'syntax.content.widget'
54
+
53
55
const argInQuotesRegExp = / ^ [ ' " ] ( .| [ \r \n ] ) * [ ' " ] $ /
56
+ const aroundQuotesRegExp = / ( ^ [ " ' ] | [ " ' ] $ ) / g
57
+
54
58
let decorations : string [ ] = [ ]
55
59
let execHistoryPos : number = 0
56
60
let execHistory : CommandExecutionUI [ ] = [ ]
@@ -75,14 +79,15 @@ const Query = (props: Props) => {
75
79
let disposeCompletionItemProvider = ( ) => { }
76
80
let disposeSignatureHelpProvider = ( ) => { }
77
81
78
- useEffect ( ( ) =>
79
- // componentWillUnmount
80
- ( ) => {
82
+ useEffect ( ( ) => {
83
+ // componentWillUnmount
84
+ return ( ) => {
81
85
contribution ?. dispose ?.( )
82
86
disposeCompletionItemProvider ( )
83
87
disposeSignatureHelpProvider ( )
84
- } ,
85
- [ ] )
88
+ }
89
+ } , [ ] )
90
+
86
91
87
92
useEffect ( ( ) => {
88
93
// HACK: The Monaco editor memoize the state and ignores updates to it
@@ -116,6 +121,23 @@ const Query = (props: Props) => {
116
121
isDedicatedEditorOpenRef . current = isDedicatedEditorOpen
117
122
} , [ isDedicatedEditorOpen ] )
118
123
124
+ const triggerUpdateCursorPosition = ( editor : monacoEditor . editor . IStandaloneCodeEditor ) => {
125
+ const position = editor . getPosition ( )
126
+ isDedicatedEditorOpenRef . current = false
127
+ editor . trigger ( 'mouse' , '_moveTo' , { position : { lineNumber : 1 , column : 1 } } )
128
+ editor . trigger ( 'mouse' , '_moveTo' , { position } )
129
+ editor . focus ( )
130
+ }
131
+
132
+ const onPressWidget = ( ) => {
133
+ if ( ! monacoObjects . current ) return
134
+ const { editor } = monacoObjects ?. current
135
+
136
+ setIsDedicatedEditorOpen ( true )
137
+ editor . updateOptions ( { readOnly : true } )
138
+ hideSyntaxWidget ( editor )
139
+ }
140
+
119
141
const onChange = ( value : string = '' ) => {
120
142
setQuery ( value )
121
143
@@ -151,7 +173,7 @@ const Query = (props: Props) => {
151
173
}
152
174
153
175
const onTriggerContentWidget = ( position : Nullable < monacoEditor . Position > , language : string = '' ) : monaco . editor . IContentWidget => ( {
154
- getId : ( ) => 'syntax.content.widget' ,
176
+ getId : ( ) => SYNTAX_WIDGET_ID ,
155
177
getDomNode : ( ) => createSyntaxWidget ( `Use ${ language } Editor` , 'Shift+Space' ) ,
156
178
getPosition : ( ) => ( {
157
179
position,
@@ -166,8 +188,12 @@ const Query = (props: Props) => {
166
188
const { editor } = monacoObjects ?. current
167
189
168
190
const position = editor . getPosition ( )
169
- // @ts -ignore
170
- if ( position ?. lineNumber !== 1 || editor . getContribution ( 'editor.contrib.suggestController' ) ?. model ?. state ) return
191
+ if (
192
+ position ?. column !== 1 ||
193
+ position ?. lineNumber !== 1 ||
194
+ // @ts -ignore
195
+ editor . getContribution ( 'editor.contrib.suggestController' ) ?. model ?. state
196
+ ) return
171
197
172
198
if ( execHistory [ execHistoryPos ] ) {
173
199
const command = execHistory [ execHistoryPos ] . command || ''
@@ -230,10 +256,6 @@ const Query = (props: Props) => {
230
256
231
257
const queryArg = command . args [ argIndex ]
232
258
const argDSL = command . info ?. arguments ?. [ argIndex ] ?. dsl || ''
233
- if ( ! argIndex ) {
234
- isWidgetEscaped . current = false
235
- return
236
- }
237
259
238
260
if ( queryArgIndex === argIndex && argInQuotesRegExp . test ( queryArg ) ) {
239
261
if ( isWidgetEscaped . current ) return
@@ -281,35 +303,35 @@ const Query = (props: Props) => {
281
303
const { editor } = monacoObjects ?. current
282
304
283
305
editor . updateOptions ( { readOnly : false } )
306
+ triggerUpdateCursorPosition ( editor )
284
307
}
285
308
286
309
const updateArgFromDedicatedEditor = ( value : string = '' ) => {
287
- if ( syntaxCommand . current ) {
288
- if ( ! monacoObjects . current ) return
289
- const { editor } = monacoObjects ?. current
310
+ if ( ! syntaxCommand . current || ! monacoObjects . current ) return
311
+ const { editor } = monacoObjects ?. current
290
312
291
- const model = editor . getModel ( )
292
- if ( ! model ) return
313
+ const model = editor . getModel ( )
314
+ if ( ! model ) return
293
315
294
- const wrapQuote = syntaxCommand . current . argToReplace [ 0 ]
295
- const replaceCommand = syntaxCommand . current . fullQuery . replace (
296
- syntaxCommand . current . argToReplace ,
297
- `${ wrapQuote } ${ value } ${ wrapQuote } `
298
- )
299
- editor . updateOptions ( { readOnly : false } )
300
- editor . executeEdits ( null , [
301
- {
302
- range : new monaco . Range (
303
- syntaxCommand . current . commandPosition . startLine ,
304
- 0 ,
305
- syntaxCommand . current . commandPosition . endLine ,
306
- model . getLineLength ( syntaxCommand . current . commandPosition . endLine ) + 1
307
- ) ,
308
- text : replaceCommand
309
- }
310
- ] )
311
- setIsDedicatedEditorOpen ( false )
312
- }
316
+ const wrapQuote = syntaxCommand . current . argToReplace [ 0 ]
317
+ const replaceCommand = syntaxCommand . current . fullQuery . replace (
318
+ syntaxCommand . current . argToReplace ,
319
+ `${ wrapQuote } ${ value } ${ wrapQuote } `
320
+ )
321
+ editor . updateOptions ( { readOnly : false } )
322
+ editor . executeEdits ( null , [
323
+ {
324
+ range : new monaco . Range (
325
+ syntaxCommand . current . commandPosition . startLine ,
326
+ 0 ,
327
+ syntaxCommand . current . commandPosition . endLine ,
328
+ model . getLineLength ( syntaxCommand . current . commandPosition . endLine ) + 1
329
+ ) ,
330
+ text : replaceCommand
331
+ }
332
+ ] )
333
+ setIsDedicatedEditorOpen ( false )
334
+ triggerUpdateCursorPosition ( editor )
313
335
}
314
336
315
337
const editorDidMount = (
@@ -335,11 +357,15 @@ const Query = (props: Props) => {
335
357
)
336
358
337
359
editor . addCommand ( monaco . KeyMod . Shift | monaco . KeyCode . Space , ( ) => {
338
- setIsDedicatedEditorOpen ( true )
339
- editor . updateOptions ( { readOnly : true } )
340
- hideSyntaxWidget ( editor )
360
+ onPressWidget ( )
341
361
} , SYNTAX_CONTEXT_ID )
342
362
363
+ editor . onMouseDown ( ( e : monaco . editor . IEditorMouseEvent ) => {
364
+ if ( e . target . detail === SYNTAX_WIDGET_ID ) {
365
+ onPressWidget ( )
366
+ }
367
+ } )
368
+
343
369
editor . addCommand ( monaco . KeyCode . Escape , ( ) => {
344
370
hideSyntaxWidget ( editor )
345
371
isWidgetEscaped . current = true
@@ -431,7 +457,7 @@ const Query = (props: Props) => {
431
457
{ isDedicatedEditorOpen && (
432
458
< DedicatedEditor
433
459
lang = { syntaxCommand . current . lang }
434
- value = { selectedArg . current . replace ( / ( ^ [ " ' ] | [ " ' ] $ ) / g , '' ) }
460
+ value = { selectedArg . current . replace ( aroundQuotesRegExp , '' ) }
435
461
onSubmit = { updateArgFromDedicatedEditor }
436
462
onCancel = { onCancelDedicatedEditor }
437
463
width = { input ?. current ?. scrollWidth || 300 }
0 commit comments