use context for handling changes

pull/2179/head
Richard Harris 7 years ago
parent e4e29b9a31
commit 977f61f1eb

@ -2,7 +2,7 @@
import { getContext } from 'svelte';
import CodeMirror from '../CodeMirror.svelte';
const { selected } = getContext('REPL');
const { selected, handle_change } = getContext('REPL');
export let error;
export let errorLoc;
@ -33,7 +33,7 @@
{error}
{errorLoc}
{warningCount}
on:change
on:change={handle_change}
/>
{/if}
</div>

@ -13,5 +13,4 @@
{error}
{errorLoc}
{warningCount}
on:change
/>

@ -44,6 +44,26 @@
// if (selected.name === name) return;
// selected = components.find(c => c.name === name);
},
handle_change: event => {
selected.update(component => {
// TODO this is a bit hacky — we're relying on mutability
// so that updating components works... might be better
// if a) components had unique IDs, b) we tracked selected
// *index* rather than component, and c) `selected` was
// derived from `components` and `index`
component.source = event.detail.value;
return component;
});
components.update(c => c);
// recompile selected component
compile($selected, compile_options);
// regenerate bundle (TODO do this in a separate worker?)
workers.bundler.postMessage({ type: 'bundle', components: $components });
}
});
@ -123,26 +143,6 @@
}
}
function handleChange(event) {
selected.update(component => {
// TODO this is a bit hacky — we're relying on mutability
// so that updating components works... might be better
// if a) components had unique IDs, b) we tracked selected
// *index* rather than component, and c) `selected` was
// derived from `components` and `index`
component.source = event.detail.value;
return component;
});
components.update(c => c);
// recompile selected component
compile($selected, compile_options);
// regenerate bundle (TODO do this in a separate worker?)
workers.bundler.postMessage({ type: 'bundle', components: $components });
}
$: if (sourceError && $selected) {
sourceErrorLoc = sourceError.filename === `${$selected.name}.${$selected.type}`
? sourceError.start
@ -275,7 +275,6 @@
error={sourceError}
errorLoc="{sourceErrorLoc || runtimeErrorLoc}"
{warningCount}
on:change="{handleChange}"
/>
</section>

Loading…
Cancel
Save