From a7f04874fc0d3e471254fa568528b0eacdcf512a Mon Sep 17 00:00:00 2001 From: Vladimir Dementyev Date: Tue, 14 May 2024 23:55:38 +0900 Subject: [PATCH] feat: add auto-run: --- Gemfile | 2 +- Gemfile.lock | 10 +++++----- src/app.js | 37 +++++++++++++++++++++++++++++++++++++ src/index.html | 1 + src/shoelace.js | 1 + 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/Gemfile b/Gemfile index 300bb18..5d70b15 100644 --- a/Gemfile +++ b/Gemfile @@ -4,4 +4,4 @@ source "https://rubygems.org" gem "js", "~> 2.5" unless ENV["JS"] == "false" gem "ruby_wasm", "~> 2.5" -gem "ruby-next", "~> 1.0" +gem "ruby-next", "~> 1.0.3" diff --git a/Gemfile.lock b/Gemfile.lock index 9443585..1acc6b7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -5,18 +5,18 @@ GEM diff-lcs (1.5.1) js (2.5.0) paco (0.2.3) - parser (3.3.0.5) + parser (3.3.1.0) ast (~> 2.4.1) racc racc (1.7.3) require-hooks (0.2.2) - ruby-next (1.0.2) + ruby-next (1.0.3) paco (~> 0.2) require-hooks (~> 0.2) - ruby-next-core (= 1.0.2) + ruby-next-core (= 1.0.3) ruby-next-parser (>= 3.2.2.0) unparser (~> 0.6.0) - ruby-next-core (1.0.2) + ruby-next-core (1.0.3) ruby-next-parser (3.2.2.0) parser (>= 3.0.3.1) ruby_wasm (2.5.0) @@ -35,7 +35,7 @@ PLATFORMS DEPENDENCIES js (~> 2.5) - ruby-next (~> 1.0) + ruby-next (~> 1.0.3) ruby_wasm (~> 2.5) BUNDLED WITH diff --git a/src/app.js b/src/app.js index 2b27eca..358e7a7 100644 --- a/src/app.js +++ b/src/app.js @@ -103,6 +103,22 @@ export default class App { this.showEditor("outputEditor"); }); + this.autorunCb = document.getElementById("autorun"); + let refreshDebounceId; + + this.codeEditor.onDidChangeModelContent((ev) => { + if (!this.autorunCb.checked) return; + + if (refreshDebounceId) { + clearTimeout(refreshDebounceId); + } + + refreshDebounceId = setTimeout(() => { + this.refresh(); + refreshDebounceId = undefined; + }, 500); + }); + this.el.addEventListener("change", this.onSelectEditor); this.versionSelect = document.getElementById("versionSelect"); @@ -197,6 +213,23 @@ export default class App { this.loadExampleFromUrl(); } + refresh() { + let newSource; + try { + newSource = this.transpile(this.codeEditor.getValue(), { raise: true }); + } catch (e) { + return; + } + + this.previewEditor.setValue(newSource); + + let { result, output } = this.executeWithOutput(newSource); + + if (result) output += "\n\n> " + result; + + this.outputEditor.setValue(output); + } + transpile(code, opts = {}) { let rubyOptions = "{"; @@ -218,6 +251,10 @@ export default class App { return result; } catch (e) { + if (opts.raise) { + throw e; + } + console.error(e); return e.message; } diff --git a/src/index.html b/src/index.html index 8650aa8..fee4e41 100644 --- a/src/index.html +++ b/src/index.html @@ -72,6 +72,7 @@

Playground

Run + Auto
diff --git a/src/shoelace.js b/src/shoelace.js index fd3936f..1695be7 100644 --- a/src/shoelace.js +++ b/src/shoelace.js @@ -8,3 +8,4 @@ import "@shoelace-style/shoelace/dist/components/dialog/dialog.js"; import "@shoelace-style/shoelace/dist/components/icon/icon.js"; import "@shoelace-style/shoelace/dist/components/button/button.js"; import "@shoelace-style/shoelace/dist/components/input/input.js"; +import "@shoelace-style/shoelace/dist/components/checkbox/checkbox.js";