@@ -11,6 +11,7 @@ const IFRAME_STYLE = [
1111let myScriptEl = getMyScriptEl ( ) as HTMLScriptElement ;
1212let myBaseURL = getMyBaseURL ( myScriptEl . src ) ;
1313let autoload = ! myScriptEl . hasAttribute ( 'data-manual' ) ;
14+ let nextId = 1 ;
1415
1516function getMyBaseURL ( url : string ) {
1617 return url . slice ( 0 , - MY_FILENAME . length ) ;
@@ -52,7 +53,10 @@ function replaceScriptWithWidget(el: HTMLScriptElement) {
5253 let p5version = el . getAttribute ( 'data-p5-version' ) ;
5354 let autoplay = el . hasAttribute ( 'data-autoplay' ) ;
5455 let url ;
55- let qsArgs = [ 'sketch=' + encodeURIComponent ( el . textContent ) ] ;
56+ let qsArgs = [
57+ 'sketch=' + encodeURIComponent ( el . textContent ) ,
58+ 'id=' + encodeURIComponent ( el . getAttribute ( 'data-id' ) )
59+ ] ;
5660 let style = IFRAME_STYLE . slice ( ) ;
5761
5862 if ( ! isNaN ( previewWidth ) && previewWidth >= 0 ) {
@@ -100,19 +104,28 @@ function whenVisible(el: HTMLScriptElement,
100104 maybeMakeVisible ( ) ;
101105}
102106
107+ function lazilyReplaceScriptWithWidget ( el : HTMLScriptElement ) {
108+ let height = getDataHeight ( el ) ;
109+
110+ el . style . display = 'block' ;
111+ el . style . fontSize = '0' ;
112+ el . style . width = '100%' ;
113+ el . style . minHeight = height + 'px' ;
114+ el . style . background = '#f0f0f0' ;
115+
116+ if ( ! el . hasAttribute ( 'data-id' ) ) {
117+ el . setAttribute ( 'data-id' , nextId . toString ( ) ) ;
118+ nextId ++ ;
119+ }
120+
121+ whenVisible ( el , replaceScriptWithWidget ) ;
122+ }
123+
103124function lazilyReplaceAllScriptsWithWidget ( ) {
104125 let scripts = document . querySelectorAll ( "script[type='text/p5']" ) ;
105126
106127 [ ] . slice . call ( scripts ) . forEach ( ( el : HTMLScriptElement ) => {
107- let height = getDataHeight ( el ) ;
108-
109- el . style . display = 'block' ;
110- el . style . fontSize = '0' ;
111- el . style . width = '100%' ;
112- el . style . minHeight = height + 'px' ;
113- el . style . background = '#f0f0f0' ;
114-
115- whenVisible ( el , replaceScriptWithWidget ) ;
128+ lazilyReplaceScriptWithWidget ( el ) ;
116129 } ) ;
117130}
118131
@@ -131,7 +144,7 @@ if (autoload) {
131144window [ 'p5Widget' ] = {
132145 baseURL : myBaseURL ,
133146 url : myBaseURL + MY_FILENAME ,
134- replaceScript : replaceScriptWithWidget ,
147+ replaceScript : lazilyReplaceScriptWithWidget ,
135148 replaceAll : lazilyReplaceAllScriptsWithWidget ,
136149 defaults : defaults
137150} ;
0 commit comments