Skip to content

Commit 7b4ae4e

Browse files
committed
Add button to revert p5.sound version after a change
1 parent de95b13 commit 7b4ae4e

File tree

5 files changed

+47
-7
lines changed

5 files changed

+47
-7
lines changed

client/modules/IDE/components/Editor/index.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@ class Editor extends React.Component {
379379
updateFileContent(id, src) {
380380
const file = this._docs[id];
381381
if (file) {
382-
console.log(this._docs[id]);
383382
this._docs[id] = CodeMirror.Doc(src, this._docs[id].modeOption);
384383
if (id === this.props.file.id) {
385384
this._cm.swapDoc(this._docs[id]);

client/modules/IDE/components/Preferences/index.jsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ import {
1919
setLinewrap,
2020
setPreferencesTab
2121
} from '../../actions/preferences';
22-
import { useP5Version } from '../../hooks/useP5Version';
22+
import { p5SoundURL, useP5Version } from '../../hooks/useP5Version';
2323
import VersionPicker from '../VersionPicker';
2424
import { updateFileContent } from '../../actions/files';
2525
import { CmControllerContext } from '../../pages/IDEView';
26+
import Button from '../../../../common/Button';
2627

2728
export default function Preferences() {
2829
const { t } = useTranslation();
@@ -45,6 +46,7 @@ export default function Preferences() {
4546

4647
const [state, setState] = useState({ fontSize });
4748
const { versionInfo, indexID } = useP5Version();
49+
const [lastP5SoundURL, setLastP5SoundURL] = useState(undefined);
4850
const cmRef = useContext(CmControllerContext);
4951

5052
function onFontInputChange(event) {
@@ -526,7 +528,15 @@ export default function Preferences() {
526528
</label>
527529
<input
528530
type="radio"
529-
onChange={() => updateHTML(versionInfo.setP5Sound(false))}
531+
onChange={() => {
532+
// If the previous p5.sound.js script tag is not the
533+
// default version that one will get via this toggle,
534+
// record it so we can give the option to put it back
535+
if (versionInfo.p5SoundURL !== p5SoundURL) {
536+
setLastP5SoundURL(versionInfo.p5SoundURL);
537+
}
538+
updateHTML(versionInfo.setP5Sound(false));
539+
}}
530540
aria-label={t('Preferences.AutosaveOffARIA')}
531541
name="soundaddon"
532542
id="soundaddon-off"
@@ -540,6 +550,16 @@ export default function Preferences() {
540550
>
541551
{t('Preferences.Off')}
542552
</label>
553+
{lastP5SoundURL && (
554+
<Button
555+
onClick={() => {
556+
updateHTML(versionInfo.setP5SoundURL(lastP5SoundURL));
557+
setLastP5SoundURL(undefined);
558+
}}
559+
>
560+
{t('Preferences.UndoSoundVersion')}
561+
</Button>
562+
)}
543563
</div>
544564
</div>
545565
<div className="preference">

client/modules/IDE/hooks/useP5Version.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ export const p5Versions = [
132132
'0.2.1'
133133
];
134134

135-
export const p5SoundURL =
136-
'https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.11.1/addons/p5.sound.min.js';
135+
export const currentP5Version = p5Versions[0];
136+
137+
export const p5SoundURL = `https://cdnjs.cloudflare.com/ajax/libs/p5.js/${currentP5Version}/addons/p5.sound.min.js`;
137138
export const p5PreloadAddonURL = 'https://TODO/preload.js';
138139
export const p5ShapesAddonURL = 'https://TODO/shapes.js';
139140

@@ -196,7 +197,12 @@ export function useP5Version() {
196197

197198
const p5SoundNode = [
198199
...dom.documentElement.querySelectorAll('script')
199-
].find((s) => s.getAttribute('src') === p5SoundURL);
200+
].find((s) =>
201+
[
202+
/^https?:\/\/cdnjs.cloudflare.com\/ajax\/libs\/p5.js\/(.+)\/addons\/p5\.sound\.min\.js$/,
203+
/^https?:\/\/cdn.jsdelivr.net\/npm\/p5@(.+)\/lib\/addons\/p5\.sound\.min\.js$/
204+
].some((regex) => regex.exec(s.getAttribute('src') || ''))
205+
);
200206
const setP5Sound = function (enabled) {
201207
if (!enabled && p5SoundNode) {
202208
p5SoundNode.parentNode.removeChild(p5SoundNode);
@@ -208,6 +214,17 @@ export function useP5Version() {
208214
return serializeResult();
209215
};
210216

217+
const setP5SoundURL = function (url) {
218+
if (p5SoundNode) {
219+
p5SoundNode.setAttribute('src', url);
220+
} else {
221+
const newNode = document.createElement('script');
222+
newNode.setAttribute('src', url);
223+
scriptNode.parentNode.insertBefore(newNode, scriptNode.nextSibling);
224+
}
225+
return serializeResult();
226+
};
227+
211228
const p5PreloadAddonNode = [
212229
...dom.documentElement.querySelectorAll('script')
213230
].find((s) => s.getAttribute('src') === p5PreloadAddonURL);
@@ -242,6 +259,8 @@ export function useP5Version() {
242259
replaceVersion,
243260
p5Sound: !!p5SoundNode,
244261
setP5Sound,
262+
setP5SoundURL,
263+
p5SoundURL: p5SoundNode?.getAttribute('src'),
245264
p5PreloadAddon: !!p5PreloadAddonNode,
246265
setP5PreloadAddon,
247266
p5ShapesAdddon: !!p5ShapesAddonNode,

client/styles/components/_preferences.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ input[type="number"]::-webkit-outer-spin-button {
174174
.preference__options {
175175
display: flex;
176176
justify-content: center;
177+
align-items: center;
177178
}
178179

179180
.preference__radio-button:checked + .preference__option {

translations/locales/en-US/translations.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@
219219
"CustomVersionReset": "If you do want to use the default libraries, you can replace your script tags in index.html to the following:",
220220
"SoundAddon": "p5.sound.js Addon",
221221
"PreloadAddon": "p5.js 2.0 Addon - Preload",
222-
"ShapesAddon": "p5.js 2.0 Addon - Shapes"
222+
"ShapesAddon": "p5.js 2.0 Addon - Shapes",
223+
"UndoSoundVersion": "Put back previous p5.sound.js version"
223224
},
224225
"KeyboardShortcuts": {
225226
"Title": " Keyboard Shortcuts",

0 commit comments

Comments
 (0)