From ef035df09a0339651dfd382ee82b0061d2acef34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Som=C4=93=20Cho?= Date: Wed, 31 May 2023 21:01:17 +0200 Subject: [PATCH 1/4] fix bug #57 --- src/App.integration.test.jsx | 22 ++++++++++++++++++++++ src/components/Editor.jsx | 5 ++++- src/lib/p5/index.js | 18 ++++++++++++++---- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/App.integration.test.jsx b/src/App.integration.test.jsx index d86cc7c..e9d99a9 100644 --- a/src/App.integration.test.jsx +++ b/src/App.integration.test.jsx @@ -196,5 +196,27 @@ describe('App integration test', () => { expect(await page.$eval('#editor-console', e => e.children[0].children.length)) .toBe(0) }) + it('tests that user state is clean between sketches', async () => { + await page.click('.cm-editor') + await page.keyboard.down('Control') + await page.keyboard.press('KeyA') + await page.keyboard.up('Control') + await page.keyboard.press('Backspace') + await page.keyboard.type('(defn setup[] (println "HELLO"))') + await page.click('#run-btn') + expect(await page.$eval('#editor-console', e => e.children[0].children.length)) + .toBe(1) + + await page.click('.cm-editor') + await page.keyboard.down('Control') + await page.keyboard.press('KeyA') + await page.keyboard.up('Control') + await page.keyboard.press('Backspace') + await page.keyboard.type('(println "hello")') + await page.click('#run-btn') + expect(await page.$eval('#editor-console', e => e.children[0].children.length)) + .toBe(1) + + }) }) diff --git a/src/components/Editor.jsx b/src/components/Editor.jsx index d6782cb..4b5ab7a 100644 --- a/src/components/Editor.jsx +++ b/src/components/Editor.jsx @@ -1,7 +1,7 @@ import CodeMirror from '@uiw/react-codemirror' import { clojure } from '@nextjournal/lang-clojure' import { useEffect, useState } from 'react' -import { clearWindowGlobals, p5Methods, assignWindowGlobals, createP5ScriptTag, removeElementById } from '../lib/p5' +import { clearWindowGlobals, p5Methods, assignWindowGlobals, removeElementById, clearCljsUserGlobals } from '../lib/p5' import { useSearchParams } from 'react-router-dom' import { defaultSketch, compile } from '../lib/cljs' import { encode, decode } from "../lib/compression" @@ -93,6 +93,9 @@ const Editor = ({ setMethods }) => { } function stop() { + if(window.cljs.user){ + clearCljsUserGlobals() + } clearWindowGlobals(); removeElementById("defaultCanvas0") } diff --git a/src/lib/p5/index.js b/src/lib/p5/index.js index 8a8a15a..75637b0 100644 --- a/src/lib/p5/index.js +++ b/src/lib/p5/index.js @@ -47,13 +47,23 @@ export function clearWindowGlobals() { })) } +/** + * Sets all cljs.user functions (e.g. setup, draw) back to undefined + */ +export function clearCljsUserGlobals() { + Object.keys(window.cljs.user).forEach(key => { + window.cljs.user[key] = undefined; + }) +} + + /** * Creates a script element with P5 CDN and returns it. * @returns {string} html literal */ -export function createP5ScriptTag(){ +export function createP5ScriptTag() { const script = document.createElement("script") - script.setAttribute("id","p5-cdn") + script.setAttribute("id", "p5-cdn") script.setAttribute("src", "https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.min.js") return script; } @@ -62,9 +72,9 @@ export function createP5ScriptTag(){ * Removes an element from the DOM if it exists. * @param {string} id - id of the element to be removed from the dom */ -export function removeElementById(id){ +export function removeElementById(id) { let elt = document.getElementById(id) - if(elt){ + if (elt) { elt.remove() } } From 6b6d26a53610938541901b7c82da17528176bd46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Som=C4=93=20Cho?= Date: Wed, 31 May 2023 21:02:24 +0200 Subject: [PATCH 2/4] remove unused state --- src/components/Editor.jsx | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/components/Editor.jsx b/src/components/Editor.jsx index 4b5ab7a..9c5d47c 100644 --- a/src/components/Editor.jsx +++ b/src/components/Editor.jsx @@ -13,7 +13,6 @@ import EditorConsole from './EditorConsole' const Editor = ({ setMethods }) => { const [source, setSource] = useState("") const [urlParams, setUrlParams] = useSearchParams(); - const [error, setError] = useState(null); const [consoleOpen, setConsoleOpen] = useState(false); const [logs, setLogs] = useState([]) const [numErrors, setNumErrors] = useState(0) @@ -56,9 +55,7 @@ const Editor = ({ setMethods }) => { document.getElementById('editor').appendChild(script) assignWindowGlobals() - setError("") } else { - setError(compileResult.cause.message) console.error(compileResult.cause.message) } @@ -93,7 +90,7 @@ const Editor = ({ setMethods }) => { } function stop() { - if(window.cljs.user){ + if (window.cljs.user) { clearCljsUserGlobals() } clearWindowGlobals(); From 0c49475633a26521ba6416767411f23a22f194f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Som=C4=93=20Cho?= Date: Wed, 31 May 2023 21:03:14 +0200 Subject: [PATCH 3/4] bump 0.14.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2863b4d..24dd86d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "p5-cljs-web-editor", "private": true, - "version": "0.14.1", + "version": "0.14.2", "type": "module", "scripts": { "dev": "vite", From afec561b67483c7d823a4b1bf800b1d524a95c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Som=C4=93=20Cho?= Date: Wed, 31 May 2023 21:05:57 +0200 Subject: [PATCH 4/4] =?UTF-8?q?=E2=9C=8D=20update=20changelog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 36d4be6..4faed12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.14.2 +- fix bug #57 where the setup and draw functions continue to persist even after + stop ## 0.14.1 - add longer run time for a test case that takes longer to run