Skip to content

Commit

Permalink
Add basic support to run console in preview context
Browse files Browse the repository at this point in the history
See #5
  • Loading branch information
priithaamer committed Nov 19, 2016
1 parent b503bcd commit 0198037
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
21 changes: 19 additions & 2 deletions App.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,26 @@ body {
}

.preview-container {
display:inline-flex;
display: inline-flex;
width: 100%;
height: 100vh;
/*height: 100vh;*/
height: 50vh;
}

.console-container {
border-top: 1px solid rgba(0,0,0,0.15);
/*display: inline-flex;*/
width: 100%;
height: 50vh;
font-size: 14px;
box-sizing: border-box;
}

.console-input {
width: 100%;
outline: none;
border: 0;
box-sizing: border-box;
}

.editor-container {
Expand Down
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
</div>
<div class="pane-preview">
<webview class="preview-container" id="preview" src="./preview.html" nodeintegration></webview>
<div class="console-container">
<div class="console-lines"></div>
<input type="text" class="console-input" />
</div>
</div>
</body>

Expand Down
8 changes: 5 additions & 3 deletions preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
</script>

<script>
require('electron').ipcRenderer.on('updater', (event, type, content) => {
const ipcRenderer = require('electron').ipcRenderer;

ipcRenderer.on('updater', (event, type, content) => {
if (type === 'html') {
document.body.innerHTML = content;
}
if (type === 'css') {
document.head.querySelector('#css').textContent = content;
}
if (type === 'javascript') {
eval(content);
eval.call(null, content);
}
})
});
</script>
</html>
19 changes: 19 additions & 0 deletions renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ preview.addEventListener('dom-ready', () => {
updateContents('javascript', jsEditor.doc.getValue());
});

const consoleInput = document.querySelector('.console-input');
const consoleLines = document.querySelector('.console-lines');
consoleInput.addEventListener('keyup', (event) => {
if (event.key === 'Enter') {
const inputValue = event.target.value;

preview.executeJavaScript(inputValue, false, (result) => {
var line1 = document.createElement('div');
line1.innerHTML = inputValue;
consoleLines.appendChild(line1);
var line = document.createElement('div');
line.innerHTML = JSON.stringify(result);
consoleLines.appendChild(line);
});
event.target.value = '';
event.target.focus();
}
});

const {remote} = electron;
const {Menu} = remote;

Expand Down

0 comments on commit 0198037

Please sign in to comment.