Skip to content

Commit

Permalink
Merge pull request #53 from milafrerichs/feat/use-slots
Browse files Browse the repository at this point in the history
Feat: Use Layouts and Slots & Use stores
  • Loading branch information
milafrerichs authored Sep 5, 2019
2 parents 4f2d3b1 + 0c659b0 commit da2c2d6
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 126 deletions.
2 changes: 1 addition & 1 deletion example/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@
</script>

<Repl {files} />
<Repl layout={'default'} {files} />

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "javascript-repl",
"svelte": "src/Repl.svelte",
"module": "index.mjs",
"version": "0.3.0",
"version": "0.4.0",
"description": "",
"main": "index.js",
"author": "Mila Frerichs <[email protected]>",
Expand Down
12 changes: 7 additions & 5 deletions src/Console.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<script>
import Viewer from './Viewer.svelte';
import Result from './Result.svelte';
import { onMount } from 'svelte';
export let output
import { code } from './stores.js'
let message = '';
$: if(output) {
$: if($code) {
message = `
document.body.innerHTML = '';
var consoleOutput = '';
Expand All @@ -15,10 +17,10 @@
consoleOutput += message + '<br />';
}
};
${output}
${$code}
document.body.innerHTML = '';
document.body.innerHTML = consoleOutput;
`
}
</script>
<Viewer code={message} />
<Result html={''} code={message} />
130 changes: 57 additions & 73 deletions src/Repl.svelte
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
<script>
import Viewer from './Viewer.svelte';
import ViewerConsole from './ViewerConsole.svelte';
import Editor from './Editor.svelte';
import Console from './Console.svelte';
import Default from './layouts/Default.svelte';
import Minimal from './layouts/Minimal.svelte';
import MinimalReverse from './layouts/MinimalReverse.svelte';
import {
code,
html,
ready,
files as file_store,
injectedJS as injectedJS_store,
injectedLibraries as injectedLibraries_store,
currentFile
} from './stores.js'
let ready = false;
let editor;
let manualUpdates = false;
let tab = 'viewer';
let currentFile = {};
let currentFileIndex = 0;
let currentContent = '';
let code = '';
let html = '';
export let mode = 'normal';
const layouts = new Map([
[ 'default', Default ],
[ 'minimal-reverse', MinimalReverse ],
[ 'minimal', Minimal ]
]);
export let layout = 'default';
export let changedCode = () => {};
export let files = [];
export let injectedLibraries = [];
Expand Down Expand Up @@ -55,10 +70,10 @@
currentContent = event.detail.value;
manualUpdates = true;
changedCode();
if (currentFile.type === 'js') {
code = currentContent;
if ($currentFile.type === 'js') {
code.set(currentContent);
} else {
html = currentContent.replace(/\n/g,'');
html.set(currentContent.replace(/\n/g,''));
}
}
Expand All @@ -73,44 +88,37 @@
}
function update() {
code = getContentForType('js') || '';
html = getContentForType('html') || '';
if(!html) html = '';
code.set(getContentForType('js') || '');
html.set(getContentForType('html') || '');
if(editor) {
editor.update(currentFile.content);
editor.update($currentFile.content);
}
}
$: showEditor = (mode === 'normal' || mode === 'minimal')
$: showTabs = (mode === 'normal' || mode === 'view')
$: showFiles = (mode === 'normal' || mode === 'view')
$: currentFile = files[currentFileIndex]
$: if(editor && currentFile) {
editor.update(currentFile.content);
$: if(files) {
file_store.set(files);
}
$: if(injectedJS) {
injectedJS_store.set(injectedJS);
}
$: if(injectedLibraries) {
injectedLibraries_store.set(injectedLibraries);
}
$: if(editor && $currentFile) {
editor.update($currentFile.content);
}
$: if(files && ready) {
$: if(files && $ready) {
manualUpdates = false;
currentFile = files[currentFileIndex];
update();
}
$: if(ready && !manualUpdates) {
$: if($ready && !manualUpdates) {
update();
}
function showFile(fileIndex) {
currentFile.content = currentContent;
currentFileIndex = fileIndex;
}
function showConsole() {
tab = 'console';
}
function showResult() {
tab = 'viewer';
}
$: selectedLayout = layouts.get(layout || 'default')
</script>

<style>
Expand All @@ -131,41 +139,17 @@
}
</style>

<div class="{cssStyles.container}" >
<div class="result-container {cssStyles.resultContainer}">
{#if showEditor}
<div class:hidden="{!showEditor}" class="{cssStyles.editor}">
{#if showFiles}
<div class="{cssStyles.editorActions.container}">
{#each files as { name }, i}
<div class="{cssStyles.editorActions.tabItem}">
<a class:active="{currentFileIndex == i}" class="{cssStyles.editorActions.link}" on:click="{() => showFile(i)}">{name}</a>
</div>
{/each}
</div>
{/if}
<Editor bind:this={editor} on:change={debounceChangeCode}/>
</div>
{/if}
<div class:view-only="{!showEditor}" class="{cssStyles.viewerContainer}">
{#if showTabs}
<div class="{cssStyles.viewerActions.container}">
<div class="{cssStyles.viewerActions.tabItem}">
<a class:active="{tab == 'viewer'}" class="{cssStyles.viewerActions.link}" on:click="{() => showResult()}">Result</a>
</div>
<div class="{cssStyles.viewerActions.tabItem}">
<a class:active="{tab == 'console'}" class="{cssStyles.viewerActions.link}" on:click="{() => showConsole()}">Console</a>
</div>
</div>
{/if}
<div class="{cssStyles.viewerConsoleContainer}">
<div class:hidden="{tab != 'viewer'}" class="{cssStyles.viewer}">
<Viewer bind:ready={ready} {code} {injectedLibraries} {html} {injectedJS} />
</div>
<div class:hidden="{tab != 'console'}" class="{cssStyles.console}">
<Console output={code} />
</div>
</div>
</div>
<svelte:component this={selectedLayout} {cssStyles} >
<div slot="editor">
<Editor bind:this={editor} on:change={debounceChangeCode}/>
</div>
<div slot="viewer">
<Viewer {cssStyles} />
</div>
<div slot="viewer-console">
<ViewerConsole {cssStyles} />
</div>
<div slot="console">
<Console {cssStyles} />
</div>
</div>
</svelte:component>
53 changes: 53 additions & 0 deletions src/Result.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<script>
import srcdoc from './srcdoc/index.js';
import { onMount } from 'svelte';
import {
ready,
injectedJS,
injectedLibraries
} from './stores.js'
let iframe;
export let injectedCSS = '';
export let code;
export let html;
let message = '';
onMount(() => {
iframe.addEventListener('load', () => {
ready.set(true);
});
});
$: if($ready && (code || html)) {
message = `
${$injectedJS}
${styles}
document.body.innerHTML = '';
document.body.innerHTML = '${html}';
${code}
`
iframe.contentWindow.postMessage({ script: message }, '*');
}
$: styles = injectedCSS && `{
const style = document.createElement('style');
style.textContent = ${JSON.stringify(injectedCSS)};
document.head.appendChild(style);
}`;
$: if($injectedLibraries.length > 0) {
libraries = $injectedLibraries.map((lib) => {
return `{
const script = document.createElement('script');
script.type= 'text/javascript';
script.src = '${lib}';
document.head.appendChild(script);
}`
})
}
</script>
<iframe
title="Result"
sandbox="allow-scripts allow-same-origin"
bind:this={iframe}
{srcdoc}
></iframe>
52 changes: 6 additions & 46 deletions src/Viewer.svelte
Original file line number Diff line number Diff line change
@@ -1,48 +1,8 @@
<script>
import srcdoc from './srcdoc/index.js';
import { onMount } from 'svelte';
let iframe;
export let injectedJS = '';
export let injectedCSS = '';
export let injectedLibraries = [];
export let html = '';
export let code = '';
export let ready = false;
let message = '';
onMount(() => {
iframe.addEventListener('load', () => {
ready = true;
});
});
$: if(ready && (code || html)) {
message = `
${injectedJS}
${styles}
document.body.innerHTML = '';
document.body.innerHTML = '${html}';
${code}
`
iframe.contentWindow.postMessage({ script: message }, '*');
}
$: styles = injectedCSS && `{
const style = document.createElement('style');
style.textContent = ${JSON.stringify(injectedCSS)};
document.head.appendChild(style);
}`;
$: if(injectedLibraries.length > 0) {
libraries = injectedLibraries.map((lib) => {
return `{
const script = document.createElement('script');
script.type= 'text/javascript';
script.src = '${lib}';
document.head.appendChild(script);
}`
})
}
import Result from './Result.svelte';
import { code, html } from './stores.js'
</script>
<iframe
title="Result"
sandbox="allow-scripts allow-same-origin"
bind:this={iframe}
{srcdoc}
></iframe>

<Result code={$code} html={$html} />
36 changes: 36 additions & 0 deletions src/ViewerConsole.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<script>
import Viewer from './Viewer.svelte';
import Console from './Console.svelte';
import { code } from './stores.js'
export let cssStyles;
let tab = 'viewer';
function showConsole() {
tab = 'console';
}
function showResult() {
tab = 'viewer';
}
</script>

<div class="{cssStyles.viewerContainer}">
<div class="{cssStyles.viewerActions.container}">
<div class="{cssStyles.viewerActions.tabItem}">
<a class:active="{tab == 'viewer'}" class="{cssStyles.viewerActions.link}" on:click="{() => showResult()}">Result</a>
</div>
<div class="{cssStyles.viewerActions.tabItem}">
<a class:active="{tab == 'console'}" class="{cssStyles.viewerActions.link}" on:click="{() => showConsole()}">Console</a>
</div>
</div>
<div class="{cssStyles.viewerConsoleContainer}">
<div class:hidden="{tab != 'viewer'}" class="{cssStyles.viewer}">
<Viewer />
</div>
<div class:hidden="{tab != 'console'}" class="{cssStyles.console}">
<Console />
</div>
</div>
</div>
28 changes: 28 additions & 0 deletions src/layouts/Default.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script>
import {
files,
currentFileIndex
} from './../stores.js'
export let cssStyles;
function showFile(fileIndex) {
currentFileIndex.set(fileIndex);
}
</script>

<div class="{cssStyles.container}" >
<div class="result-container {cssStyles.resultContainer}">
<div class="{cssStyles.editor}">
<div class="{cssStyles.editorActions.container}">
{#each $files as { name }, i}
<div class="{cssStyles.editorActions.tabItem}">
<a class:active="{$currentFileIndex == i}" class="{cssStyles.editorActions.link}" on:click="{() => showFile(i)}">{name}</a>
</div>
{/each}
</div>
<slot name="editor"></slot>
</div>
<slot name="viewer-console"></slot>
</div>
</div>
Loading

0 comments on commit da2c2d6

Please sign in to comment.