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

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

@ -44,6 +44,26 @@
// if (selected.name === name) return; // if (selected.name === name) return;
// selected = components.find(c => c.name === name); // 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) { $: if (sourceError && $selected) {
sourceErrorLoc = sourceError.filename === `${$selected.name}.${$selected.type}` sourceErrorLoc = sourceError.filename === `${$selected.name}.${$selected.type}`
? sourceError.start ? sourceError.start
@ -275,7 +275,6 @@
error={sourceError} error={sourceError}
errorLoc="{sourceErrorLoc || runtimeErrorLoc}" errorLoc="{sourceErrorLoc || runtimeErrorLoc}"
{warningCount} {warningCount}
on:change="{handleChange}"
/> />
</section> </section>

Loading…
Cancel
Save