@@ -19,13 +19,17 @@ codeInput.plugins.AutoCloseBrackets = class extends codeInput.Plugin {
1919
2020 /* Add keystroke events */
2121 afterElementsAdded ( codeInput ) {
22- codeInput . textareaElement . addEventListener ( 'keydown' , ( event ) => { this . checkBackspace ( codeInput , event ) } ) ;
23- codeInput . textareaElement . addEventListener ( 'beforeinput' , ( event ) => { this . checkBrackets ( codeInput , event ) ; } ) ;
22+ codeInput . pluginData . autoCloseBrackets = { automatedKeypresses : false } ;
23+ codeInput . textareaElement . addEventListener ( 'keydown' , ( event ) => { this . checkBackspace ( codeInput , event ) ; } ) ;
24+ codeInput . textareaElement . addEventListener ( 'beforeinput' , ( event ) => { this . checkClosingBracket ( codeInput , event ) ; } ) ;
25+ codeInput . textareaElement . addEventListener ( 'input' , ( event ) => { this . checkOpeningBracket ( codeInput , event ) ; } ) ;
2426 }
2527
26- /* Deal with the automatic creation of closing bracket when opening brackets are typed, and the ability to "retype" a closing
27- bracket where one has already been placed. */
28- checkBrackets ( codeInput , event ) {
28+ /* Deal with the ability to "retype" a closing bracket where one has already
29+ been placed. Runs before input so newly typing a closing bracket can be
30+ prevented.*/
31+ checkClosingBracket ( codeInput , event ) {
32+ if ( codeInput . pluginData . autoCloseBrackets . automatedKeypresses ) return ;
2933 if ( event . data == codeInput . textareaElement . value [ codeInput . textareaElement . selectionStart ] ) {
3034 // Check if a closing bracket is typed
3135 for ( let openingBracket in this . bracketPairs ) {
@@ -37,18 +41,30 @@ codeInput.plugins.AutoCloseBrackets = class extends codeInput.Plugin {
3741 break ;
3842 }
3943 }
40- } else if ( event . data in this . bracketPairs ) {
44+ }
45+ }
46+
47+ /* Deal with the automatic creation of closing bracket when opening brackets are typed. Runs after input for consistency between browsers. */
48+ checkOpeningBracket ( codeInput , event ) {
49+ if ( codeInput . pluginData . autoCloseBrackets . automatedKeypresses ) return ;
50+ if ( event . data in this . bracketPairs ) {
4151 // Opening bracket typed; Create bracket pair
4252 let closingBracket = this . bracketPairs [ event . data ] ;
4353 // Insert the closing bracket
54+ // automatedKeypresses property to prevent keypresses being captured
55+ // by this plugin during automated input as some browsers
56+ // (e.g. GNOME Web) do.
57+ codeInput . pluginData . autoCloseBrackets . automatedKeypresses = true ;
4458 document . execCommand ( "insertText" , false , closingBracket ) ;
59+ codeInput . pluginData . autoCloseBrackets . automatedKeypresses = false ;
4560 // Move caret before the inserted closing bracket
4661 codeInput . textareaElement . selectionStart = codeInput . textareaElement . selectionEnd -= 1 ;
4762 }
4863 }
4964
5065 /* Deal with cases where a backspace deleting an opening bracket deletes the closing bracket straight after it as well */
5166 checkBackspace ( codeInput , event ) {
67+ if ( codeInput . pluginData . autoCloseBrackets . automatedKeypresses ) return ;
5268 if ( event . key == "Backspace" && codeInput . textareaElement . selectionStart == codeInput . textareaElement . selectionEnd ) {
5369 let closingBracket = this . bracketPairs [ codeInput . textareaElement . value [ codeInput . textareaElement . selectionStart - 1 ] ] ;
5470 if ( closingBracket != undefined && codeInput . textareaElement . value [ codeInput . textareaElement . selectionStart ] == closingBracket ) {
@@ -58,4 +74,4 @@ codeInput.plugins.AutoCloseBrackets = class extends codeInput.Plugin {
5874 }
5975 }
6076 }
61- }
77+ }
0 commit comments