highlight active events, js only
This commit is contained in:
parent
cf5ae93638
commit
8d872ccee9
1 changed files with 27 additions and 1 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import React, { useCallback, useLayoutEffect, useRef } from 'react';
|
import React, { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
||||||
import * as Tone from 'tone';
|
import * as Tone from 'tone';
|
||||||
import CodeMirror from './CodeMirror';
|
import CodeMirror from './CodeMirror';
|
||||||
import cx from './cx';
|
import cx from './cx';
|
||||||
|
|
@ -35,9 +35,33 @@ function getRandomTune() {
|
||||||
const randomTune = getRandomTune();
|
const randomTune = getRandomTune();
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
const [editor, setEditor] = useState<any>();
|
||||||
|
const doc = useMemo(() => editor?.getDoc(), [editor]);
|
||||||
const { setCode, setPattern, error, code, cycle, dirty, log, togglePlay, activateCode, pattern, pushLog } = useRepl({
|
const { setCode, setPattern, error, code, cycle, dirty, log, togglePlay, activateCode, pattern, pushLog } = useRepl({
|
||||||
tune: decoded || randomTune,
|
tune: decoded || randomTune,
|
||||||
defaultSynth,
|
defaultSynth,
|
||||||
|
onEvent: useCallback(
|
||||||
|
(event) => {
|
||||||
|
const locs = event.value.locations;
|
||||||
|
if (!locs) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// mark active event
|
||||||
|
const marks = locs.map(({ start, end }) =>
|
||||||
|
doc.markText(
|
||||||
|
{ line: start.line - 1, ch: start.column },
|
||||||
|
{ line: end.line - 1, ch: end.column },
|
||||||
|
{ css: 'background-color: gray;' }
|
||||||
|
)
|
||||||
|
);
|
||||||
|
//Tone.Transport.schedule(() => { // problem: this can be cleared by scheduler...
|
||||||
|
setTimeout(() => {
|
||||||
|
marks.forEach((mark) => mark.clear());
|
||||||
|
// }, '+' + event.duration * 0.5);
|
||||||
|
}, event.duration * 0.9 * 1000);
|
||||||
|
},
|
||||||
|
[doc]
|
||||||
|
),
|
||||||
});
|
});
|
||||||
const logBox = useRef<any>();
|
const logBox = useRef<any>();
|
||||||
// scroll log box to bottom when log changes
|
// scroll log box to bottom when log changes
|
||||||
|
|
@ -106,10 +130,12 @@ function App() {
|
||||||
<div className={cx('h-full bg-[#2A3236]', error ? 'focus:ring-red-500' : 'focus:ring-slate-800')}>
|
<div className={cx('h-full bg-[#2A3236]', error ? 'focus:ring-red-500' : 'focus:ring-slate-800')}>
|
||||||
<CodeMirror
|
<CodeMirror
|
||||||
value={code}
|
value={code}
|
||||||
|
editorDidMount={setEditor}
|
||||||
options={{
|
options={{
|
||||||
mode: 'javascript',
|
mode: 'javascript',
|
||||||
theme: 'material',
|
theme: 'material',
|
||||||
lineNumbers: true,
|
lineNumbers: true,
|
||||||
|
styleSelectedText: true,
|
||||||
}}
|
}}
|
||||||
onChange={(_: any, __: any, value: any) => setCode(value)}
|
onChange={(_: any, __: any, value: any) => setCode(value)}
|
||||||
/>
|
/>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue