1
1
/* eslint-disable func-names */
2
- import React , { useContext , useMemo , useState } from 'react' ;
2
+ import React , { useContext , useMemo } from 'react' ;
3
3
import { useSelector } from 'react-redux' ;
4
4
import PropTypes from 'prop-types' ;
5
+ import { currentP5Version , p5Versions } from '../../../../common/p5Versions' ;
5
6
6
- // Generated from https://www.npmjs.com/package/p5?activeTab=versions
7
- // Run this in the console:
8
- // JSON.stringify([...document.querySelectorAll('._132722c7')].map(n => n.innerText), null, 2)
9
- // TODO: use their API for this to grab these at build time?
10
- export const p5Versions = [
11
- '2.0.3' ,
12
- '2.0.2' ,
13
- '2.0.1' ,
14
- '2.0.0' ,
15
- '1.11.8' ,
16
- '1.11.7' ,
17
- '1.11.6' ,
18
- '1.11.5' ,
19
- '1.11.4' ,
20
- '1.11.3' ,
21
- '1.11.2' ,
22
- '1.11.1' ,
23
- '1.11.0' ,
24
- '1.10.0' ,
25
- '1.9.4' ,
26
- '1.9.3' ,
27
- '1.9.2' ,
28
- '1.9.1' ,
29
- '1.9.0' ,
30
- '1.8.0' ,
31
- '1.7.0' ,
32
- '1.6.0' ,
33
- '1.5.0' ,
34
- '1.4.2' ,
35
- '1.4.1' ,
36
- '1.4.0' ,
37
- '1.3.1' ,
38
- '1.3.0' ,
39
- '1.2.0' ,
40
- '1.1.9' ,
41
- '1.1.8' ,
42
- '1.1.7' ,
43
- '1.1.5' ,
44
- '1.1.4' ,
45
- '1.1.3' ,
46
- '1.1.2' ,
47
- '1.1.1' ,
48
- '1.1.0' ,
49
- '1.0.0' ,
50
- '0.10.2' ,
51
- '0.10.1' ,
52
- '0.10.0' ,
53
- '0.9.0' ,
54
- '0.8.0' ,
55
- '0.7.3' ,
56
- '0.7.2' ,
57
- '0.7.1' ,
58
- '0.7.0' ,
59
- '0.6.1' ,
60
- '0.6.0' ,
61
- '0.5.16' ,
62
- '0.5.15' ,
63
- '0.5.14' ,
64
- '0.5.13' ,
65
- '0.5.12' ,
66
- '0.5.11' ,
67
- '0.5.10' ,
68
- '0.5.9' ,
69
- '0.5.8' ,
70
- '0.5.7' ,
71
- '0.5.6' ,
72
- '0.5.5' ,
73
- '0.5.4' ,
74
- '0.5.3' ,
75
- '0.5.2' ,
76
- '0.5.1' ,
77
- '0.5.0' ,
78
- '0.4.24' ,
79
- '0.4.23' ,
80
- '0.4.22' ,
81
- '0.4.21' ,
82
- '0.4.20' ,
83
- '0.4.19' ,
84
- '0.4.18' ,
85
- '0.4.17' ,
86
- '0.4.16' ,
87
- '0.4.15' ,
88
- '0.4.14' ,
89
- '0.4.13' ,
90
- '0.4.12' ,
91
- '0.4.11' ,
92
- '0.4.10' ,
93
- '0.4.9' ,
94
- '0.4.8' ,
95
- '0.4.7' ,
96
- '0.4.6' ,
97
- '0.4.5' ,
98
- '0.4.4' ,
99
- '0.4.3' ,
100
- '0.4.2' ,
101
- '0.4.1' ,
102
- '0.4.0' ,
103
- '0.3.16' ,
104
- '0.3.15' ,
105
- '0.3.14' ,
106
- '0.3.13' ,
107
- '0.3.12' ,
108
- '0.3.11' ,
109
- '0.3.10' ,
110
- '0.3.9' ,
111
- '0.3.8' ,
112
- '0.3.7' ,
113
- '0.3.6' ,
114
- '0.3.5' ,
115
- '0.3.4' ,
116
- '0.3.3' ,
117
- '0.3.2' ,
118
- '0.3.1' ,
119
- '0.3.0' ,
120
- '0.2.23' ,
121
- '0.2.22' ,
122
- '0.2.21' ,
123
- '0.2.20' ,
124
- '0.2.19' ,
125
- '0.2.18' ,
126
- '0.2.17' ,
127
- '0.2.16' ,
128
- '0.2.15' ,
129
- '0.2.14' ,
130
- '0.2.13' ,
131
- '0.2.12' ,
132
- '0.2.11' ,
133
- '0.2.10' ,
134
- '0.2.9' ,
135
- '0.2.8' ,
136
- '0.2.7' ,
137
- '0.2.6' ,
138
- '0.2.5' ,
139
- '0.2.4' ,
140
- '0.2.3' ,
141
- '0.2.2' ,
142
- '0.2.1'
143
- ] ;
7
+ export const majorVersion = ( version ) => version . split ( '.' ) [ 0 ] ;
144
8
145
- export const currentP5Version = '1.11.5' ; // Don't update to 2.x until 2026
146
-
147
- export const p5SoundURLOld = `https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.11.3/addons/p5.sound.min.js` ;
9
+ export const p5SoundURLOldTemplate =
10
+ 'https://cdnjs.cloudflare.com/ajax/libs/p5.js/$VERSION/addons/p5.sound.min.js' ;
11
+ export const p5SoundURLOld = p5SoundURLOldTemplate . replace (
12
+ '$VERSION' ,
13
+ currentP5Version
14
+ ) ;
148
15
export const p5SoundURL =
149
16
'https://cdn.jsdelivr.net/npm/[email protected] /dist/p5.sound.min.js' ;
150
17
export const p5PreloadAddonURL =
@@ -168,8 +35,6 @@ export function P5VersionProvider(props) {
168
35
const indexSrc = indexFile ?. content ;
169
36
const indexID = indexFile ?. id ;
170
37
171
- const [ lastP5SoundURL , setLastP5SoundURL ] = useState ( undefined ) ;
172
-
173
38
// { version: string, minified: boolean, replaceVersion: (version: string) => string } | null
174
39
const versionInfo = useMemo ( ( ) => {
175
40
if ( ! indexSrc ) return null ;
@@ -207,14 +72,6 @@ export function P5VersionProvider(props) {
207
72
// We only know for certain which one we've got if
208
73
if ( usedP5Versions . length === 1 ) {
209
74
const { version, minified, scriptNode } = usedP5Versions [ 0 ] ;
210
- const replaceVersion = function ( newVersion ) {
211
- const file = minified ? 'p5.min.js' : 'p5.js' ;
212
- scriptNode . setAttribute (
213
- 'src' ,
214
- `https://cdn.jsdelivr.net/npm/p5@${ newVersion } /lib/${ file } `
215
- ) ;
216
- return serializeResult ( ) ;
217
- } ;
218
75
219
76
const p5SoundNode = [
220
77
...dom . documentElement . querySelectorAll ( 'script' )
@@ -232,7 +89,9 @@ export function P5VersionProvider(props) {
232
89
const newNode = document . createElement ( 'script' ) ;
233
90
newNode . setAttribute (
234
91
'src' ,
235
- version . startsWith ( '2' ) ? p5SoundURL : p5SoundURLOld
92
+ majorVersion ( version ) === '2'
93
+ ? p5SoundURL
94
+ : p5SoundURLOldTemplate . replace ( '$VERSION' , version )
236
95
) ;
237
96
scriptNode . parentNode . insertBefore ( newNode , scriptNode . nextSibling ) ;
238
97
}
@@ -250,6 +109,31 @@ export function P5VersionProvider(props) {
250
109
return serializeResult ( ) ;
251
110
} ;
252
111
112
+ const replaceVersion = function ( newVersion ) {
113
+ const file = minified ? 'p5.min.js' : 'p5.js' ;
114
+ scriptNode . setAttribute (
115
+ 'src' ,
116
+ `https://cdn.jsdelivr.net/npm/p5@${ newVersion } /lib/${ file } `
117
+ ) ;
118
+
119
+ if ( p5SoundNode ) {
120
+ if ( majorVersion ( version ) !== majorVersion ( newVersion ) ) {
121
+ // Turn off p5.sound if the user switched from 1.x to 2.x
122
+ setP5Sound ( false ) ;
123
+ } else {
124
+ // Replace the existing p5.sound with the one compatible with
125
+ // the new version
126
+ setP5SoundURL (
127
+ majorVersion ( version ) === '2'
128
+ ? p5SoundURL
129
+ : p5SoundURLOldTemplate . replace ( '$VERSION' , newVersion )
130
+ ) ;
131
+ }
132
+ }
133
+
134
+ return serializeResult ( ) ;
135
+ } ;
136
+
253
137
const p5PreloadAddonNode = [
254
138
...dom . documentElement . querySelectorAll ( 'script' )
255
139
] . find ( ( s ) => s . getAttribute ( 'src' ) === p5PreloadAddonURL ) ;
@@ -294,14 +178,13 @@ export function P5VersionProvider(props) {
294
178
295
179
return {
296
180
version,
181
+ isVersion2 : majorVersion ( version ) === '2' ,
297
182
minified,
298
183
replaceVersion,
299
184
p5Sound : ! ! p5SoundNode ,
300
185
setP5Sound,
301
186
setP5SoundURL,
302
187
p5SoundURL : p5SoundNode ?. getAttribute ( 'src' ) ,
303
- lastP5SoundURL,
304
- setLastP5SoundURL,
305
188
p5PreloadAddon : ! ! p5PreloadAddonNode ,
306
189
setP5PreloadAddon,
307
190
p5ShapesAddon : ! ! p5ShapesAddonNode ,
0 commit comments