@@ -7,12 +7,14 @@ import {
77 SearchQuery ,
88 setSearchQuery ,
99} from "@codemirror/search" ;
10+ import { EditorView } from "@codemirror/view" ;
1011import { executeCommand , getRegisteredCommands } from "cm/commandRegistry" ;
1112import { focusEditorIfEditable } from "cm/editorReadOnly" ;
1213import {
1314 canQuickToolsEdit ,
1415 finishQuickToolsModifierInput ,
1516 focusQuickToolsModifierInput ,
17+ isSelectedRangeDeletion ,
1618 setQuickToolsModifierInputHandler ,
1719} from "cm/quickToolsModifierInput" ;
1820import {
@@ -30,6 +32,7 @@ import {
3032 clearModifierState ,
3133 clearQuickToolsButtonFeedback ,
3234 removeActionStackEntries ,
35+ shouldCaptureModifierInput ,
3336} from "./quickToolsState" ;
3437
3538export let quickToolUsed = false ;
@@ -69,13 +72,12 @@ quickTools.$input.addEventListener("input", (e) => {
6972 if ( ! key || key . length > 1 ) return ;
7073 const keyCombination = getKeys ( { key } ) ;
7174 const target = getInput ( ) ;
75+ const codeMirrorView = getCodeMirrorInputView ( target ) ;
7276
73- if ( isCodeMirrorEditorInput ( target ) ) {
74- handleCodeMirrorQuickToolsTextInput (
75- editorManager . editor ,
76- key ,
77- keyCombination ,
78- ) ;
77+ if (
78+ codeMirrorView &&
79+ runCodeMirrorQuickToolsTextKey ( codeMirrorView , key , keyCombination )
80+ ) {
7981 return ;
8082 }
8183
@@ -108,20 +110,27 @@ quickTools.$input.addEventListener("keydown", (e) => {
108110 e . preventDefault ( ) ;
109111
110112 let target = getInput ( ) ;
111- if ( isCodeMirrorEditorInput ( target ) ) {
113+ const codeMirrorView = getCodeMirrorInputView ( target ) ;
114+ if ( codeMirrorView ) {
112115 try {
113- runCodeMirrorQuickToolKey ( keyCode , keyCombination ) ;
116+ const handled = runQuickToolKey ( codeMirrorView , keyCode , keyCombination ) ;
117+ if ( ! handled && ! codeMirrorView . state . readOnly ) {
118+ target . dispatchEvent ( KeyboardEvent ( "keydown" , keyCombination ) ) ;
119+ }
114120 } finally {
115- resetKeys ( ) ;
116- dismissReadOnlyQuickToolsInput ( editorManager . editor ) ;
121+ if ( codeMirrorView . state . readOnly ) {
122+ resetKeys ( ) ;
123+ dismissReadOnlyQuickToolsInput ( codeMirrorView ) ;
124+ }
125+ setQuicktoolsUsed ( ) ;
117126 }
118127 return ;
119128 }
120129 if ( target === quickTools . $input ) {
121- target = editorManager . editor . contentDOM ;
130+ target = editorManager . editor ? .contentDOM ;
122131 }
123132
124- target . dispatchEvent ( KeyboardEvent ( "keydown" , keyCombination ) ) ;
133+ target ? .dispatchEvent ( KeyboardEvent ( "keydown" , keyCombination ) ) ;
125134 setQuicktoolsUsed ( ) ;
126135} ) ;
127136
@@ -244,7 +253,9 @@ export function cancelQuickToolsModifierInput() {
244253 const changed = clearQuickToolsModifierState ( ) ;
245254 quickTools . $input . value = "" ;
246255 quickTools . $input . blur ( ) ;
247- dismissReadOnlyQuickToolsInput ( editorManager . editor ) ;
256+ dismissReadOnlyQuickToolsInput (
257+ getCodeMirrorInputView ( input ) || editorManager . editor ,
258+ ) ;
248259 return changed ;
249260}
250261
@@ -266,10 +277,19 @@ export default function actions(action, value) {
266277 state [ action ] = value ;
267278 events [ action ] . forEach ( ( cb ) => cb ( value ) ) ;
268279 if ( Object . values ( state ) . includes ( true ) ) {
269- if ( isCodeMirrorEditorInput ( input ) ) {
270- focusQuickToolsModifierInput ( editor , $input ) ;
280+ const codeMirrorView = getCodeMirrorInputView ( input ) ;
281+ const shouldCapture =
282+ codeMirrorView ?. state . readOnly ||
283+ shouldCaptureModifierInput ( state , Boolean ( codeMirrorView ) ) ;
284+ if ( shouldCapture ) {
285+ $input . value = "" ;
286+ if ( codeMirrorView ?. state . readOnly ) {
287+ focusQuickToolsModifierInput ( codeMirrorView , $input ) ;
288+ } else {
289+ $input . focus ( ) ;
290+ }
271291 } else {
272- $input . focus ( ) ;
292+ if ( codeMirrorView ) focusEditorIfEditable ( codeMirrorView ) ;
273293 }
274294 } else {
275295 restoreQuickToolsTargetFocus ( ) ;
@@ -411,27 +431,36 @@ function setInput() {
411431 input = activeElement ;
412432}
413433
414- function isCodeMirrorEditorInput ( target ) {
415- const { editor, activeFile } = editorManager ;
416- if ( ! editor || activeFile ?. type !== "editor" ) return false ;
417- const contentDOM = editor . contentDOM ;
418- return target === contentDOM || ( contentDOM ?. contains ?. ( target ) ?? false ) ;
434+ function getCodeMirrorInputView ( target ) {
435+ if ( ! ( target instanceof HTMLElement ) ) return null ;
436+ const view = EditorView . findFromDOM ( target ) ;
437+ if ( ! view ?. contentDOM ) return null ;
438+ return target === view . contentDOM || view . contentDOM . contains ( target )
439+ ? view
440+ : null ;
419441}
420442
421443function runCodeMirrorQuickToolKey ( keyCode , keyCombination ) {
422- if ( ! isCodeMirrorEditorInput ( input ) ) return false ;
423- return runQuickToolKey ( editorManager . editor , keyCode , keyCombination ) ;
444+ const view = getCodeMirrorInputView ( input ) ;
445+ return view ? runQuickToolKey ( view , keyCode , keyCombination ) : false ;
424446}
425447
426- export function handleCodeMirrorQuickToolsTextInput (
427- view ,
428- text ,
429- modifiers = null ,
430- ) {
448+ export function handleCodeMirrorQuickToolsTextInput ( view , input ) {
449+ if ( ! Object . values ( state ) . includes ( true ) ) return false ;
431450 if ( ! view ?. state || ! view . contentDOM ) return false ;
451+ if ( isSelectedRangeDeletion ( view , input ) ) {
452+ setQuicktoolsUsed ( ) ;
453+ return true ;
454+ }
455+
456+ const { text } = input ;
432457 if ( ! text || text . length !== 1 ) return false ;
433458
434- const keyCombination = modifiers || getKeys ( { key : text } ) ;
459+ const keyCombination = getKeys ( { key : text } ) ;
460+ return runCodeMirrorQuickToolsTextKey ( view , text , keyCombination ) ;
461+ }
462+
463+ function runCodeMirrorQuickToolsTextKey ( view , text , keyCombination ) {
435464 if ( ! hasQuickToolsModifier ( keyCombination ) ) return false ;
436465 const canEdit = canQuickToolsEdit ( view ) ;
437466 resetKeys ( ) ;
@@ -921,8 +950,7 @@ function blockReadOnlyQuickToolsEdit(view) {
921950}
922951
923952function isReadOnlyCodeMirrorInput ( target ) {
924- const { editor } = editorManager ;
925- return isCodeMirrorEditorInput ( target ) && ! ! editor ?. state ?. readOnly ;
953+ return ! ! getCodeMirrorInputView ( target ) ?. state . readOnly ;
926954}
927955
928956function dismissReadOnlyQuickToolsInput ( view ) {
@@ -931,10 +959,10 @@ function dismissReadOnlyQuickToolsInput(view) {
931959}
932960
933961function restoreQuickToolsTargetFocus ( ) {
934- const { editor } = editorManager ;
935- if ( isCodeMirrorEditorInput ( input ) && editor ) {
936- if ( dismissReadOnlyQuickToolsInput ( editor ) ) return ;
937- focusEditorIfEditable ( editor ) ;
962+ const codeMirrorView = getCodeMirrorInputView ( input ) ;
963+ if ( codeMirrorView ) {
964+ if ( dismissReadOnlyQuickToolsInput ( codeMirrorView ) ) return ;
965+ focusEditorIfEditable ( codeMirrorView ) ;
938966 return ;
939967 }
940968 if ( input ) {
0 commit comments