mirror of https://github.com/sveltejs/svelte
parent
27ec345f31
commit
e6f0282da1
@ -0,0 +1,59 @@
|
||||
<script>
|
||||
import { getContext, onMount } from 'svelte';
|
||||
import CodeMirror from '../CodeMirror.svelte';
|
||||
import Message from '../Message.svelte';
|
||||
|
||||
const { bundle, selected, handle_change, navigate, register_module_editor } = getContext('REPL');
|
||||
|
||||
export let errorLoc;
|
||||
|
||||
let editor;
|
||||
onMount(() => {
|
||||
register_module_editor(editor);
|
||||
});
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.editor-wrapper {
|
||||
z-index: 5;
|
||||
background: var(--back-light);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.editor {
|
||||
height: 0;
|
||||
flex: 1 1 auto;
|
||||
}
|
||||
|
||||
@media (min-width: 600px) {
|
||||
:global(.columns) .editor-wrapper {
|
||||
/* make it easier to interact with scrollbar */
|
||||
padding-right: 8px;
|
||||
height: auto;
|
||||
/* height: 100%; */
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="editor-wrapper">
|
||||
<div class="editor">
|
||||
<CodeMirror
|
||||
bind:this={editor}
|
||||
{errorLoc}
|
||||
on:change={handle_change}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="info">
|
||||
{#if $bundle}
|
||||
{#if $bundle.error}
|
||||
<Message kind="error" details={$bundle.error} filename="{$selected.name}.{$selected.type}"/>
|
||||
{:else if $bundle.warnings.length > 0}
|
||||
{#each $bundle.warnings as warning}
|
||||
<Message kind="warning" details={warning} filename="{$selected.name}.{$selected.type}"/>
|
||||
{/each}
|
||||
{/if}
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
@ -0,0 +1,80 @@
|
||||
<script>
|
||||
import { getContext } from 'svelte';
|
||||
|
||||
const { navigate } = getContext('REPL');
|
||||
|
||||
export let kind;
|
||||
export let details = null;
|
||||
export let filename = null;
|
||||
|
||||
function message(details) {
|
||||
let str = details.message || '[missing message]';
|
||||
|
||||
let loc = [];
|
||||
|
||||
if (details.filename && details.filename !== filename) {
|
||||
loc.push(details.filename);
|
||||
}
|
||||
|
||||
if (details.start) loc.push(details.start.line, details.start.column);
|
||||
|
||||
return str + (loc.length ? ` (${loc.join(':')})` : ``);
|
||||
};
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.message {
|
||||
position: relative;
|
||||
color: white;
|
||||
padding: 1.2rem 1.6rem 1.2rem 4.4rem;
|
||||
font: 400 1.2rem/1.7 var(--font);
|
||||
margin: 0;
|
||||
border-top: 1px solid white;
|
||||
}
|
||||
|
||||
.navigable {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.message::before {
|
||||
content: '!';
|
||||
position: absolute;
|
||||
left: 1.2rem;
|
||||
top: 1.1rem;
|
||||
width: 1rem;
|
||||
height: 1rem;
|
||||
text-align: center;
|
||||
line-height: 1;
|
||||
padding: .4rem;
|
||||
border-radius: 50%;
|
||||
color: white;
|
||||
border: .2rem solid white;
|
||||
}
|
||||
|
||||
p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.info {
|
||||
background-color: var(--second);
|
||||
}
|
||||
|
||||
.error {
|
||||
background-color: #da106e;
|
||||
}
|
||||
|
||||
.warning {
|
||||
background-color: #e47e0a;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="message {kind}">
|
||||
{#if details}
|
||||
<p
|
||||
class:navigable={details.filename}
|
||||
on:click="{() => navigate(details)}"
|
||||
>{message(details)}</p>
|
||||
{:else}
|
||||
<slot></slot>
|
||||
{/if}
|
||||
</div>
|
@ -0,0 +1,38 @@
|
||||
export default class Compiler {
|
||||
constructor(version) {
|
||||
this.worker = new Worker('/workers/compiler.js');
|
||||
this.worker.postMessage({ type: 'init', version });
|
||||
|
||||
this.uid = 1;
|
||||
this.handlers = new Map();
|
||||
|
||||
this.worker.onmessage = event => {
|
||||
const handler = this.handlers.get(event.data.id);
|
||||
handler(event.data.result);
|
||||
this.handlers.delete(event.data.id);
|
||||
};
|
||||
}
|
||||
|
||||
compile(component, options) {
|
||||
return new Promise(fulfil => {
|
||||
const id = this.uid++;
|
||||
|
||||
this.handlers.set(id, fulfil);
|
||||
|
||||
this.worker.postMessage({
|
||||
id,
|
||||
type: 'compile',
|
||||
source: component.source,
|
||||
options: Object.assign({
|
||||
name: component.name,
|
||||
filename: `${component.name}.svelte`
|
||||
}, options),
|
||||
entry: component.name === 'App'
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.worker.terminate();
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
export default class ReplProxy {
|
||||
constructor(iframe, handlers) {
|
||||
this.iframe = iframe;
|
||||
this.handlers = handlers;
|
||||
|
||||
this.cmdId = 1;
|
||||
this.pendingCmds = new Map();
|
||||
|
||||
this.handle_event = e => this.handleReplMessage(e);
|
||||
window.addEventListener('message', this.handle_event, false);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
window.removeEventListener('message', this.handle_event);
|
||||
}
|
||||
|
||||
iframeCommand(command, args) {
|
||||
return new Promise( (resolve, reject) => {
|
||||
this.cmdId += 1;
|
||||
this.pendingCmds.set(this.cmdId, { resolve, reject });
|
||||
|
||||
this.iframe.contentWindow.postMessage({
|
||||
action: command,
|
||||
cmdId: this.cmdId,
|
||||
args
|
||||
}, '*')
|
||||
});
|
||||
}
|
||||
|
||||
handleCommandMessage(cmdData) {
|
||||
let action = cmdData.action;
|
||||
let id = cmdData.cmdId;
|
||||
let handler = this.pendingCmds.get(id);
|
||||
|
||||
if (handler) {
|
||||
this.pendingCmds.delete(id);
|
||||
if (action === 'cmdError') {
|
||||
let { message, stack } = cmdData;
|
||||
let e = new Error(message);
|
||||
e.stack = stack;
|
||||
console.log('repl cmd fail');
|
||||
handler.reject(e)
|
||||
}
|
||||
|
||||
if (action === 'cmdOk') {
|
||||
handler.resolve(cmdData.args)
|
||||
}
|
||||
} else {
|
||||
console.error('command not found', id, cmdData, [...this.pendingCmds.keys()]);
|
||||
}
|
||||
}
|
||||
|
||||
handleReplMessage(event) {
|
||||
const { action, args } = event.data;
|
||||
|
||||
if (action === 'cmdError' || action === 'cmdOk') {
|
||||
this.handleCommandMessage(event.data);
|
||||
}
|
||||
|
||||
if (action === 'prop_update') {
|
||||
const { prop, value } = args;
|
||||
this.handlers.onPropUpdate(prop, value)
|
||||
}
|
||||
|
||||
if (action === 'fetch_progress') {
|
||||
this.handlers.onFetchProgress(args.remaining)
|
||||
}
|
||||
}
|
||||
|
||||
eval(script) {
|
||||
return this.iframeCommand('eval', { script });
|
||||
}
|
||||
|
||||
setProp(prop, value) {
|
||||
return this.iframeCommand('set_prop', {prop, value})
|
||||
}
|
||||
|
||||
bindProps(props) {
|
||||
return this.iframeCommand('bind_props', { props })
|
||||
}
|
||||
|
||||
handleLinks() {
|
||||
return this.iframeCommand('catch_clicks', {});
|
||||
}
|
||||
|
||||
fetchImports(imports, import_map) {
|
||||
return this.iframeCommand('fetch_imports', { imports, import_map })
|
||||
}
|
||||
}
|
@ -0,0 +1,158 @@
|
||||
<script>
|
||||
import { onMount, createEventDispatcher, getContext } from 'svelte';
|
||||
import getLocationFromStack from './getLocationFromStack.js';
|
||||
import ReplProxy from './ReplProxy.js';
|
||||
import Message from '../Message.svelte';
|
||||
import { decode } from 'sourcemap-codec';
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
const { bundle, values, navigate } = getContext('REPL');
|
||||
|
||||
export let props;
|
||||
export let error; // TODO should this be exposed as a prop?
|
||||
|
||||
export function setProp(prop, value) {
|
||||
if (!proxy) return;
|
||||
proxy.setProp(prop, value);
|
||||
}
|
||||
|
||||
let iframe;
|
||||
let pending_imports = 0;
|
||||
let pending = false;
|
||||
|
||||
let proxy = null;
|
||||
|
||||
let ready = false;
|
||||
|
||||
onMount(() => {
|
||||
proxy = new ReplProxy(iframe, {
|
||||
onPropUpdate: (prop, value) => {
|
||||
dispatch('binding', { prop, value });
|
||||
values.update(values => Object.assign({}, values, {
|
||||
[prop]: value
|
||||
}));
|
||||
},
|
||||
|
||||
onFetchProgress: progress => {
|
||||
pending_imports = progress;
|
||||
}
|
||||
});
|
||||
|
||||
iframe.addEventListener('load', () => {
|
||||
proxy.handleLinks();
|
||||
ready = true;
|
||||
});
|
||||
|
||||
return () => {
|
||||
proxy.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
let current_token;
|
||||
|
||||
async function apply_bundle($bundle) {
|
||||
if (!$bundle || $bundle.error) return;
|
||||
|
||||
const token = current_token = {};
|
||||
|
||||
try {
|
||||
await proxy.fetchImports($bundle.imports, $bundle.import_map);
|
||||
if (token !== current_token) return;
|
||||
|
||||
await proxy.eval(`
|
||||
const styles = document.querySelectorAll('style.svelte');
|
||||
let i = styles.length;
|
||||
while (i--) styles[i].parentNode.removeChild(styles[i]);
|
||||
`)
|
||||
|
||||
await proxy.eval(`${$bundle.dom.code}
|
||||
if (window.component) {
|
||||
try {
|
||||
window.component.$destroy();
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
document.body.innerHTML = '';
|
||||
window.location.hash = '';
|
||||
window._svelteTransitionManager = null;
|
||||
|
||||
window.component = new SvelteComponent({
|
||||
target: document.body,
|
||||
props: ${JSON.stringify($values)}
|
||||
});
|
||||
`);
|
||||
|
||||
await proxy.bindProps(props);
|
||||
|
||||
error = null;
|
||||
} catch (e) {
|
||||
const loc = getLocationFromStack(e.stack, $bundle.dom.map);
|
||||
if (loc) {
|
||||
e.filename = loc.source;
|
||||
e.loc = { line: loc.line, column: loc.column };
|
||||
}
|
||||
|
||||
error = e;
|
||||
}
|
||||
}
|
||||
|
||||
$: if (ready) apply_bundle($bundle);
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.iframe-container {
|
||||
position: absolute;
|
||||
background-color: white;
|
||||
border: none;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
iframe {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
/* height: calc(100vh - var(--nav-h)); */
|
||||
border: none;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.greyed-out {
|
||||
filter: grayscale(50%) blur(1px);
|
||||
opacity: .25;
|
||||
}
|
||||
|
||||
.overlay {
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="iframe-container">
|
||||
<iframe title="Result" bind:this={iframe} sandbox="allow-popups-to-escape-sandbox allow-scripts allow-popups allow-forms allow-pointer-lock allow-top-navigation allow-modals" class="{error || pending || pending_imports ? 'greyed-out' : ''}" srcdoc='
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="/repl-viewer.css">
|
||||
</head>
|
||||
<body>
|
||||
<script src="/curl.js"></script>
|
||||
<script>curl.config({ dontAddFileExt: /./ });</script>
|
||||
<script src="/repl-runner.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
'></iframe>
|
||||
|
||||
<div class="overlay">
|
||||
{#if error}
|
||||
<Message kind="error" details={error}/>
|
||||
{:else if !$bundle}
|
||||
<Message kind="info">loading Svelte compiler...</Message>
|
||||
{:else if pending_imports}
|
||||
<Message kind="info">loading {pending_imports} {pending_imports === 1 ? 'dependency' : 'dependencies'} from
|
||||
https://bundle.run</Message>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
@ -0,0 +1,246 @@
|
||||
<script>
|
||||
import { getContext, onMount } from 'svelte';
|
||||
import SplitPane from '../SplitPane.svelte';
|
||||
import Viewer from './Viewer.svelte';
|
||||
import CompilerOptions from './CompilerOptions.svelte';
|
||||
import Compiler from './Compiler.js';
|
||||
import PropEditor from './PropEditor.svelte';
|
||||
import CodeMirror from '../CodeMirror.svelte';
|
||||
|
||||
const { values, register_output } = getContext('REPL');
|
||||
|
||||
export let version;
|
||||
export let sourceErrorLoc;
|
||||
export let runtimeError;
|
||||
export let embedded;
|
||||
export let show_props;
|
||||
|
||||
let props;
|
||||
|
||||
let compile_options = {
|
||||
generate: 'dom',
|
||||
dev: false,
|
||||
css: false,
|
||||
hydratable: false,
|
||||
customElement: false,
|
||||
immutable: false,
|
||||
legacy: false
|
||||
};
|
||||
|
||||
register_output({
|
||||
set: async selected => {
|
||||
if (selected.type === 'js') {
|
||||
js_editor.set(`/* Select a component to see its compiled code */`);
|
||||
css_editor.set(`/* Select a component to see its compiled code */`);
|
||||
return;
|
||||
}
|
||||
|
||||
const compiled = await compiler.compile(selected, compile_options);
|
||||
|
||||
js_editor.set(compiled.js, 'js');
|
||||
css_editor.set(compiled.css, 'css');
|
||||
if (compiled.props) props = compiled.props;
|
||||
},
|
||||
|
||||
update: async selected => {
|
||||
if (selected.type === 'js') return;
|
||||
|
||||
const compiled = await compiler.compile(selected, compile_options);
|
||||
|
||||
js_editor.update(compiled.js);
|
||||
css_editor.update(compiled.css);
|
||||
if (compiled.props) props = compiled.props;
|
||||
}
|
||||
});
|
||||
|
||||
let compiler;
|
||||
|
||||
onMount(() => {
|
||||
compiler = new Compiler(version);
|
||||
return () => compiler.destroy();
|
||||
});
|
||||
|
||||
// refs
|
||||
let viewer;
|
||||
let js_editor;
|
||||
let css_editor;
|
||||
const setters = {};
|
||||
|
||||
let view = 'result';
|
||||
|
||||
function updateValues(prop, value) {
|
||||
values.update(v => Object.assign({}, v, {
|
||||
[prop]: value
|
||||
}));
|
||||
}
|
||||
|
||||
function setPropFromViewer(prop, value) {
|
||||
// console.log(setters, prop, value);
|
||||
// setters[prop](value);
|
||||
updateValues(prop, value);
|
||||
}
|
||||
|
||||
function setPropFromEditor(prop, value) {
|
||||
viewer.setProp(prop, value);
|
||||
updateValues(prop, value);
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.view-toggle {
|
||||
height: var(--pane-controls-h);
|
||||
border-bottom: 1px solid #eee;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
button {
|
||||
/* width: 50%;
|
||||
height: 100%; */
|
||||
text-align: left;
|
||||
position: relative;
|
||||
font: 400 1.2rem/1.5 var(--font);
|
||||
border-bottom: var(--border-w) solid transparent;
|
||||
padding: 1.2rem 0.8rem 0.8rem 0.8rem;
|
||||
color: #999;
|
||||
}
|
||||
|
||||
button.active {
|
||||
border-bottom: var(--border-w) solid var(--prime);
|
||||
color: #333;
|
||||
}
|
||||
|
||||
div[slot] {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
h3 {
|
||||
font: 700 1.2rem/1.5 var(--font);
|
||||
padding: 1.2rem 0 0.8rem 1rem;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.props {
|
||||
display: grid;
|
||||
padding: 0.5em;
|
||||
grid-template-columns: auto 1fr;
|
||||
grid-auto-rows: min-content;
|
||||
grid-gap: 0.5em;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.props code {
|
||||
top: .1rem;
|
||||
}
|
||||
|
||||
.tab-content {
|
||||
position: absolute;
|
||||
width: 100%;
|
||||
height: calc(100% - 4.2rem);
|
||||
opacity: 0;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.tab-content.visible {
|
||||
/* can't use visibility due to a weird painting bug in Chrome */
|
||||
opacity: 1;
|
||||
pointer-events: all;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="view-toggle">
|
||||
<button
|
||||
class:active="{view === 'result'}"
|
||||
on:click="{() => view = 'result'}"
|
||||
>Result</button>
|
||||
|
||||
<button
|
||||
class:active="{view === 'js'}"
|
||||
on:click="{() => view = 'js'}"
|
||||
>JS output</button>
|
||||
|
||||
<button
|
||||
class:active="{view === 'css'}"
|
||||
on:click="{() => view = 'css'}"
|
||||
>CSS output</button>
|
||||
</div>
|
||||
|
||||
<!-- component viewer -->
|
||||
<div class="tab-content" class:visible="{view === 'result'}">
|
||||
<SplitPane type="vertical" pos={67} fixed={!show_props} fixed_pos={100}>
|
||||
<div slot="a">
|
||||
<Viewer
|
||||
bind:this={viewer}
|
||||
{props}
|
||||
bind:error={runtimeError}
|
||||
on:binding="{e => setPropFromViewer(e.detail.prop, e.detail.value)}"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<section slot="b">
|
||||
{#if show_props}
|
||||
<h3>Props editor</h3>
|
||||
|
||||
{#if props}
|
||||
{#if props.length > 0}
|
||||
<div class="props">
|
||||
{#each props.sort() as prop (prop)}
|
||||
<code style="display: block; whitespace: pre;">{prop}</code>
|
||||
|
||||
<!-- TODO `bind:this={propEditors[prop]}` — currently fails -->
|
||||
<PropEditor
|
||||
value={$values[prop]}
|
||||
on:change="{e => setPropFromEditor(prop, e.detail.value)}"
|
||||
/>
|
||||
{/each}
|
||||
</div>
|
||||
{:else}
|
||||
<div style="padding: 0.5em" class="linkify">
|
||||
<!-- TODO explain distincion between logic-less and logic-ful components? -->
|
||||
<!-- TODO style the <a> so it looks like a link -->
|
||||
<p style="font-size: 1.3rem; color: var(--second)">This component has no props — <a href="guide#external-properties">declare props with the export keyword</a></p>
|
||||
</div>
|
||||
{/if}
|
||||
{/if}
|
||||
{/if}
|
||||
</section>
|
||||
</SplitPane>
|
||||
</div>
|
||||
|
||||
<!-- js output -->
|
||||
<div class="tab-content" class:visible="{view === 'js'}">
|
||||
{#if embedded}
|
||||
<CodeMirror
|
||||
bind:this={js_editor}
|
||||
mode="js"
|
||||
errorLoc={sourceErrorLoc}
|
||||
readonly
|
||||
/>
|
||||
{:else}
|
||||
<SplitPane type="vertical" pos={67}>
|
||||
<div slot="a">
|
||||
<CodeMirror
|
||||
bind:this={js_editor}
|
||||
mode="js"
|
||||
errorLoc={sourceErrorLoc}
|
||||
readonly
|
||||
/>
|
||||
</div>
|
||||
|
||||
<section slot="b">
|
||||
<h3>Compiler options</h3>
|
||||
|
||||
<CompilerOptions bind:options={compile_options}/>
|
||||
</section>
|
||||
</SplitPane>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<!-- css output -->
|
||||
<div class="tab-content" class:visible="{view === 'css'}">
|
||||
<CodeMirror
|
||||
bind:this={css_editor}
|
||||
mode="css"
|
||||
errorLoc={sourceErrorLoc}
|
||||
readonly
|
||||
/>
|
||||
</div>
|
@ -0,0 +1,232 @@
|
||||
<script>
|
||||
import { onMount, setContext, createEventDispatcher } from 'svelte';
|
||||
import { writable } from 'svelte/store';
|
||||
import SplitPane from './SplitPane.svelte';
|
||||
import CodeMirror from './CodeMirror.svelte';
|
||||
import ComponentSelector from './Input/ComponentSelector.svelte';
|
||||
import ModuleEditor from './Input/ModuleEditor.svelte';
|
||||
import Output from './Output/index.svelte';
|
||||
import InputOutputToggle from './InputOutputToggle.svelte';
|
||||
|
||||
export let version = 'beta'; // TODO change this to latest when the time comes
|
||||
export let embedded = false;
|
||||
export let orientation = 'columns';
|
||||
export let show_props = true;
|
||||
|
||||
export function toJSON() {
|
||||
// TODO there's a bug here — Svelte hoists this function because
|
||||
// it wrongly things that $components is global. Needs to
|
||||
// factor in $ variables when determining hoistability
|
||||
|
||||
version; // workaround
|
||||
|
||||
return {
|
||||
imports: $bundle.imports,
|
||||
components: $components,
|
||||
values: $values
|
||||
};
|
||||
}
|
||||
|
||||
export function set(data) {
|
||||
components.set(data.components);
|
||||
values.set(data.values);
|
||||
selected.set(data.components[0]);
|
||||
|
||||
module_editor.set($selected.source, $selected.type);
|
||||
output.set($selected);
|
||||
}
|
||||
|
||||
export function update(data) {
|
||||
const { name, type } = $selected || {};
|
||||
|
||||
components.set(data.components);
|
||||
values.set(data.values);
|
||||
|
||||
const matched_component = data.components.find(file => file.name === name && file.type === type);
|
||||
selected.set(matched_component || data.components[0]);
|
||||
|
||||
if (matched_component) {
|
||||
module_editor.update(matched_component.source);
|
||||
output.update(matched_component);
|
||||
} else {
|
||||
module_editor.set(matched_component.source, matched_component.type);
|
||||
output.set(matched_component);
|
||||
}
|
||||
}
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
|
||||
const components = writable([]);
|
||||
const values = writable({});
|
||||
const selected = writable(null);
|
||||
const bundle = writable(null);
|
||||
|
||||
let module_editor;
|
||||
let output;
|
||||
|
||||
setContext('REPL', {
|
||||
components,
|
||||
values,
|
||||
selected,
|
||||
bundle,
|
||||
|
||||
navigate: item => {
|
||||
const match = /^(.+)\.(\w+)$/.exec(item.filename);
|
||||
if (!match) return; // ???
|
||||
|
||||
const [, name, type] = match;
|
||||
const component = $components.find(c => c.name === name && c.type === type);
|
||||
selected.set(component);
|
||||
|
||||
output.set($selected);
|
||||
|
||||
// TODO select the line/column in question
|
||||
},
|
||||
|
||||
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
|
||||
output.update($selected);
|
||||
|
||||
// regenerate bundle (TODO do this in a separate worker?)
|
||||
workers.bundler.postMessage({ type: 'bundle', components: $components });
|
||||
|
||||
dispatch('change', {
|
||||
components: $components
|
||||
});
|
||||
},
|
||||
|
||||
register_module_editor(editor) {
|
||||
module_editor = editor;
|
||||
},
|
||||
|
||||
register_output(handlers) {
|
||||
output = handlers;
|
||||
}
|
||||
});
|
||||
|
||||
function handle_select(component) {
|
||||
selected.set(component);
|
||||
module_editor.set(component.source, component.type);
|
||||
output.set($selected);
|
||||
}
|
||||
|
||||
let workers;
|
||||
|
||||
let input;
|
||||
let sourceErrorLoc;
|
||||
let runtimeErrorLoc; // TODO refactor this stuff — runtimeErrorLoc is unused
|
||||
|
||||
let width = typeof window !== 'undefined' ? window.innerWidth : 300;
|
||||
let show_output = false;
|
||||
|
||||
onMount(async () => {
|
||||
workers = {
|
||||
bundler: new Worker('/workers/bundler.js')
|
||||
};
|
||||
|
||||
workers.bundler.postMessage({ type: 'init', version });
|
||||
workers.bundler.onmessage = event => {
|
||||
bundle.set(event.data);
|
||||
};
|
||||
|
||||
return () => {
|
||||
workers.bundler.terminate();
|
||||
};
|
||||
});
|
||||
|
||||
$: if ($bundle && $bundle.error && $selected) {
|
||||
sourceErrorLoc = $bundle.error.filename === `${$selected.name}.${$selected.type}`
|
||||
? $bundle.error.start
|
||||
: null;
|
||||
}
|
||||
|
||||
$: if (workers && $components) {
|
||||
workers.bundler.postMessage({ type: 'bundle', components: $components });
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.container {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
height: calc(100% - 4.2rem);
|
||||
}
|
||||
|
||||
.repl-inner {
|
||||
width: 200%;
|
||||
height: 100%;
|
||||
transition: transform 0.3s;
|
||||
}
|
||||
|
||||
.repl-inner :global(section) {
|
||||
position: relative;
|
||||
padding: 4.2rem 0 0 0;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.repl-inner :global(section) > :global(*):first-child {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 4.2rem;
|
||||
}
|
||||
|
||||
.repl-inner :global(section) > :global(*):last-child {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.offset {
|
||||
transform: translate(-50%,0);
|
||||
}
|
||||
|
||||
@media (min-width: 600px) {
|
||||
.container {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.repl-inner {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.offset {
|
||||
transition: none;
|
||||
transform: none;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="container" class:orientation bind:clientWidth={width}>
|
||||
<div class="repl-inner" class:offset="{show_output}">
|
||||
<SplitPane
|
||||
type="{orientation === 'rows' ? 'vertical' : 'horizontal'}"
|
||||
fixed="{600 > width}"
|
||||
pos="{orientation === 'rows' ? 50 : 60}"
|
||||
fixed_pos={50}
|
||||
>
|
||||
<section slot=a>
|
||||
<ComponentSelector {handle_select}/>
|
||||
<ModuleEditor bind:this={input} errorLoc="{sourceErrorLoc || runtimeErrorLoc}"/>
|
||||
</section>
|
||||
|
||||
<section slot=b style='height: 100%;'>
|
||||
<Output {version} {embedded} {show_props}/>
|
||||
</section>
|
||||
</SplitPane>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<InputOutputToggle bind:checked={show_output}/>
|
@ -1,38 +0,0 @@
|
||||
<script>
|
||||
import CodeMirror from '../CodeMirror.svelte';
|
||||
|
||||
export let component;
|
||||
export let error;
|
||||
export let errorLoc;
|
||||
export let warningCount = 0;
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.editor-wrapper {
|
||||
z-index: 5;
|
||||
background: var(--back-light);
|
||||
}
|
||||
|
||||
@media (min-width: 600px) {
|
||||
.editor-wrapper {
|
||||
/* make it easier to interact with scrollbar */
|
||||
padding-right: 8px;
|
||||
height: auto;
|
||||
/* height: 100%; */
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="editor-wrapper">
|
||||
{#if component}
|
||||
<CodeMirror
|
||||
mode="{component.type === 'js' ? 'javascript' : 'handlebars'}"
|
||||
code={component.source}
|
||||
{error}
|
||||
{errorLoc}
|
||||
{warningCount}
|
||||
on:change
|
||||
on:navigate
|
||||
/>
|
||||
{/if}
|
||||
</div>
|
@ -1,28 +0,0 @@
|
||||
<script>
|
||||
import ComponentSelector from './ComponentSelector.svelte';
|
||||
import ModuleEditor from './ModuleEditor.svelte';
|
||||
|
||||
export let selected_store;
|
||||
export let component_store;
|
||||
export let error;
|
||||
export let errorLoc;
|
||||
export let warningCount;
|
||||
</script>
|
||||
|
||||
<!-- TODO would be nice if events bubbled -->
|
||||
<ComponentSelector
|
||||
{component_store}
|
||||
{selected_store}
|
||||
on:create
|
||||
on:remove
|
||||
on:select
|
||||
/>
|
||||
|
||||
<ModuleEditor
|
||||
component={$selected_store}
|
||||
{error}
|
||||
{errorLoc}
|
||||
{warningCount}
|
||||
on:change
|
||||
on:navigate
|
||||
/>
|
@ -1,325 +0,0 @@
|
||||
<script>
|
||||
import { onMount, onDestroy, createEventDispatcher } from 'svelte';
|
||||
import getLocationFromStack from '../../_utils/getLocationFromStack.js';
|
||||
import ReplProxy from '../../_utils/replProxy.js';
|
||||
import { decode } from 'sourcemap-codec';
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
|
||||
export let values_store;
|
||||
export let bundle;
|
||||
export let dom;
|
||||
export let ssr;
|
||||
export let props;
|
||||
export let sourceError;
|
||||
export let error;
|
||||
|
||||
export function setProp(prop, value) {
|
||||
if (!replProxy) return;
|
||||
replProxy.setProp(prop, value);
|
||||
}
|
||||
|
||||
let hasComponent = false;
|
||||
|
||||
const refs = {};
|
||||
let pendingImports = 0;
|
||||
let pending = false;
|
||||
|
||||
let replProxy = null;
|
||||
|
||||
|
||||
const namespaceSpecifier = /\*\s+as\s+(\w+)/;
|
||||
const namedSpecifiers = /\{(.+)\}/;
|
||||
|
||||
function parseSpecifiers(specifiers) {
|
||||
specifiers = specifiers.trim();
|
||||
|
||||
let match = namespaceSpecifier.exec(specifiers);
|
||||
if (match) {
|
||||
return {
|
||||
namespace: true,
|
||||
name: match[1]
|
||||
};
|
||||
}
|
||||
|
||||
let names = [];
|
||||
|
||||
specifiers = specifiers.replace(namedSpecifiers, (match, str) => {
|
||||
names = str.split(',').map(name => {
|
||||
const split = name.split('as');
|
||||
const exported = split[0].trim();
|
||||
const local = (split[1] || exported).trim();
|
||||
|
||||
return { local, exported };
|
||||
});
|
||||
|
||||
return '';
|
||||
});
|
||||
|
||||
match = /\w+/.exec(specifiers);
|
||||
|
||||
return {
|
||||
namespace: false,
|
||||
names,
|
||||
default: match ? match[0] : null
|
||||
};
|
||||
}
|
||||
|
||||
let createComponent;
|
||||
let init;
|
||||
onDestroy(() => {
|
||||
if (replProxy) {
|
||||
replProxy.destroy();
|
||||
}
|
||||
});
|
||||
onMount(() => {
|
||||
replProxy = new ReplProxy(refs.child);
|
||||
|
||||
refs.child.addEventListener('load', () => {
|
||||
|
||||
replProxy.onPropUpdate = (prop, value) => {
|
||||
dispatch('binding', { prop, value });
|
||||
values_store.update(values => Object.assign({}, values, {
|
||||
[prop]: value
|
||||
}));
|
||||
};
|
||||
|
||||
replProxy.onFetchProgress = (progress) => {
|
||||
pendingImports = progress
|
||||
}
|
||||
|
||||
replProxy.handleLinks();
|
||||
|
||||
let promise = null;
|
||||
let updating = false;
|
||||
|
||||
let toDestroy = null;
|
||||
|
||||
const init = () => {
|
||||
if (sourceError) return;
|
||||
|
||||
const removeStyles = () => {
|
||||
replProxy.eval(`
|
||||
const styles = document.querySelectorAll('style.svelte');
|
||||
let i = styles.length;
|
||||
while (i--) styles[i].parentNode.removeChild(styles[i]);
|
||||
`)
|
||||
};
|
||||
|
||||
const destroyComponent = () => {
|
||||
replProxy.eval(`if (window.component)
|
||||
window.component.\$destroy();
|
||||
window.component = null`);
|
||||
};
|
||||
|
||||
const ready = () => {
|
||||
error = null;
|
||||
|
||||
if (toDestroy) {
|
||||
removeStyles();
|
||||
destroyComponent();
|
||||
toDestroy = null;
|
||||
}
|
||||
|
||||
if (ssr) { // this only gets generated if component uses lifecycle hooks
|
||||
pending = true;
|
||||
createHtml();
|
||||
} else {
|
||||
pending = false;
|
||||
createComponent();
|
||||
}
|
||||
}
|
||||
|
||||
const createHtml = () => {
|
||||
replProxy.eval(`${ssr.code}
|
||||
var rendered = SvelteComponent.render(${JSON.stringify($values_store)});
|
||||
|
||||
if (rendered.css.code) {
|
||||
var style = document.createElement('style');
|
||||
style.className = 'svelte';
|
||||
style.textContent = rendered.css.code;
|
||||
document.head.appendChild(style);
|
||||
}
|
||||
|
||||
document.body.innerHTML = rendered.html;
|
||||
`)
|
||||
.catch( e => {
|
||||
const loc = getLocationFromStack(e.stack, ssr.map);
|
||||
if (loc) {
|
||||
e.filename = loc.source;
|
||||
e.loc = { line: loc.line, column: loc.column };
|
||||
}
|
||||
|
||||
error = e;
|
||||
});
|
||||
};
|
||||
|
||||
const createComponent = () => {
|
||||
// remove leftover styles from SSR renderer
|
||||
if (ssr) removeStyles();
|
||||
|
||||
replProxy.eval(`${dom.code}
|
||||
document.body.innerHTML = '';
|
||||
window.location.hash = '';
|
||||
window._svelteTransitionManager = null;
|
||||
|
||||
window.component = new SvelteComponent({
|
||||
target: document.body,
|
||||
props: ${JSON.stringify($values_store)}
|
||||
});
|
||||
`)
|
||||
.then(()=> {
|
||||
replProxy.bindProps(props);
|
||||
})
|
||||
.catch(e => {
|
||||
// TODO show in UI
|
||||
hasComponent = false;
|
||||
|
||||
const loc = getLocationFromStack(e.stack, dom.map);
|
||||
if (loc) {
|
||||
e.filename = loc.source;
|
||||
e.loc = { line: loc.line, column: loc.column };
|
||||
}
|
||||
|
||||
error = e;
|
||||
});
|
||||
};
|
||||
|
||||
// Download the imports (sets them on iframe window when complete)
|
||||
{
|
||||
let cancelled = false;
|
||||
promise = replProxy.fetchImports(bundle);
|
||||
promise.cancel = () => { cancelled = true };
|
||||
promise.then(() => {
|
||||
if (cancelled) return;
|
||||
ready()
|
||||
}).catch(e => {
|
||||
if (cancelled) return;
|
||||
error = e;
|
||||
});
|
||||
}
|
||||
|
||||
run = () => {
|
||||
pending = false;
|
||||
|
||||
// TODO do we need to clear out SSR HTML?
|
||||
createComponent();
|
||||
props_handler = props => {
|
||||
replProxy.bindProps(props)
|
||||
};
|
||||
replProxy.bindProps(props);
|
||||
};
|
||||
}
|
||||
|
||||
bundle_handler = bundle => {
|
||||
if (!bundle) return; // TODO can this ever happen?
|
||||
if (promise) promise.cancel();
|
||||
|
||||
toDestroy = hasComponent;
|
||||
hasComponent = false;
|
||||
|
||||
init();
|
||||
};
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
function noop(){}
|
||||
let run = noop;
|
||||
let bundle_handler = noop;
|
||||
let props_handler = noop;
|
||||
|
||||
$: bundle_handler(bundle);
|
||||
$: props_handler(props);
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.iframe-container {
|
||||
background-color: white;
|
||||
border: none;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
iframe {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
/* height: calc(100vh - var(--nav-h)); */
|
||||
border: none;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.greyed-out {
|
||||
filter: grayscale(50%) blur(1px);
|
||||
opacity: .25;
|
||||
}
|
||||
|
||||
.overlay {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
padding: 1em;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.overlay p {
|
||||
pointer-events: all;
|
||||
}
|
||||
|
||||
.pending {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
text-align: center;
|
||||
pointer-events: all;
|
||||
}
|
||||
|
||||
.pending button {
|
||||
position: absolute;
|
||||
margin-top: 6rem;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="iframe-container">
|
||||
<iframe title="Result" bind:this={refs.child} sandbox="allow-scripts allow-popups allow-forms allow-pointer-lock allow-top-navigation allow-modals" class="{error || pending || pendingImports ? 'greyed-out' : ''}" srcdoc='
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="/repl-viewer.css">
|
||||
</head>
|
||||
<body>
|
||||
<script src="/curl.js"></script>
|
||||
<script>curl.config({ dontAddFileExt: /./ });</script>
|
||||
<script src="/repl-runner.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
'></iframe>
|
||||
</div>
|
||||
|
||||
<div class="overlay">
|
||||
{#if error}
|
||||
<p class="error message">
|
||||
{#if error.loc}
|
||||
<strong>
|
||||
{#if error.filename}
|
||||
<span class="filename" on:click="{() => dispatch('navigate', { filename: error.filename })}">{error.filename}</span>
|
||||
{/if}
|
||||
|
||||
({error.loc.line}:{error.loc.column})
|
||||
</strong>
|
||||
{/if}
|
||||
|
||||
{error.message}
|
||||
</p>
|
||||
{:elseif pending}
|
||||
<div class="pending" on:click={run}>
|
||||
<button class="bg-second white">Click to run</button>
|
||||
</div>
|
||||
{:elseif pendingImports}
|
||||
<p class="info message">loading {pendingImports} {pendingImports === 1 ? 'dependency' : 'dependencies'} from
|
||||
https://bundle.run</p>
|
||||
{/if}
|
||||
</div>
|
@ -1,197 +0,0 @@
|
||||
<script>
|
||||
import SplitPane from '../SplitPane.svelte';
|
||||
import Viewer from './Viewer.svelte';
|
||||
import CompilerOptions from './CompilerOptions.svelte';
|
||||
import PropEditor from './PropEditor.svelte';
|
||||
import CodeMirror from '../CodeMirror.svelte';
|
||||
|
||||
export let values_store;
|
||||
|
||||
export let bundle;
|
||||
export let js;
|
||||
export let css;
|
||||
export let dom;
|
||||
export let ssr;
|
||||
export let props;
|
||||
export let json5;
|
||||
export let sourceError;
|
||||
export let sourceErrorLoc;
|
||||
export let runtimeError;
|
||||
export let compileOptions;
|
||||
export let embedded;
|
||||
|
||||
// refs
|
||||
let viewer;
|
||||
const setters = {};
|
||||
|
||||
let view = 'result';
|
||||
|
||||
function updateValues(prop, value) {
|
||||
values_store.update(v => Object.assign({}, v, {
|
||||
[prop]: value
|
||||
}));
|
||||
}
|
||||
|
||||
function setPropFromViewer(prop, value) {
|
||||
// console.log(setters, prop, value);
|
||||
// setters[prop](value);
|
||||
updateValues(prop, value);
|
||||
}
|
||||
|
||||
function setPropFromEditor(prop, value) {
|
||||
viewer.setProp(prop, value);
|
||||
updateValues(prop, value);
|
||||
}
|
||||
|
||||
function navigate(event) {
|
||||
// TODO handle navigation from error messages
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.view-toggle {
|
||||
height: var(--pane-controls-h);
|
||||
border-bottom: 1px solid #eee;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
button {
|
||||
/* width: 50%;
|
||||
height: 100%; */
|
||||
text-align: left;
|
||||
position: relative;
|
||||
font: 400 1.2rem/1.5 var(--font);
|
||||
border-bottom: var(--border-w) solid transparent;
|
||||
padding: 1.2rem 0.8rem 0.8rem 0.8rem;
|
||||
color: #999;
|
||||
}
|
||||
|
||||
button.active {
|
||||
border-bottom: var(--border-w) solid var(--prime);
|
||||
color: #333;
|
||||
}
|
||||
|
||||
div[slot] {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
h3 {
|
||||
font: 700 1.2rem/1.5 var(--font);
|
||||
padding: 1.2rem 0 0.8rem 1rem;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.loading.message {
|
||||
position: absolute !important;
|
||||
background-color: #666;
|
||||
top: 1em;
|
||||
left: 50%;
|
||||
transform: translate(-50%,0);
|
||||
}
|
||||
|
||||
.props {
|
||||
display: grid;
|
||||
padding: 0.5em;
|
||||
grid-template-columns: auto 1fr;
|
||||
grid-auto-rows: min-content;
|
||||
grid-gap: 0.5em;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.props code {
|
||||
top: .1rem;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="view-toggle">
|
||||
<button
|
||||
class:active="{view === 'result'}"
|
||||
on:click="{() => view = 'result'}"
|
||||
>Result</button>
|
||||
|
||||
<button
|
||||
class:active="{view === 'js'}"
|
||||
on:click="{() => view = 'js'}"
|
||||
>JS output</button>
|
||||
|
||||
<button
|
||||
class:active="{view === 'css'}"
|
||||
on:click="{() => view = 'css'}"
|
||||
>CSS output</button>
|
||||
</div>
|
||||
|
||||
{#if view === 'result'}
|
||||
<SplitPane type="vertical" pos={67}>
|
||||
<div slot="a">
|
||||
{#if bundle}
|
||||
<Viewer
|
||||
bind:this={viewer}
|
||||
{bundle}
|
||||
{dom}
|
||||
{ssr}
|
||||
{values_store}
|
||||
{props}
|
||||
{sourceError}
|
||||
bind:error={runtimeError}
|
||||
on:navigate={navigate}
|
||||
on:binding="{e => setPropFromViewer(e.detail.prop, e.detail.value)}"
|
||||
/>
|
||||
{:else}
|
||||
<!-- TODO componentise this -->
|
||||
<p class="loading message">loading Svelte compiler...</p>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<section slot="b">
|
||||
<h3>Props editor</h3>
|
||||
|
||||
{#if props}
|
||||
{#if props.length > 0}
|
||||
<div class="props">
|
||||
{#each props.sort() as prop (prop)}
|
||||
<code style="display: block; whitespace: pre;">{prop}</code>
|
||||
|
||||
<!-- TODO `bind:this={propEditors[prop]}` — currently fails -->
|
||||
<PropEditor
|
||||
value={$values_store[prop]}
|
||||
on:change="{e => setPropFromEditor(prop, e.detail.value)}"
|
||||
/>
|
||||
{/each}
|
||||
</div>
|
||||
{:else}
|
||||
<div style="padding: 0.5em" class="linkify">
|
||||
<!-- TODO explain distincion between logic-less and logic-ful components? -->
|
||||
<!-- TODO style the <a> so it looks like a link -->
|
||||
<p style="font-size: 1.3rem; color: var(--second)">This component has no props — <a href="guide#external-properties">declare props with the export keyword</a></p>
|
||||
</div>
|
||||
{/if}
|
||||
{/if}
|
||||
</section>
|
||||
</SplitPane>
|
||||
{:elseif embedded}
|
||||
<CodeMirror
|
||||
mode="javascript"
|
||||
code="{view === 'js' ? js : css}"
|
||||
error={sourceError}
|
||||
errorLoc={sourceErrorLoc}
|
||||
readonly
|
||||
/>
|
||||
{:else}
|
||||
<SplitPane type="vertical" pos={67}>
|
||||
<div slot="a">
|
||||
<CodeMirror
|
||||
mode="javascript"
|
||||
code="{view === 'js' ? js : css}"
|
||||
error={sourceError}
|
||||
errorLoc={sourceErrorLoc}
|
||||
readonly
|
||||
/>
|
||||
</div>
|
||||
|
||||
<section slot="b">
|
||||
<h3>Compiler options</h3>
|
||||
|
||||
<CompilerOptions bind:options={compileOptions}/>
|
||||
</section>
|
||||
</SplitPane>
|
||||
{/if}
|
@ -1,321 +0,0 @@
|
||||
<script>
|
||||
import { onMount } from 'svelte';
|
||||
import { writable } from 'svelte/store';
|
||||
import * as fleece from 'golden-fleece';
|
||||
import SplitPane from './SplitPane.svelte';
|
||||
import CodeMirror from './CodeMirror.svelte';
|
||||
import Input from './Input/index.svelte';
|
||||
import Output from './Output/index.svelte';
|
||||
import InputOutputToggle from './InputOutputToggle.svelte';
|
||||
|
||||
export let version = 'beta'; // TODO change this to latest when the time comes
|
||||
export let app;
|
||||
export let embedded = false;
|
||||
|
||||
export function toJSON() {
|
||||
// TODO there's a bug here — Svelte hoists this function because
|
||||
// it wrongly things that $component_store is global. Needs to
|
||||
// factor in $ variables when determining hoistability
|
||||
|
||||
version; // workaround
|
||||
|
||||
return {
|
||||
imports: bundle && bundle.imports || [],
|
||||
components: $component_store,
|
||||
values: $values_store
|
||||
};
|
||||
}
|
||||
|
||||
let component_store = writable([]);
|
||||
let values_store = writable({});
|
||||
let selected_store = writable(null);
|
||||
|
||||
$: {
|
||||
component_store.set(app.components);
|
||||
values_store.set(app.values);
|
||||
selected_store.set(app.components[0]);
|
||||
}
|
||||
|
||||
$: {
|
||||
// necessary pending https://github.com/sveltejs/svelte/issues/1889
|
||||
$component_store;
|
||||
$values_store;
|
||||
}
|
||||
|
||||
let workers;
|
||||
|
||||
let bundle = null;
|
||||
let dom;
|
||||
let ssr;
|
||||
let sourceError = null;
|
||||
let runtimeError = null;
|
||||
let warningCount = 0;
|
||||
let js = '';
|
||||
let css = '';
|
||||
let uid = 0;
|
||||
let props = [];
|
||||
let sourceErrorLoc;
|
||||
let runtimeErrorLoc;
|
||||
|
||||
let compileOptions = {
|
||||
generate: 'dom',
|
||||
dev: false,
|
||||
css: false,
|
||||
hydratable: false,
|
||||
customElement: false,
|
||||
immutable: false,
|
||||
legacy: false
|
||||
};
|
||||
|
||||
let width = typeof window !== 'undefined' ? window.innerWidth : 300;
|
||||
let show_output = false;
|
||||
|
||||
onMount(async () => {
|
||||
workers = {
|
||||
compiler: new Worker('/workers/compiler.js'),
|
||||
bundler: new Worker('/workers/bundler.js')
|
||||
};
|
||||
|
||||
workers.compiler.postMessage({ type: 'init', version });
|
||||
workers.compiler.onmessage = event => {
|
||||
js = event.data.js;
|
||||
css = event.data.css || `/* Add a <style> tag to see compiled CSS */`;
|
||||
if (event.data.props) props = event.data.props;
|
||||
};
|
||||
|
||||
workers.bundler.postMessage({ type: 'init', version });
|
||||
workers.bundler.onmessage = event => {
|
||||
({ bundle, dom, ssr, warningCount, error: sourceError } = event.data);
|
||||
if (sourceError) console.error(sourceError);
|
||||
runtimeError = null;
|
||||
};
|
||||
|
||||
return () => {
|
||||
workers.compiler.terminate();
|
||||
workers.bundler.terminate();
|
||||
};
|
||||
});
|
||||
|
||||
function removeComponent() {
|
||||
const selected = $selected_store;
|
||||
|
||||
if (selected.name === 'App') {
|
||||
// App.svelte can't be removed
|
||||
selected.source = '';
|
||||
// $selected_store.set(selected);
|
||||
} else {
|
||||
const components = $component_store;
|
||||
const index = components.indexOf(selected);
|
||||
|
||||
if (~index) {
|
||||
component_store.set(components.slice(0, index).concat(components.slice(index + 1)));
|
||||
} else {
|
||||
console.error(`Could not find component! That's... odd`);
|
||||
}
|
||||
|
||||
selected_store.set(components[index] || components[components.length - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
function compile(component, options) {
|
||||
if (component.type === 'svelte') {
|
||||
workers.compiler.postMessage({
|
||||
type: 'compile',
|
||||
source: component.source,
|
||||
options: Object.assign({
|
||||
name: component.name,
|
||||
filename: `${component.name}.svelte`
|
||||
}, options),
|
||||
entry: component === $component_store[0]
|
||||
});
|
||||
} else {
|
||||
js = css = `/* Select a component to see its compiled code */`;
|
||||
}
|
||||
}
|
||||
|
||||
function handleChange(event) {
|
||||
selected_store.update(component => {
|
||||
// TODO this is a bit hacky — we're relying on mutability
|
||||
// so that updating component_store 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;
|
||||
});
|
||||
|
||||
component_store.update(c => c);
|
||||
|
||||
// recompile selected component
|
||||
compile($selected_store, compileOptions);
|
||||
|
||||
// regenerate bundle (TODO do this in a separate worker?)
|
||||
workers.bundler.postMessage({ type: 'bundle', components: $component_store });
|
||||
}
|
||||
|
||||
function navigate(filename) {
|
||||
const name = filename.replace(/\.svelte$/, '');
|
||||
|
||||
console.error(`TODO navigate`);
|
||||
|
||||
// if (selected.name === name) return;
|
||||
// selected = components.find(c => c.name === name);
|
||||
}
|
||||
|
||||
$: if (sourceError && $selected_store) {
|
||||
sourceErrorLoc = sourceError.filename === `${$selected_store.name}.${$selected_store.type}`
|
||||
? sourceError.start
|
||||
: null;
|
||||
}
|
||||
|
||||
$: if (runtimeError && $selected_store) {
|
||||
runtimeErrorLoc = runtimeError.filename === `${$selected_store.name}.${$selected_store.type}`
|
||||
? runtimeError.start
|
||||
: null;
|
||||
}
|
||||
|
||||
$: if (workers && app.components) {
|
||||
workers.bundler.postMessage({ type: 'bundle', components: app.components });
|
||||
}
|
||||
|
||||
$: if (workers && $selected_store) {
|
||||
compile($selected_store, compileOptions);
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.container {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
height: calc(100% - 4.2rem);
|
||||
}
|
||||
|
||||
.repl-inner {
|
||||
width: 200%;
|
||||
height: 100%;
|
||||
transition: transform 0.3s;
|
||||
}
|
||||
|
||||
.repl-inner :global(section) {
|
||||
position: relative;
|
||||
padding: 4.2rem 0 0 0;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.repl-inner :global(section) > :global(*):first-child {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 4.2rem;
|
||||
}
|
||||
|
||||
.repl-inner :global(section) > :global(*):last-child {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.repl-inner :global(.message) {
|
||||
position: relative;
|
||||
border-radius: var(--border-r);
|
||||
margin: 0;
|
||||
padding: 1.2rem 1.6rem 1.2rem 4.4rem;
|
||||
vertical-align: middle;
|
||||
font: 400 1.2rem/1.7 var(--font);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.repl-inner :global(.message::before) {
|
||||
content: '!';
|
||||
position: absolute;
|
||||
left: 1.2rem;
|
||||
top: 1.1rem;
|
||||
width: 1rem;
|
||||
height: 1rem;
|
||||
text-align: center;
|
||||
line-height: 1;
|
||||
padding: .4rem;
|
||||
border-radius: 50%;
|
||||
color: white;
|
||||
border: .2rem solid white;
|
||||
}
|
||||
|
||||
.repl-inner :global(.error.message) {
|
||||
background-color: #da106e;
|
||||
}
|
||||
|
||||
.repl-inner :global(.warning.message) {
|
||||
background-color: #e47e0a;
|
||||
}
|
||||
|
||||
.repl-inner :global(.info.message) {
|
||||
background-color: var(--second);
|
||||
animation: fade-in .4s .2s both;
|
||||
}
|
||||
|
||||
.repl-inner :global(.error) :global(.filename) {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.offset {
|
||||
transform: translate(-50%,0);
|
||||
}
|
||||
|
||||
@media (min-width: 600px) {
|
||||
.container {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.repl-inner {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.input-output-toggle {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.offset {
|
||||
transition: none;
|
||||
transform: none;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="container" bind:clientWidth={width}>
|
||||
<div class="repl-inner" class:offset="{show_output}">
|
||||
<SplitPane type="horizontal" fixed="{600 > width}" pos={60} fixed_pos={50}>
|
||||
<section slot=a>
|
||||
<Input
|
||||
{component_store}
|
||||
{selected_store}
|
||||
{values_store}
|
||||
error={sourceError}
|
||||
errorLoc="{sourceErrorLoc || runtimeErrorLoc}"
|
||||
{warningCount}
|
||||
on:remove={removeComponent}
|
||||
on:change="{handleChange}"
|
||||
/>
|
||||
</section>
|
||||
|
||||
<section slot=b style='height: 100%;'>
|
||||
<Output
|
||||
bind:compileOptions
|
||||
{version}
|
||||
{selected_store}
|
||||
{js}
|
||||
{css}
|
||||
{bundle}
|
||||
{ssr}
|
||||
{dom}
|
||||
{props}
|
||||
{values_store}
|
||||
{sourceError}
|
||||
{runtimeError}
|
||||
{embedded}
|
||||
/>
|
||||
</section>
|
||||
</SplitPane>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<InputOutputToggle bind:checked={show_output}/>
|
@ -1,90 +0,0 @@
|
||||
export default class ReplProxy {
|
||||
constructor(iframe) {
|
||||
this.iframe = iframe;
|
||||
this.cmdId = 1;
|
||||
this.pendingCmds = new Map();
|
||||
this.onPropUpdate = null;
|
||||
this.onFetchProgress = null;
|
||||
this.handle_event = (ev) => this.handleReplMessage(ev);
|
||||
window.addEventListener("message", this.handle_event, false);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
window.removeEventListener("message", this.handle_event);
|
||||
}
|
||||
|
||||
iframeCommand(command, args) {
|
||||
return new Promise( (resolve, reject) => {
|
||||
this.cmdId = this.cmdId + 1;
|
||||
this.pendingCmds.set(this.cmdId, { resolve: resolve, reject: reject });
|
||||
this.iframe.contentWindow.postMessage({
|
||||
action: command,
|
||||
cmdId: this.cmdId,
|
||||
args: args
|
||||
}, '*')
|
||||
});
|
||||
}
|
||||
|
||||
handleCommandMessage(cmdData) {
|
||||
let action = cmdData.action;
|
||||
let id = cmdData.cmdId;
|
||||
let handler = this.pendingCmds.get(id);
|
||||
if (handler) {
|
||||
|
||||
this.pendingCmds.delete(id);
|
||||
if (action == "cmdError") {
|
||||
let { message, stack } = cmdData;
|
||||
let e = new Error(message);
|
||||
e.stack = stack;
|
||||
console.log("repl cmd fail");
|
||||
handler.reject(e)
|
||||
}
|
||||
|
||||
if (action == "cmdOk") {
|
||||
handler.resolve(cmdData.args)
|
||||
}
|
||||
} else {
|
||||
console.error("command not found", id, cmdData, [...this.pendingCmds.keys()]);
|
||||
}
|
||||
}
|
||||
|
||||
handleReplMessage(ev) {
|
||||
|
||||
let action = ev.data.action;
|
||||
if ( action == "cmdError" || action == "cmdOk" ) {
|
||||
this.handleCommandMessage(ev.data);
|
||||
}
|
||||
|
||||
if (action == "prop_update") {
|
||||
let { prop, value } = ev.data.args;
|
||||
if (this.onPropUpdate)
|
||||
this.onPropUpdate(prop, value)
|
||||
}
|
||||
|
||||
if (action == "fetch_progress") {
|
||||
if (this.onFetchProgress)
|
||||
this.onFetchProgress(ev.data.args.remaining)
|
||||
}
|
||||
}
|
||||
|
||||
eval(script) {
|
||||
return this.iframeCommand("eval", { script: script });
|
||||
}
|
||||
|
||||
setProp(prop, value) {
|
||||
return this.iframeCommand("set_prop", {prop, value})
|
||||
}
|
||||
|
||||
bindProps(props) {
|
||||
return this.iframeCommand("bind_props", { props: props })
|
||||
}
|
||||
|
||||
handleLinks() {
|
||||
return this.iframeCommand("catch_clicks", {});
|
||||
}
|
||||
|
||||
fetchImports(bundle) {
|
||||
return this.iframeCommand("fetch_imports", { bundle })
|
||||
}
|
||||
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
export default function process_markdown(markdown) {
|
||||
const match = /---\r?\n([\s\S]+?)\r?\n---/.exec(markdown);
|
||||
const frontMatter = match[1];
|
||||
const content = markdown.slice(match[0].length);
|
||||
|
||||
const metadata = {};
|
||||
frontMatter.split('\n').forEach(pair => {
|
||||
const colonIndex = pair.indexOf(':');
|
||||
metadata[pair.slice(0, colonIndex).trim()] = pair
|
||||
.slice(colonIndex + 1)
|
||||
.trim();
|
||||
});
|
||||
|
||||
return { metadata, content };
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
import * as fleece from 'golden-fleece';
|
||||
|
||||
export function extract_frontmatter(markdown) {
|
||||
const match = /---\r?\n([\s\S]+?)\r?\n---/.exec(markdown);
|
||||
const frontMatter = match[1];
|
||||
const content = markdown.slice(match[0].length);
|
||||
|
||||
const metadata = {};
|
||||
frontMatter.split('\n').forEach(pair => {
|
||||
const colonIndex = pair.indexOf(':');
|
||||
metadata[pair.slice(0, colonIndex).trim()] = pair
|
||||
.slice(colonIndex + 1)
|
||||
.trim();
|
||||
});
|
||||
|
||||
return { metadata, content };
|
||||
}
|
||||
|
||||
export function extract_metadata(line, lang) {
|
||||
try {
|
||||
if (lang === 'html' && line.startsWith('<!--') && line.endsWith('-->')) {
|
||||
return fleece.evaluate(line.slice(4, -3).trim());
|
||||
}
|
||||
|
||||
if (
|
||||
lang === 'js' ||
|
||||
(lang === 'json' && line.startsWith('/*') && line.endsWith('*/'))
|
||||
) {
|
||||
return fleece.evaluate(line.slice(2, -2).trim());
|
||||
}
|
||||
} catch (err) {
|
||||
// TODO report these errors, don't just squelch them
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// map lang to prism-language-attr
|
||||
export const langs = {
|
||||
bash: 'bash',
|
||||
html: 'markup',
|
||||
js: 'javascript',
|
||||
css: 'css',
|
||||
};
|
Loading…
Reference in new issue