Skip to content

Commit 4cd92ca

Browse files
Embeded mode preview for Scastie
see: scalacenter/scastie#269
1 parent 57d6fb2 commit 4cd92ca

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed

_includes/scastie.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
<div class="wrap">
33
<div class="heading-line">
44
<h2><span>Run Scala in your browser</span></h2>
5-
<p class="lead">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer commodo neque eget placerat dapibus. Mauris ullamcorper dui eu pellentesque venenatis. Nam non elit vitae dolor posuere eleifend a facilisis diam</p>
5+
<p class="lead">
6+
Scastie is Scala + SBT in your browser!
7+
You can use any version of Scala, or even alternate backends
8+
such as Dotty, Scala.js, Scala Native, and Typelevel Scala. You can use
9+
any published library. You can save and share Scala programs/builds
10+
with anybody.
11+
</p>
612
</div>
713
</div>
814
<div class="call-to-action action-medium">
@@ -20,4 +26,4 @@ <h2><span>Run Scala in your browser</span></h2>
2026
</div>
2127
</div>
2228
</div>
23-
</section>
29+
</section>m

resources/js/functions.js

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -234,33 +234,52 @@ $(document).ready(function() {
234234

235235
// Scala in the browser
236236
$(document).ready(function() {
237-
if ($("#scastie-textarea").length) {
238-
var editor = CodeMirror.fromTextArea(document.getElementById("scastie-textarea"), {
239-
lineNumbers: true,
240-
matchBrackets: true,
241-
theme: "monokai",
242-
mode: "text/x-scala",
243-
autoRefresh: true,
244-
fixedGutter: false
245-
});
246-
editor.setSize("100%", ($("#scastie-code-container").height()));
247-
248-
var codeSnippet = "List(\"Hello\", \"World\").mkString(\"\", \", \", \"!\")";
249-
editor.getDoc().setValue(codeSnippet);
250-
editor.refresh();
251-
252-
$('.btn-run').click(function() {
253-
// TODO: Code to connect to the scastie server would be here, what follows is just a simulation for the UI elements:
254-
$('.btn-run').addClass("inactive");
255-
$('.btn-run i').removeClass("fa fa-play").addClass("fa fa-spinner fa-spin");
256-
setTimeout(function() {
257-
var currentCodeSnippet = editor.getDoc().getValue();
258-
console.log("Current code snippet: " + currentCodeSnippet);
259-
$('.btn-run').removeClass("inactive");
260-
$('.btn-run i').removeClass("fa-spinner fa-spin").addClass("fa fa-play");
261-
}, 2000);
262-
})
237+
if ($("#scastie-textarea").length) {
238+
var editor =
239+
CodeMirror.fromTextArea(
240+
document.getElementById("scastie-textarea"),
241+
{
242+
// lineNumbers: false,
243+
matchBrackets: true,
244+
theme: "monokai",
245+
mode: "text/x-scala",
246+
autoRefresh: true,
247+
fixedGutter: false,
248+
extraKeys: {
249+
'Ctrl-Enter': 'run',
250+
'Cmd-Enter': 'run'
251+
}
252+
}
253+
);
254+
255+
editor.setSize("100%", ($("#scastie-code-container").height()));
256+
257+
var codeSnippet = "List(\"Hello\", \"World\").mkString(\"\", \", \", \"!\")";
258+
editor.getDoc().setValue(codeSnippet);
259+
editor.refresh();
260+
261+
function run(){
262+
console.log("run");
263+
// var scastieBaseUrl = "https://scastie.scala-lang.org";
264+
var scastieBaseUrl = "http://localhost:9000";
265+
266+
$.ajax(
267+
{
268+
type: "POST",
269+
url: scastieBaseUrl + '/scala-lang',
270+
data: editor.getDoc().getValue(),
271+
success: function(url) {
272+
window.open(scastieBaseUrl + "/" + url);
273+
},
274+
// otherwise it's considered a popup
275+
async: false
276+
}
277+
)
263278
}
279+
280+
$('.btn-run').click(run);
281+
CodeMirror.commands.run = run;
282+
}
264283
});
265284

266285
// OS detection

0 commit comments

Comments
 (0)