Skip to content

Commit f53f7b0

Browse files
committed
Refactor defaultHTML to accept options object
Convert defaultHTML from a constant string to a function that accepts an options object for version and add-on library configuration. This enables URL parameter parsing for library selection. Maintains backward compatibility - calling defaultHTML() with no arguments produces the same output as before.
1 parent 07c9c71 commit f53f7b0

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

client/modules/IDE/reducers/files.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const initialState = () => {
3232
},
3333
{
3434
name: 'index.html',
35-
content: defaultHTML,
35+
content: defaultHTML(),
3636
id: b,
3737
_id: b,
3838
fileType: 'file',

server/domain-objects/createDefaultFiles.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,35 @@ function draw() {
88
background(220);
99
}`;
1010

11-
export const defaultHTML = `<!DOCTYPE html>
11+
export function defaultHTML({
12+
version = currentP5Version,
13+
sound = true,
14+
preload = false,
15+
shapes = false,
16+
data = false
17+
} = {}) {
18+
const soundURL = version.startsWith('2.')
19+
? `https://cdn.jsdelivr.net/npm/p5.sound@0.2.0/dist/p5.sound.min.js`
20+
: `https://cdnjs.cloudflare.com/ajax/libs/p5.js/${version}/addons/p5.sound.min.js`;
21+
22+
const libraries = [
23+
`<script src="https://cdn.jsdelivr.net/npm/p5@${version}/lib/p5.js"></script>`,
24+
sound ? `<script src="${soundURL}"></script>` : '',
25+
preload
26+
? `<script src="https://cdn.jsdelivr.net/npm/p5.js-compatibility@0.1.2/src/preload.js"></script>`
27+
: '',
28+
shapes
29+
? `<script src="https://cdn.jsdelivr.net/npm/p5.js-compatibility@0.1.2/src/shapes.js"></script>`
30+
: '',
31+
data
32+
? `<script src="https://cdn.jsdelivr.net/npm/p5.js-compatibility@0.1.2/src/data.js"></script>`
33+
: ''
34+
].join('\n ');
35+
36+
return `<!DOCTYPE html>
1237
<html lang="en">
1338
<head>
14-
<script src="https://cdn.jsdelivr.net/npm/p5@${currentP5Version}/lib/p5.js"></script>
15-
<script src="https://cdn.jsdelivr.net/npm/p5@${currentP5Version}/lib/addons/p5.sound.min.js"></script>
39+
${libraries}
1640
<link rel="stylesheet" type="text/css" href="style.css">
1741
<meta charset="utf-8" />
1842
@@ -24,6 +48,7 @@ export const defaultHTML = `<!DOCTYPE html>
2448
</body>
2549
</html>
2650
`;
51+
}
2752

2853
export const defaultCSS = `html, body {
2954
margin: 0;
@@ -37,7 +62,7 @@ canvas {
3762
export default function createDefaultFiles() {
3863
return {
3964
'index.html': {
40-
content: defaultHTML
65+
content: defaultHTML()
4166
},
4267
'style.css': {
4368
content: defaultCSS

0 commit comments

Comments
 (0)