diff --git a/repl/package-lock.json b/repl/package-lock.json index abe99f12..8574c551 100644 --- a/repl/package-lock.json +++ b/repl/package-lock.json @@ -20,6 +20,7 @@ "react-codemirror2": "^7.2.1", "react-codemirror6": "^1.1.0", "react-dom": "^17.0.2", + "react-hook-inview": "^4.4.1", "react-scripts": "5.0.0", "tone": "^14.7.77", "web-vitals": "^2.1.4" @@ -31,6 +32,7 @@ "autoprefixer": "^10.4.4", "parcel": "^2.3.1", "postcss": "^8.4.12", + "process": "^0.11.10", "serve": "^13.0.2", "tailwindcss": "^3.0.23" } @@ -16435,6 +16437,15 @@ "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, + "node_modules/process": { + "version": "0.11.10", + "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", + "integrity": "sha1-czIwDoQBYb2j5podHZGn1LwW8YI=", + "dev": true, + "engines": { + "node": ">= 0.6.0" + } + }, "node_modules/process-nextick-args": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", @@ -16861,6 +16872,15 @@ "resolved": "https://registry.npmjs.org/react-error-overlay/-/react-error-overlay-6.0.10.tgz", "integrity": "sha512-mKR90fX7Pm5seCOfz8q9F+66VCc1PGsWSBxKbITjfKVQHMNF2zudxHnMdJiB1fRCb+XsbQV9sO9DCkgsMQgBIA==" }, + "node_modules/react-hook-inview": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/react-hook-inview/-/react-hook-inview-4.4.1.tgz", + "integrity": "sha512-JKx0+aWCna0YUfTCKSaphGbaMp25S/YjpWtfk5VbqhlESesOEAPr7TCGMRLS3Cv6I1mEDhWYIhz2YLHgnHX4Yw==", + "peerDependencies": { + "react": "^16.8.0 || ^17.0.0", + "react-dom": "^16.8.0 || ^17.0.0" + } + }, "node_modules/react-is": { "version": "17.0.2", "resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz", @@ -31994,6 +32014,12 @@ } } }, + "process": { + "version": "0.11.10", + "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", + "integrity": "sha1-czIwDoQBYb2j5podHZGn1LwW8YI=", + "dev": true + }, "process-nextick-args": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", @@ -32317,6 +32343,12 @@ "resolved": "https://registry.npmjs.org/react-error-overlay/-/react-error-overlay-6.0.10.tgz", "integrity": "sha512-mKR90fX7Pm5seCOfz8q9F+66VCc1PGsWSBxKbITjfKVQHMNF2zudxHnMdJiB1fRCb+XsbQV9sO9DCkgsMQgBIA==" }, + "react-hook-inview": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/react-hook-inview/-/react-hook-inview-4.4.1.tgz", + "integrity": "sha512-JKx0+aWCna0YUfTCKSaphGbaMp25S/YjpWtfk5VbqhlESesOEAPr7TCGMRLS3Cv6I1mEDhWYIhz2YLHgnHX4Yw==", + "requires": {} + }, "react-is": { "version": "17.0.2", "resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz", diff --git a/repl/package.json b/repl/package.json index f4e3a9a8..eccac897 100644 --- a/repl/package.json +++ b/repl/package.json @@ -16,6 +16,7 @@ "react-codemirror2": "^7.2.1", "react-codemirror6": "^1.1.0", "react-dom": "^17.0.2", + "react-hook-inview": "^4.4.1", "react-scripts": "5.0.0", "tone": "^14.7.77", "web-vitals": "^2.1.4" @@ -56,6 +57,7 @@ "autoprefixer": "^10.4.4", "parcel": "^2.3.1", "postcss": "^8.4.12", + "process": "^0.11.10", "serve": "^13.0.2", "tailwindcss": "^3.0.23" } diff --git a/repl/src/App.js b/repl/src/App.js index 2448ec62..46236294 100644 --- a/repl/src/App.js +++ b/repl/src/App.js @@ -33,6 +33,7 @@ import '@strudel.cycles/osc/osc.mjs'; import '@strudel.cycles/webaudio/webaudio.mjs'; import '@strudel.cycles/serial/serial.mjs'; import controls from '@strudel.cycles/core/controls.mjs'; +import useHighlighting from './useHighlighting'; extend( Tone, @@ -71,7 +72,6 @@ const defaultSynth = getDefaultSynth(); function App() { // const [editor, setEditor] = useState(); const [view, setView] = useState(); - const [codeToHighlight, setCodeToHighlight] = useState(); const { setCode, setPattern, @@ -90,11 +90,7 @@ function App() { } = useRepl({ tune: decoded || randomTune, defaultSynth, - // onDraw: useCallback((time, event) => markEvent(editor)(time, event), [editor]), - // onDraw: useCallback((_, e, code) => code && highlightEvent(e, view, code), [view]), - onDraw: () => {}, }); - const [uiHidden, setUiHidden] = useState(false); const logBox = useRef(); // scroll log box to bottom when log changes useLayoutEffect(() => { @@ -119,28 +115,7 @@ function App() { return () => window.removeEventListener('keydown', handleKeyPress); }, [pattern, code, activateCode, cycle]); - useEffect(() => { - if (view) { - if (pattern && cycle.started) { - let frame = requestAnimationFrame(updateHighlights); - - function updateHighlights() { - let audioTime = Tone.Transport.seconds; - let timespan = new strudel.TimeSpan(audioTime, audioTime + 1 / 60); - let events = pattern.query(new strudel.State(timespan)); - view.dispatch({ effects: setHighlights.of(events) }); - - frame = requestAnimationFrame(updateHighlights); - } - - return () => { - cancelAnimationFrame(frame); - }; - } else { - view.dispatch({ effects: setHighlights.of([]) }); - } - } - }, [pattern, cycle.started]); + useHighlighting({ view, pattern, started: cycle.started }); useWebMidi({ ready: useCallback( @@ -167,10 +142,7 @@ function App() {