@@ -482,6 +482,7 @@ var codeInput = {
482
482
* to syntax-highlight it. */
483
483
484
484
needsHighlight = false ; // Just inputted
485
+ passEventsToTextarea = true ; // Turn to false when unusual internal events are called on the textarea
485
486
486
487
/**
487
488
* Highlight the code as soon as possible
@@ -494,17 +495,6 @@ var codeInput = {
494
495
* Call an animation frame
495
496
*/
496
497
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
-
508
498
// Synchronise the contents of the pre/code and textarea elements
509
499
if ( this . needsHighlight ) {
510
500
this . update ( ) ;
@@ -534,6 +524,22 @@ var codeInput = {
534
524
if ( this . template . includeCodeInputInHighlightFunc ) this . template . highlight ( resultElement , this ) ;
535
525
else this . template . highlight ( resultElement ) ;
536
526
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
+
537
543
this . pluginEvt ( "afterHighlight" ) ;
538
544
}
539
545
@@ -737,7 +743,7 @@ var codeInput = {
737
743
if ( this . template . preElementStyled ) this . classList . add ( "code-input_pre-element-styled" ) ;
738
744
else this . classList . remove ( "code-input_pre-element-styled" ) ;
739
745
// Syntax Highlight
740
- this . needsHighlight = true ;
746
+ this . scheduleHighlight ( ) ;
741
747
742
748
break ;
743
749
@@ -769,7 +775,7 @@ var codeInput = {
769
775
770
776
if ( mainTextarea . placeholder == oldValue ) mainTextarea . placeholder = newValue ;
771
777
772
- this . needsHighlight = true ;
778
+ this . scheduleHighlight ( ) ;
773
779
774
780
break ;
775
781
default :
@@ -806,8 +812,9 @@ var codeInput = {
806
812
* @override
807
813
*/
808
814
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 ) ;
811
818
this . boundEventCallbacks [ listener ] = boundCallback ;
812
819
813
820
if ( codeInput . textareaSyncEvents . includes ( type ) ) {
@@ -885,7 +892,7 @@ var codeInput = {
885
892
// Save in editable textarea element
886
893
this . textareaElement . value = val ;
887
894
// Trigger highlight
888
- this . needsHighlight = true ;
895
+ this . scheduleHighlight ( ) ;
889
896
return val ;
890
897
}
891
898
0 commit comments