From 9f56cd7ca4a66a3012d1b24e2f272b661a602252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Mass=C3=A9?= Date: Mon, 17 Jul 2017 12:04:22 +0200 Subject: [PATCH] Embeded mode preview for Scastie --- _includes/scastie.html | 10 ++++-- _layouts/frontpage.html | 2 +- resources/js/functions.js | 71 +++++++++++++++++++++++++-------------- 3 files changed, 54 insertions(+), 29 deletions(-) diff --git a/_includes/scastie.html b/_includes/scastie.html index fb7a6ec65..9dbfba9f3 100644 --- a/_includes/scastie.html +++ b/_includes/scastie.html @@ -2,7 +2,13 @@

Run Scala in your browser

-

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

+

+ Scastie is Scala + SBT in your browser! + You can use any version of Scala, or even alternate backends + such as Dotty, Scala.js, Scala Native, and Typelevel Scala. You can use + any published library. You can save and share Scala programs/builds + with anybody. +

@@ -20,4 +26,4 @@

Run Scala in your browser

- \ No newline at end of file +m \ No newline at end of file diff --git a/_layouts/frontpage.html b/_layouts/frontpage.html index c8f9928f0..f64cf8e84 100644 --- a/_layouts/frontpage.html +++ b/_layouts/frontpage.html @@ -173,7 +173,7 @@

{{scalaItem.shortTitle}}

- +{% include scastie.html %}
diff --git a/resources/js/functions.js b/resources/js/functions.js index afd7f5b35..c6507649e 100644 --- a/resources/js/functions.js +++ b/resources/js/functions.js @@ -234,33 +234,52 @@ $(document).ready(function() { // Scala in the browser $(document).ready(function() { - if ($("#scastie-textarea").length) { - var editor = CodeMirror.fromTextArea(document.getElementById("scastie-textarea"), { - lineNumbers: true, - matchBrackets: true, - theme: "monokai", - mode: "text/x-scala", - autoRefresh: true, - fixedGutter: false - }); - editor.setSize("100%", ($("#scastie-code-container").height())); - - var codeSnippet = "List(\"Hello\", \"World\").mkString(\"\", \", \", \"!\")"; - editor.getDoc().setValue(codeSnippet); - editor.refresh(); - - $('.btn-run').click(function() { - // TODO: Code to connect to the scastie server would be here, what follows is just a simulation for the UI elements: - $('.btn-run').addClass("inactive"); - $('.btn-run i').removeClass("fa fa-play").addClass("fa fa-spinner fa-spin"); - setTimeout(function() { - var currentCodeSnippet = editor.getDoc().getValue(); - console.log("Current code snippet: " + currentCodeSnippet); - $('.btn-run').removeClass("inactive"); - $('.btn-run i').removeClass("fa-spinner fa-spin").addClass("fa fa-play"); - }, 2000); - }) + if ($("#scastie-textarea").length) { + var editor = + CodeMirror.fromTextArea( + document.getElementById("scastie-textarea"), + { + // lineNumbers: false, + matchBrackets: true, + theme: "monokai", + mode: "text/x-scala", + autoRefresh: true, + fixedGutter: false, + extraKeys: { + 'Ctrl-Enter': 'run', + 'Cmd-Enter': 'run' + } + } + ); + + editor.setSize("100%", ($("#scastie-code-container").height())); + + var codeSnippet = "List(\"Hello\", \"World\").mkString(\"\", \", \", \"!\")"; + editor.getDoc().setValue(codeSnippet); + editor.refresh(); + + function run(){ + console.log("run"); + // var scastieBaseUrl = "https://scastie.scala-lang.org"; + var scastieBaseUrl = "http://localhost:9000"; + + $.ajax( + { + type: "POST", + url: scastieBaseUrl + '/scala-lang', + data: editor.getDoc().getValue(), + success: function(url) { + window.open(scastieBaseUrl + "/" + url); + }, + // otherwise it's considered a popup + async: false + } + ) } + + $('.btn-run').click(run); + CodeMirror.commands.run = run; + } }); // OS detection