@@ -11,6 +11,7 @@ const IFRAME_STYLE = [
11
11
let myScriptEl = getMyScriptEl ( ) as HTMLScriptElement ;
12
12
let myBaseURL = getMyBaseURL ( myScriptEl . src ) ;
13
13
let autoload = ! myScriptEl . hasAttribute ( 'data-manual' ) ;
14
+ let nextId = 1 ;
14
15
15
16
function getMyBaseURL ( url : string ) {
16
17
return url . slice ( 0 , - MY_FILENAME . length ) ;
@@ -52,7 +53,10 @@ function replaceScriptWithWidget(el: HTMLScriptElement) {
52
53
let p5version = el . getAttribute ( 'data-p5-version' ) ;
53
54
let autoplay = el . hasAttribute ( 'data-autoplay' ) ;
54
55
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
+ ] ;
56
60
let style = IFRAME_STYLE . slice ( ) ;
57
61
58
62
if ( ! isNaN ( previewWidth ) && previewWidth >= 0 ) {
@@ -100,19 +104,28 @@ function whenVisible(el: HTMLScriptElement,
100
104
maybeMakeVisible ( ) ;
101
105
}
102
106
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
+
103
124
function lazilyReplaceAllScriptsWithWidget ( ) {
104
125
let scripts = document . querySelectorAll ( "script[type='text/p5']" ) ;
105
126
106
127
[ ] . 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 ) ;
116
129
} ) ;
117
130
}
118
131
@@ -131,7 +144,7 @@ if (autoload) {
131
144
window [ 'p5Widget' ] = {
132
145
baseURL : myBaseURL ,
133
146
url : myBaseURL + MY_FILENAME ,
134
- replaceScript : replaceScriptWithWidget ,
147
+ replaceScript : lazilyReplaceScriptWithWidget ,
135
148
replaceAll : lazilyReplaceAllScriptsWithWidget ,
136
149
defaults : defaults
137
150
} ;
0 commit comments