@@ -482,6 +482,7 @@ var codeInput = {
482482 * to syntax-highlight it. */
483483
484484 needsHighlight = false ; // Just inputted
485+ passEventsToTextarea = true ; // Turn to false when unusual internal events are called on the textarea
485486
486487 /**
487488 * Highlight the code as soon as possible
@@ -494,17 +495,6 @@ var codeInput = {
494495 * Call an animation frame
495496 */
496497 animateFrame ( ) {
497- // Synchronise the size of the pre/code and textarea elements
498- if ( this . template . preElementStyled ) {
499- this . style . backgroundColor = getComputedStyle ( this . preElement ) . backgroundColor ;
500- this . textareaElement . style . height = getComputedStyle ( this . preElement ) . height ;
501- this . textareaElement . style . width = getComputedStyle ( this . preElement ) . width ;
502- } else {
503- this . style . backgroundColor = getComputedStyle ( this . codeElement ) . backgroundColor ;
504- this . textareaElement . style . height = getComputedStyle ( this . codeElement ) . height ;
505- this . textareaElement . style . width = getComputedStyle ( this . codeElement ) . width ;
506- }
507-
508498 // Synchronise the contents of the pre/code and textarea elements
509499 if ( this . needsHighlight ) {
510500 this . update ( ) ;
@@ -534,6 +524,22 @@ var codeInput = {
534524 if ( this . template . includeCodeInputInHighlightFunc ) this . template . highlight ( resultElement , this ) ;
535525 else this . template . highlight ( resultElement ) ;
536526
527+ // Synchronise the size of the pre/code and textarea elements
528+ if ( this . template . preElementStyled ) {
529+ this . style . backgroundColor = getComputedStyle ( this . preElement ) . backgroundColor ;
530+ this . textareaElement . style . height = getComputedStyle ( this . preElement ) . height ;
531+ this . textareaElement . style . width = getComputedStyle ( this . preElement ) . width ;
532+ } else {
533+ this . style . backgroundColor = getComputedStyle ( this . codeElement ) . backgroundColor ;
534+ this . textareaElement . style . height = getComputedStyle ( this . codeElement ) . height ;
535+ this . textareaElement . style . width = getComputedStyle ( this . codeElement ) . width ;
536+ }
537+ // Scroll to the caret by focusing, though this shouldn't count as a focus event
538+ this . passEventsToTextarea = false ;
539+ this . textareaElement . blur ( ) ;
540+ this . textareaElement . focus ( ) ;
541+ this . passEventsToTextarea = true ;
542+
537543 this . pluginEvt ( "afterHighlight" ) ;
538544 }
539545
@@ -737,7 +743,7 @@ var codeInput = {
737743 if ( this . template . preElementStyled ) this . classList . add ( "code-input_pre-element-styled" ) ;
738744 else this . classList . remove ( "code-input_pre-element-styled" ) ;
739745 // Syntax Highlight
740- this . needsHighlight = true ;
746+ this . scheduleHighlight ( ) ;
741747
742748 break ;
743749
@@ -769,7 +775,7 @@ var codeInput = {
769775
770776 if ( mainTextarea . placeholder == oldValue ) mainTextarea . placeholder = newValue ;
771777
772- this . needsHighlight = true ;
778+ this . scheduleHighlight ( ) ;
773779
774780 break ;
775781 default :
@@ -806,8 +812,9 @@ var codeInput = {
806812 * @override
807813 */
808814 addEventListener ( type , listener , options = undefined ) {
809- // Save a copy of the callback where `this` refers to the code-input element
810- let boundCallback = listener . bind ( this ) ;
815+ // Save a copy of the callback where `this` refers to the code-input element.
816+ // This callback is modified to only run when the passEventsToTextarea is set.
817+ let boundCallback = function ( evt ) { if ( this . passEventsToTextarea ) listener ( evt ) ; } . bind ( this ) ;
811818 this . boundEventCallbacks [ listener ] = boundCallback ;
812819
813820 if ( codeInput . textareaSyncEvents . includes ( type ) ) {
@@ -885,7 +892,7 @@ var codeInput = {
885892 // Save in editable textarea element
886893 this . textareaElement . value = val ;
887894 // Trigger highlight
888- this . needsHighlight = true ;
895+ this . scheduleHighlight ( ) ;
889896 return val ;
890897 }
891898
0 commit comments