@@ -513,30 +513,49 @@ var codeInput = {
513513 * the result (pre code) element, then use the template object
514514 * to syntax-highlight it. */
515515
516- /** Update the text value to the result element, after the textarea contents have changed.
517- * @param {string } value - The text value of the code-input element
518- * @param {boolean } originalUpdate - Whether this update originates from the textarea's content; if so, run it first so custom updates override it.
516+ needsHighlight = false ; // Just inputted
517+ needsDisableDuplicateSearching = false ; // Just highlighted
518+
519+ /**
520+ * Highlight the code ASAP
519521 */
520- update ( value ) {
521- // Prevent this from running multiple times on the same input when "value" attribute is changed,
522- // by not running when value is already equal to the input of this (implying update has already
523- // been run). Thank you to peterprvy for this.
524- if ( this . ignoreValueUpdate ) return ;
525-
526- if ( this . textareaElement == null ) {
527- this . addEventListener ( "code-input_load" , ( ) => { this . update ( value ) } ) ; // Only run when fully loaded
528- return ;
522+ scheduleHighlight ( ) {
523+ this . needsHighlight = true ;
524+ }
525+
526+ /**
527+ * Call an animation frame
528+ */
529+ animateFrame ( ) {
530+ // Sync size
531+ this . textareaElement . style . height = getComputedStyle ( this . preElement ) . height ;
532+ this . textareaElement . style . width = getComputedStyle ( this . preElement ) . width ;
533+
534+ // Sync content
535+ if ( this . needsHighlight ) {
536+ console . log ( "Update" ) ;
537+ this . update ( ) ;
538+ this . needsHighlight = false ;
539+ this . needsDisableDuplicateSearching = true ;
540+ }
541+ if ( this . needsDisableDuplicateSearching && this . codeElement . querySelector ( "*" ) != null ) {
542+ // Has been highlighted
543+ this . resultElementDisableSearching ( ) ;
544+ this . needsDisableDuplicateSearching = false ;
529545 }
530546
531- this . ignoreValueUpdate = true ;
532- this . value = value ;
533- this . ignoreValueUpdate = false ;
534- if ( this . textareaElement . value != value ) this . textareaElement . value = value ;
547+ window . requestAnimationFrame ( this . animateFrame . bind ( this ) ) ;
548+ }
535549
550+ /**
551+ * Update the text value to the result element, after the textarea contents have changed.
552+ */
553+ update ( ) {
536554 let resultElement = this . codeElement ;
555+ let value = this . value ;
537556
538557 // Handle final newlines
539- if ( value [ value . length - 1 ] == "\n" ) {
558+ if ( value [ value . length - 1 ] == "\n" || value . length == 0 ) {
540559 value += " " ;
541560 }
542561
@@ -549,37 +568,6 @@ var codeInput = {
549568 else this . template . highlight ( resultElement ) ;
550569
551570 this . pluginEvt ( "afterHighlight" ) ;
552-
553- if ( this . template . autoDisableDuplicateSearching ) {
554- if ( this . codeElement . querySelector ( "*" ) === null ) {
555- // Fix for tries-to-disable-searching-before-highlighting-possible bug:
556- // Wait until user interaction so can expect
557- // highlight before disabling searching
558- let listenerKeydown = window . addEventListener ( "keydown" , ( ) => {
559- this . resultElementDisableSearching ( ) ;
560- window . removeEventListener ( "keydown" , listenerKeydown ) ;
561- window . removeEventListener ( "mousemove" , listenerMousemove ) ;
562- } ) ;
563- let listenerMousemove = window . addEventListener ( "mousemove" , ( ) => {
564- this . resultElementDisableSearching ( ) ;
565- window . removeEventListener ( "keydown" , listenerKeydown ) ;
566- window . removeEventListener ( "mousemove" , listenerMousemove ) ;
567- } ) ;
568- } else {
569- this . resultElementDisableSearching ( ) ;
570- }
571- }
572- }
573-
574- /**
575- * Synchronise the scrolling of the textarea to the result element.
576- */
577- syncScroll ( ) {
578- let inputElement = this . textareaElement ;
579- let resultElement = this . template . preElementStyled ? this . preElement : this . codeElement ;
580-
581- resultElement . scrollTop = inputElement . scrollTop ;
582- resultElement . scrollLeft = inputElement . scrollLeft ;
583571 }
584572
585573 /**
@@ -694,8 +682,7 @@ var codeInput = {
694682 }
695683 } ) ;
696684
697- textarea . addEventListener ( 'input' , ( evt ) => { textarea . parentElement . update ( textarea . value ) ; textarea . parentElement . sync_scroll ( ) ; } ) ;
698- textarea . addEventListener ( 'scroll' , ( evt ) => textarea . parentElement . sync_scroll ( ) ) ;
685+ textarea . addEventListener ( 'input' , ( evt ) => { this . value = this . textareaElement . value ; } ) ;
699686
700687 // Save element internally
701688 this . textareaElement = textarea ;
@@ -720,16 +707,10 @@ var codeInput = {
720707
721708 this . pluginEvt ( "afterElementsAdded" ) ;
722709
723- this . update ( value ) ;
724-
725710 this . dispatchEvent ( new CustomEvent ( "code-input_load" ) ) ;
726- }
727711
728- /**
729- * @deprecated Please use `codeInput.CodeInput.syncScroll`
730- */
731- sync_scroll ( ) {
732- this . syncScroll ( ) ;
712+ this . value = value ;
713+ this . animateFrame ( ) ;
733714 }
734715
735716 /**
@@ -822,7 +803,7 @@ var codeInput = {
822803 if ( this . template . preElementStyled ) this . classList . add ( "code-input_pre-element-styled" ) ;
823804 else this . classList . remove ( "code-input_pre-element-styled" ) ;
824805 // Syntax Highlight
825- this . update ( this . value ) ;
806+ this . needsHighlight = true ;
826807
827808 break ;
828809
@@ -854,7 +835,7 @@ var codeInput = {
854835
855836 if ( mainTextarea . placeholder == oldValue ) mainTextarea . placeholder = newValue ;
856837
857- this . update ( this . value ) ;
838+ this . needsHighlight = true ;
858839
859840 break ;
860841 default :
@@ -954,7 +935,7 @@ var codeInput = {
954935 val = "" ;
955936 }
956937 this . _value = val ;
957- this . update ( val ) ;
938+ this . needsHighlight = true ;
958939 return val ;
959940 }
960941
@@ -1032,7 +1013,7 @@ var codeInput = {
10321013 * Update value on form reset
10331014 */
10341015 formResetCallback ( ) {
1035- this . update ( this . initialValue ) ;
1016+ this . value = this . initialValue ;
10361017 } ;
10371018 } ,
10381019
0 commit comments