From 069fff673a58b4df2173b968f9d56ce3af26d16d Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Mon, 25 Apr 2022 22:00:18 +0200 Subject: [PATCH] fix some bugs --- repl/src/App.js | 9 +++--- repl/src/CodeMirror6.jsx | 45 ++++++++++++++++----------- repl/src/themes/material-palenight.js | 7 +++-- repl/src/useRepl.mjs | 11 ++++--- 4 files changed, 41 insertions(+), 31 deletions(-) diff --git a/repl/src/App.js b/repl/src/App.js index 1c574e6e..ffd0a27a 100644 --- a/repl/src/App.js +++ b/repl/src/App.js @@ -82,6 +82,7 @@ function App() { log, togglePlay, activeCode, + setActiveCode, activateCode, pattern, pushLog, @@ -90,11 +91,8 @@ function App() { tune: decoded || randomTune, defaultSynth, // onDraw: useCallback((time, event) => markEvent(editor)(time, event), [editor]), - onDraw: useCallback((_, e) => highlightEvent(e, view, codeToHighlight), [view, codeToHighlight]), + onDraw: useCallback((_, e, code) => code && highlightEvent(e, view, code), [view]), }); - useEffect(() => { - setCodeToHighlight(activeCode); - }, [activeCode]); const [uiHidden, setUiHidden] = useState(false); const logBox = useRef(); // scroll log box to bottom when log changes @@ -191,6 +189,7 @@ function App() { uiHelpers.cleanup(); const parsed = await evaluate(_code); setPattern(parsed.pattern); + setActiveCode(_code); }} > 🎲 random @@ -201,7 +200,7 @@ function App() {
-
+
{/* onCursor={markParens} */} diff --git a/repl/src/CodeMirror6.jsx b/repl/src/CodeMirror6.jsx index 341ed0df..f82f5d62 100644 --- a/repl/src/CodeMirror6.jsx +++ b/repl/src/CodeMirror6.jsx @@ -18,26 +18,31 @@ const highlightField = StateField.define({ return Decoration.none; }, update(highlights, tr) { - highlights = highlights.map(tr.changes); - for (let e of tr.effects) { - if (e.is(addHighlight)) { - highlights = highlights.update({ - add: [highlightMark.range(e.value.from, e.value.to)], - }); - } - if (e.is(removeHighlight)) { - highlights = highlights.update({ - filter: (f, t, value) => { - if (f === e.value.from && t === e.value.to) { - return false; - } - return true; - // console.log('filter', f,t,value, e.value.from, e.value.to); - }, - }); + try { + highlights = highlights.map(tr.changes); + for (let e of tr.effects) { + if (e.is(addHighlight)) { + highlights = highlights.update({ + add: [highlightMark.range(e.value.from, e.value.to)], + }); + } + if (e.is(removeHighlight)) { + highlights = highlights.update({ + filter: (f, t, value) => { + if (f === e.value.from && t === e.value.to) { + return false; + } + return true; + // console.log('filter', f,t,value, e.value.from, e.value.to); + }, + }); + } } + return highlights; + } catch (err) { + // console.warn('highlighting error', err); + return highlights; } - return highlights; }, provide: (f) => EditorView.decorations.from(f), }); @@ -117,6 +122,10 @@ export function offsetToPosition(offset, code) { // returns absolute character offset from { line, ch } export function positionToOffset(position, code) { const lines = code.split('\n'); + if (position.line > lines.length) { + // throw new Error('positionToOffset: position.line > lines.length'); + return 0; + } let offset = 0; for (let i = 0; i < position.line; i++) { offset += lines[i].length + 1; diff --git a/repl/src/themes/material-palenight.js b/repl/src/themes/material-palenight.js index be7527c1..0f219155 100644 --- a/repl/src/themes/material-palenight.js +++ b/repl/src/themes/material-palenight.js @@ -26,6 +26,7 @@ export const materialPalenightTheme = EditorView.theme( '&': { color: '#ffffff', backgroundColor: background, + fontSize: '16px', 'z-index': 11, }, @@ -33,8 +34,8 @@ export const materialPalenightTheme = EditorView.theme( '.cm-content': { caretColor: cursor, }, - '.cm-line > *': { - background: '#00000070', + '.cm-line': { + background: '#2C323699', }, // done '&.cm-focused .cm-cursor': { @@ -68,7 +69,7 @@ export const materialPalenightTheme = EditorView.theme( // done '.cm-gutters': { - background, + background: '#2C323699', color: '#676e95', border: 'none', }, diff --git a/repl/src/useRepl.mjs b/repl/src/useRepl.mjs index c20a9cba..21605dce 100644 --- a/repl/src/useRepl.mjs +++ b/repl/src/useRepl.mjs @@ -11,7 +11,7 @@ let s4 = () => { }; const generateHash = (code) => encodeURIComponent(btoa(code)); -function useRepl({ tune, defaultSynth, autolink = true, onEvent, onDraw }) { +function useRepl({ tune, defaultSynth, autolink = true, onEvent, onDraw: onDrawProp }) { const id = useMemo(() => s4(), []); const [code, setCode] = useState(tune); const [activeCode, setActiveCode] = useState(); @@ -24,12 +24,12 @@ function useRepl({ tune, defaultSynth, autolink = true, onEvent, onDraw }) { const pushLog = useCallback((message) => setLog((log) => log + `${log ? '\n\n' : ''}${message}`), []); // below block allows disabling the highlighting by including "strudel disable-highlighting" in the code (as comment) - onDraw = useMemo(() => { + const onDraw = useMemo(() => { if (activeCode && !activeCode.includes('strudel disable-highlighting')) { - return onDraw; + return (time, event) => onDrawProp(time, event, activeCode); } - }, [activeCode, onDraw]); - + }, [activeCode, onDrawProp]); + // cycle hook to control scheduling const cycle = useCycle({ onDraw, @@ -137,6 +137,7 @@ function useRepl({ tune, defaultSynth, autolink = true, onEvent, onDraw }) { dirty, log, togglePlay, + setActiveCode, activateCode, activeCode, pushLog,