@@ -100,25 +100,31 @@ var codeInput = {
100100 throw TypeError ( `code-input: Template for "${ templateName } " invalid, because the plugin provided at index ${ i } is not valid; it is "${ template . plugins [ i ] } ". Please make sure you use one of the constructors in codeInput.templates, and that you provide the correct arguments.` ) ;
101101 }
102102 } ) ;
103+
103104
104105 codeInput . usedTemplates [ templateName ] = template ;
105106 // Add waiting code-input elements wanting this template from queue
106107 if ( templateName in codeInput . templateNotYetRegisteredQueue ) {
107108 for ( let i in codeInput . templateNotYetRegisteredQueue [ templateName ] ) {
108109 elem = codeInput . templateNotYetRegisteredQueue [ templateName ] [ i ] ;
109110 elem . template = template ;
110- codeInput . runOnceWindowLoaded ( ( ) => { elem . setup ( ) ; } , elem ) ; // So innerHTML can be read
111+ codeInput . runOnceWindowLoaded ( ( function ( elem ) { elem . connectedCallback ( ) ; } ) . bind ( null , elem ) , elem ) ;
112+ // Bind sets elem in parameter
113+ // So innerHTML can be read
111114 }
112115 console . log ( `code-input: template: Added existing elements with template ${ templateName } ` ) ;
113116 }
117+
114118 if ( codeInput . defaultTemplate == undefined ) {
115119 codeInput . defaultTemplate = templateName ;
116120 // Add elements with default template from queue
117121 if ( undefined in codeInput . templateNotYetRegisteredQueue ) {
118122 for ( let i in codeInput . templateNotYetRegisteredQueue [ undefined ] ) {
119123 elem = codeInput . templateNotYetRegisteredQueue [ undefined ] [ i ] ;
120124 elem . template = template ;
121- codeInput . runOnceWindowLoaded ( ( ) => { elem . setup ( ) ; } , elem ) ; // So innerHTML can be read
125+ codeInput . runOnceWindowLoaded ( ( function ( elem ) { elem . connectedCallback ( ) ; } ) . bind ( null , elem ) , elem ) ;
126+ // Bind sets elem in parameter
127+ // So innerHTML can be read
122128 }
123129 }
124130 console . log ( `code-input: template: Set template ${ templateName } as default` ) ;
@@ -471,16 +477,16 @@ var codeInput = {
471477 * @param {boolean } originalUpdate - Whether this update originates from the textarea's content; if so, run it first so custom updates override it.
472478 */
473479 update ( value ) {
474- if ( this . textareaElement == null ) {
475- this . addEventListener ( "code-input_load" , ( ) => { this . update ( value ) } ) ; // Only run when fully loaded
476- return ;
477- }
478-
479480 // Prevent this from running multiple times on the same input when "value" attribute is changed,
480481 // by not running when value is already equal to the input of this (implying update has already
481482 // been run). Thank you to peterprvy for this.
482483 if ( this . ignoreValueUpdate ) return ;
483484
485+ if ( this . textareaElement == null ) {
486+ this . addEventListener ( "code-input_load" , ( ) => { this . update ( value ) } ) ; // Only run when fully loaded
487+ return ;
488+ }
489+
484490 this . ignoreValueUpdate = true ;
485491 this . value = value ;
486492 this . ignoreValueUpdate = false ;
@@ -563,6 +569,8 @@ var codeInput = {
563569 * This will be called once the template has been added.
564570 */
565571 setup ( ) {
572+ if ( this . textareaElement != null ) return ; // Already set up
573+
566574 this . classList . add ( "code-input_registered" ) ; // Remove register message
567575 if ( this . template . preElementStyled ) this . classList . add ( "code-input_pre-element-styled" ) ;
568576
@@ -996,13 +1004,19 @@ var codeInput = {
9961004 * has loaded (or now if it has already loaded)
9971005 */
9981006 runOnceWindowLoaded ( callback , codeInputElem ) {
999- if ( codeInputElem . textareaElement != null ) {
1007+ if ( codeInput . windowLoaded ) {
10001008 callback ( ) ; // Fully loaded
10011009 } else {
10021010 window . addEventListener ( "load" , callback ) ;
10031011 }
10041012 } ,
1013+ windowLoaded : false
10051014}
1015+ window . addEventListener ( "load" , function ( ) {
1016+ codeInput . windowLoaded = true ;
1017+ } ) ;
1018+
1019+
10061020/**
10071021 * convert wildcards into regex
10081022 */
0 commit comments