2016-03-12 13:50:43 -05:00
|
|
|
"use strict";
|
|
|
|
|
2016-04-09 14:40:28 -04:00
|
|
|
//! {"require": ["ui"]}
|
|
|
|
|
2016-04-04 16:26:01 -04:00
|
|
|
terminal.setEcho(false);
|
|
|
|
terminal.setTitle("Live TurtleScript");
|
|
|
|
|
2016-04-09 14:40:28 -04:00
|
|
|
let gEditEvent;
|
2016-04-04 16:26:01 -04:00
|
|
|
|
2016-04-09 14:40:28 -04:00
|
|
|
function back() {
|
2016-04-04 16:26:01 -04:00
|
|
|
terminal.split([{name: "terminal"}]);
|
2016-04-09 14:40:28 -04:00
|
|
|
gEditEvent.back();
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
2016-04-04 16:26:01 -04:00
|
|
|
core.register("onWindowMessage", function(event) {
|
|
|
|
if (event.message.ready) {
|
2016-04-09 14:40:28 -04:00
|
|
|
terminal.postMessageToIframe("iframe", {title: gEditEvent.name, contents: gEditEvent.value});
|
2016-04-04 16:26:01 -04:00
|
|
|
} else if (event.message.index) {
|
2016-04-09 14:40:28 -04:00
|
|
|
back();
|
2016-04-04 16:26:01 -04:00
|
|
|
} else {
|
2016-04-09 14:40:28 -04:00
|
|
|
gEditEvent.save(event.message.title, event.message.contents).then(back);
|
2016-04-04 16:26:01 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-09 14:40:28 -04:00
|
|
|
function editPage(event) {
|
|
|
|
gEditEvent = event;
|
2016-04-04 16:26:01 -04:00
|
|
|
terminal.split([{name: "terminal", type: "vertical"}]);
|
|
|
|
terminal.clear();
|
|
|
|
terminal.print({iframe: `<html>
|
|
|
|
<head>
|
|
|
|
<script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.13.2/codemirror.min.js"></script>
|
|
|
|
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.13.2/codemirror.min.css"></link>
|
|
|
|
<style>
|
|
|
|
html {
|
|
|
|
height: 100%;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
}
|
|
|
|
body {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
height: 100%;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
}
|
|
|
|
#menu {
|
|
|
|
flex: 0 0 auto;
|
|
|
|
}
|
|
|
|
#container {
|
|
|
|
flex: 1 1 auto;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
width: 100%;
|
|
|
|
background-color: white;
|
|
|
|
}
|
|
|
|
.CodeMirror {
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
}
|
|
|
|
.CodeMirror-scroll {
|
|
|
|
}
|
|
|
|
#edit { background-color: white }
|
|
|
|
#preview { background-color: white }
|
|
|
|
#edit, #preview {
|
|
|
|
display: flex;
|
|
|
|
overflow: auto;
|
|
|
|
flex: 0 0 50%;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<script><!--
|
|
|
|
var gEditor;
|
|
|
|
function index() {
|
|
|
|
parent.postMessage({index: true}, "*");
|
|
|
|
}
|
|
|
|
function submit() {
|
|
|
|
parent.postMessage({
|
|
|
|
title: document.getElementById("title").value,
|
|
|
|
contents: gEditor.getValue(),
|
|
|
|
}, "*");
|
|
|
|
}
|
|
|
|
function textChanged() {
|
|
|
|
var preview = document.getElementById("preview");
|
|
|
|
while (preview.firstChild) {
|
|
|
|
preview.removeChild(preview.firstChild);
|
|
|
|
}
|
|
|
|
var iframe = document.createElement("iframe");
|
|
|
|
iframe.setAttribute('srcdoc', '<script src="https://codeheart.williams.edu/turtle/turtle.min.js?">-*- javascript -*-</script><script>' + gEditor.getValue() + '</script>');
|
|
|
|
iframe.setAttribute('style', 'width: 100vw; height: 100vh; border: 0');
|
|
|
|
preview.appendChild(iframe);
|
|
|
|
}
|
|
|
|
--></script>
|
|
|
|
<script>
|
|
|
|
window.addEventListener("message", function(message) {
|
|
|
|
document.getElementById("title").value = message.data.title;
|
|
|
|
gEditor.setValue(message.data.contents);
|
|
|
|
textChanged();
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
window.addEventListener("load", function() {
|
|
|
|
gEditor = CodeMirror.fromTextArea(document.getElementById("contents"), {
|
|
|
|
lineNumbers: true
|
|
|
|
});
|
|
|
|
gEditor.on("change", textChanged);
|
|
|
|
document.getElementById("preview").addEventListener("error", function(error) {
|
|
|
|
console.debug("onerror: " + error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
parent.postMessage({ready: true}, "*");
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="menu">
|
|
|
|
<input type="button" value="Back" onclick="index()">
|
|
|
|
` + (core.user.credentials.permissions.authenticated ? `
|
|
|
|
<input type="button" value="Save" onclick="submit()">
|
|
|
|
` : "") +
|
|
|
|
` <a target="_top" href="http://codeheartjs.com/turtle/">TurtleScript</a>
|
|
|
|
<input type="text" id="title" oninput="textChanged()">
|
|
|
|
</div>
|
|
|
|
<div id="container">
|
|
|
|
<div id="edit"><textarea id="contents" oninput="textChanged()"></textarea></div>
|
|
|
|
<div id="preview"></div>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>`, name: "iframe", style: "flex: 1 1 auto; border: 0; width: 100%"});
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
|
2016-04-09 14:40:28 -04:00
|
|
|
require("ui").fileList({
|
|
|
|
title: "Live TurtleScript",
|
|
|
|
edit: editPage,
|
|
|
|
});
|