diff --git a/packages/core/examples/vite-vanilla-repl-cm6/codemirror.js b/packages/core/examples/vite-vanilla-repl-cm6/codemirror.js index 7ba377ab..7a0d95d8 100644 --- a/packages/core/examples/vite-vanilla-repl-cm6/codemirror.js +++ b/packages/core/examples/vite-vanilla-repl-cm6/codemirror.js @@ -7,7 +7,7 @@ import { StateField, StateEffect } from '@codemirror/state'; import './style.css'; // https://codemirror.net/docs/guide/ -export function initEditor(initialCode, onUpdate) { +export function initEditor({ initialCode, onChange, onEvaluate, onStop }) { let state = EditorState.create({ doc: initialCode, extensions: [ @@ -22,9 +22,17 @@ export function initEditor(initialCode, onUpdate) { syntaxHighlighting(defaultHighlightStyle), keymap.of(defaultKeymap), //flashField, - EditorView.updateListener.of((v) => { - onUpdate(v); - }), + EditorView.updateListener.of((v) => onChange(v)), + keymap.of([ + { + key: 'Ctrl-Enter', + run: () => onEvaluate(), + }, + { + key: 'Ctrl-.', + run: () => onStop(), + }, + ]), ], }); diff --git a/packages/core/examples/vite-vanilla-repl-cm6/main.js b/packages/core/examples/vite-vanilla-repl-cm6/main.js index 814f3609..fafed909 100644 --- a/packages/core/examples/vite-vanilla-repl-cm6/main.js +++ b/packages/core/examples/vite-vanilla-repl-cm6/main.js @@ -5,28 +5,36 @@ import { initStrudel } from './strudel'; import { Highlighter } from './highlighter'; import { bumpStreet } from './tunes'; let code = bumpStreet; - -const view = initEditor(code, (v) => { - code = v.state.doc.toString(); -}); const repl = initStrudel(); -let highlighter = new Highlighter((haps) => highlightHaps(view, haps)); - -document.getElementById('play').addEventListener('click', async () => { +async function onEvaluate() { const { evaluate, scheduler } = await repl; if (!scheduler.started) { scheduler.stop(); - scheduler.lastEnd = 0; await evaluate(code); highlighter.start(scheduler); } else { await evaluate(code); } +} + +async function onStop() { + const { scheduler } = await repl; + scheduler.stop(); + highlighter.stop(); +} + +const view = initEditor({ + initialCode: code, + onChange: (v) => { + code = v.state.doc.toString(); + }, + onEvaluate, + onStop, }); -document.getElementById('stop').addEventListener('click', async () => { - const { stop } = await repl; - stop(); - highlighter.stop(); -}); +let highlighter = new Highlighter((haps) => highlightHaps(view, haps)); + +document.getElementById('play').addEventListener('click', () => onEvaluate()); + +document.getElementById('stop').addEventListener('click', async () => onStop());