Skip to content

Commit

Permalink
Merge pull request #31 from milafrerichs/be-just-a-repl
Browse files Browse the repository at this point in the history
Be just a repl
  • Loading branch information
milafrerichs authored Jul 27, 2019
2 parents 3a95864 + ff6f6cf commit 4d6fb8c
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 229 deletions.
16 changes: 14 additions & 2 deletions example/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
}
</style>
<script>
import Tutorials from '../src/Tutorials.svelte';
import Repl from '../src/Repl.svelte';
let chapters = [
{
title: 'Entry',
Expand Down Expand Up @@ -69,8 +69,20 @@
prev: 'prev'
}
}
let files = [
{
name: 'index.html',
content: '<h1>Test</h1>',
type: 'html'
},
{
name: 'index.js',
content: '',
type: 'js'
}
];
</script>

<Tutorials {chapters} />
<Repl {files} />

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "svelte-javascript-tutorials",
"svelte": "src/Tutorials.svelte",
"name": "javascript-repl",
"svelte": "src/Repl.svelte",
"module": "index.mjs",
"version": "0.1.2",
"version": "0.2.0",
"description": "",
"main": "index.js",
"author": "Mila Frerichs <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const name = pkg.name
.replace(/-\w/g, m => m[1].toUpperCase());

export default {
input: 'src/Tutorials.svelte',
input: 'src/Repl.svelte',
output: [
{ file: pkg.module, 'format': 'es' },
{ file: pkg.main, 'format': 'umd', name }
Expand Down
135 changes: 135 additions & 0 deletions src/Repl.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<script>
import Viewer from './Viewer.svelte';
import Editor from './Editor.svelte';
import Console from './Console.svelte';
let ready = false;
let editor;
let manualUpdates = false;
let tab = 'viewer';
let currentFile = {};
let currentFileIndex = 0;
let currentContent = '';
let code = '';
let html = '';
export let viewOnly = false;
export let files = [];
export let injectedLibraries = [];
export let injectedJS = '';
export let cssStyles = {
container: 'container',
resultContainer: 'result-container',
viewerContainer: 'viewer-container',
viewerConsoleContainer: 'viewer-console-container',
editorActions: {
container: '',
tabItem: '',
link: ''
},
viewerActions: {
container: '',
tabItem: '',
link: ''
},
editor: 'editor',
viewer: 'viewer',
};
function changeCode(event) {
currentContent = event.detail.value;
manualUpdates = true;
if (currentFile.type === 'js') {
code = currentContent;
} else {
html = currentContent.replace(/\n/g,'');
}
}
function getContentForType(type = 'js') {
return files.reduce((content, file) => {
if(file.type === type) {
return content + file.content;
}
return content;
}, '');
}
$: currentFile = files[currentFileIndex]
$: if(editor && currentFile) {
editor.update(currentFile.content);
}
$: if(ready && !manualUpdates) {
code = getContentForType('js')
html = getContentForType('html')
if(!html) html = '';
if(editor) {
editor.update(currentFile.content);
}
}
function showFile(fileIndex) {
currentFile.content = currentContent;
currentFileIndex = fileIndex;
}
function showConsole() {
tab = 'console';
}
function showResult() {
tab = 'viewer';
}
</script>

<style>
.hidden {
visibility: hidden;
}
.content-container {
transition-property: all;
transition-duration: .5s;
transition-timing-function: cubic-bezier(0, 1, 0.5, 1);
}
.hide-content .content-container{
max-width: 0;
overflow-x: hidden;
}
.hide-content .result-container {
width: 95%;
}
</style>

<div class="{cssStyles.container}" >
<div class="result-container {cssStyles.resultContainer}">
{#if !viewOnly }
<div class:hidden="{viewOnly}" 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>
<Editor bind:this={editor} on:change={changeCode}/>
</div>
{/if}
<div class:view-only="{viewOnly}" 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 bind:ready {code} {injectedLibraries} {html} {injectedJS} />
</div>
<div class:hidden="{tab != 'console'}" class="{cssStyles.console}">
<Console bind:ready output={code} />
</div>
</div>
</div>
</div>
</div>
Loading

0 comments on commit 4d6fb8c

Please sign in to comment.