Skip to content

Merge changes from #128 #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 14, 2020
Merged
13 changes: 13 additions & 0 deletions client/frame.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Try PureScript!</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="js/frame.js"></script>
</head>
<body>
<main id="main"></main>
</body>
</html>
69 changes: 20 additions & 49 deletions client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,6 @@
</a>
</li>
</ul>
</li><li class="menu-item menu-dropdown no-mobile">
<label title="Select a Backend">Backend</label>
<ul id="backend">
<li>
<input type="radio" name="backend_inputs" value="core" id="backend_core" checked="true">
<label for="backend_core" title="Use the core libraries backend">Core</label>
</li>
<!--
<li>
<input type="radio" name="backend_inputs" value="thermite" id="backend_thermite">
<label for="backend_thermite" title="Use the try-thermite backend">Thermite</label>
</li>
<li>
<input type="radio" name="backend_inputs" value="slides" id="backend_slides">
<label for="backend_slides" title="Use the try-slides backend">Slides</label>
</li>
<li>
<input type="radio" name="backend_inputs" value="flare" id="backend_flare">
<label for="backend_flare" title="Use the try-flare backend">Flare</label>
</li>
<li>
<input type="radio" name="backend_inputs" value="mathbox" id="backend_mathbox">
<label for="backend_mathbox" title="Use the try-mathbox backend">Mathbox</label>
</li>
<li>
<input type="radio" name="backend_inputs" value="behaviors" id="backend_behaviors">
<label for="backend_behaviors" title="Use the try-behaviors backend">Behaviors</label>
</li>
-->
</ul>
</li><li class="menu-item view_gist_li mobile-only">
<a class="view_gist" target="trypurs_gist">
<label title="Open the original gist in a new window">View Gist</label>
Expand Down Expand Up @@ -188,30 +158,31 @@
})(marker));
}

function setupIFrame($ctr, html, js) {
var $iframe = $('<iframe id="output-iframe">');
function setupIFrame($ctr, data) {
var $iframe = $('<iframe sandbox="allow-scripts" id="output-iframe" src="frame.html">');

$ctr
.empty()
.append($iframe);

var iframe = $iframe.get(0).contentWindow.document;
iframe.open();
iframe.write(html);
iframe.close();

var script = iframe.createElement('script');
script.appendChild(iframe.createTextNode(js));

$iframe.ready(function() {
var checkExists = setInterval(function() {
var body = iframe.getElementsByTagName('body')[0];
if (body) {
body.appendChild(script);
clearInterval(checkExists);
}
}, 100);
});
var tries = 0;
var sendSources = setInterval(function() {
// Stop after 10 seconds
if (tries >= 100) {
return clearInterval(sendSources);
}
tries++;
var iframe = $iframe.get(0).contentWindow;
if (iframe) {
iframe.postMessage(data, "*");
} else {
console.warn("Frame is not available");
}
}, 100);

window.addEventListener("message", function() {
clearInterval(sendSources);
}, { once: true });

return $iframe;
}
Expand Down
39 changes: 39 additions & 0 deletions client/js/frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(function() {
function evalSources(sources) {
var modules = {};
function dirname(str) {
var ix = str.lastIndexOf("/");
return ix < 0 ? "" : str.slice(0, ix);
}
function resolvePath(a, b) {
if (b[0] === "." && b[1] === "/") {
return dirname(a) + b.slice(1);
}
if (b[0] === "." && b[1] === "." && b[2] === "/") {
return dirname(dirname(a)) + b.slice(2);
}
return b;
}
return function load(name) {
if (modules[name]) {
return modules[name].exports;
}
function require(path) {
return load(resolvePath(name, path));
}
var module = modules[name] = { exports: {} };
new Function("module", "exports", "require", sources[name])(module, module.exports, require);
return module.exports;
};
}

document.addEventListener("DOMContentLoaded", function() {
window.addEventListener("message", function(event) {
event.source.postMessage("trypurescript", "*");
var file = evalSources(event.data)("<file>");
if (file.main && typeof file.main === "function") {
file.main();
}
}, { once: true });
}, { once: true });
})();
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"build": "pulp build -- --censor-lib --strict",
"bundle": "pulp build -O --to js/index.js && open index.html"
"bundle": "pulp build -O --to js/index.js"
},
"devDependencies": {
"pulp": "^13.0.0",
Expand Down
Loading