fix some bugs

This commit is contained in:
Felix Roos 2022-04-25 22:00:18 +02:00
parent 5aed046b34
commit 069fff673a
4 changed files with 41 additions and 31 deletions

View File

@ -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() {
</div>
</header>
<section className="grow flex flex-col text-gray-100">
<div className="grow relative flex" id="code">
<div className="grow relative flex overflow-auto" id="code">
{/* onCursor={markParens} */}
<CodeMirror6 value={code} onChange={setCode} onViewChanged={setView} />
<span className="p-4 absolute top-0 right-0 text-xs whitespace-pre text-right pointer-events-none">

View File

@ -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;

View File

@ -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',
},

View File

@ -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,