Skip to content

Commit f7d1a40

Browse files
Encode engine info into url to restore playground state
1 parent 33e8444 commit f7d1a40

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

source/javascripts/try_ruby.js.rb

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ def name
7474
"Opal #{Opal::VERSION}"
7575
end
7676

77+
def engine_id
78+
"opal"
79+
end
80+
7781
def run(source, writer)
7882
# Compile
7983
js_code = Opal.compile(source)
@@ -104,6 +108,10 @@ def name
104108
"CRuby #{@version}"
105109
end
106110

111+
def engine_id
112+
"cruby-#{@version}"
113+
end
114+
107115
def wasm_module
108116
return @module if @module
109117
%x{
@@ -154,13 +162,15 @@ def run(source, writer)
154162
end
155163
end
156164

157-
ENGINES = {
158-
opal: OpalEngine.new,
159-
cruby: CRubyEngine.new(
165+
ENGINES = [
166+
OpalEngine.new,
167+
CRubyEngine.new(
160168
"https://cdn.jsdelivr.net/npm/ruby-wasm-wasi@0.1.2/dist/ruby.wasm",
161169
"3.2.0dev"
162170
),
163-
}
171+
].each_with_object({}) do |engine, hash|
172+
hash[engine.engine_id] = engine
173+
end
164174
end
165175

166176

@@ -214,8 +224,6 @@ def initialize
214224
theme: 'tomorrow-night-eighties',
215225
)
216226

217-
@engine = RubyEngine::ENGINES[DEFAULT_RUBY_ENGINE]
218-
219227
# Bind run button
220228
$document.on(:click, '#btn_run') { do_run }
221229

@@ -235,8 +243,9 @@ def initialize_menu
235243
def initialize_playground
236244
@playground = true
237245

238-
code = get_code_from_url
246+
code, selected_engine = get_state_from_url
239247
@editor.value = code || INITIAL_TRY_CODE.strip
248+
update_engine(selected_engine || DEFAULT_RUBY_ENGINE)
240249

241250
$document.on(:click, '#btn_copy_url') { do_copy_url }
242251
$window.on(:hashchange) { on_hash_change }
@@ -246,6 +255,7 @@ def initialize_playground
246255
end
247256

248257
def initialize_tutorial
258+
@engine = RubyEngine::ENGINES[DEFAULT_RUBY_ENGINE]
249259
$document.on(:click, '#btn_copy') { do_copy }
250260
$document.on(:click, '#btn_next') { do_next }
251261
$document.on(:click, '#btn_back') { do_back }
@@ -351,16 +361,24 @@ def create_engine_options
351361
options = ''
352362

353363
RubyEngine::ENGINES.each do |key, engine|
354-
options += "<option value=\"#{key}\" #{key == DEFAULT_RUBY_ENGINE ? "selected" : ""}>#{engine.name}</option>\n"
364+
options += "<option value=\"#{key}\" #{key == @engine.engine_id ? "selected" : ""}>#{engine.name}</option>\n"
355365
end
356366

357367
engine_element = $document.at_css('#tryruby-engine')
358368
engine_element.inner_html = options
359369
$document.on(:change, '#tryruby-engine') {
360-
@engine = RubyEngine::ENGINES[engine_element.value]
370+
update_engine(engine_element.value)
371+
$window.history.replace(gen_url)
361372
}
362373
end
363374

375+
def update_engine(engine)
376+
@engine = RubyEngine::ENGINES.fetch(engine) do
377+
STDERR.puts "Selected engine id '#{engine}' is unknown. Fallback to default engine '#{DEFAULT_RUBY_ENGINE}'."
378+
RubyEngine::ENGINES[DEFAULT_RUBY_ENGINE]
379+
end
380+
end
381+
364382
def get_cookie(key)
365383
$document.cookies[key]
366384
end
@@ -449,11 +467,10 @@ def do_change_lang(event)
449467
end
450468

451469
# Playground methods
452-
def get_code_from_url
470+
def get_state_from_url
453471
hash = $document.location.fragment.to_s
454-
hash = Browser::FormData.decode(hash.gsub('+', ' '))
455-
456-
hash.delete_prefix("#code=") if hash.start_with?('#code=')
472+
hash = Browser::FormData.parse_query(hash.gsub('+', ' '))
473+
[hash['code'], hash['engine']]
457474
end
458475

459476
def do_copy_url
@@ -462,13 +479,14 @@ def do_copy_url
462479

463480
def gen_url
464481
prefix = $document.location.uri.split("#").first
465-
suffix = Browser::FormData.build_query(code: @editor.value).gsub("%20", "+")
482+
suffix = Browser::FormData.build_query(code: @editor.value, engine: @engine.engine_id).gsub("%20", "+")
466483

467484
"#{prefix}##{suffix}"
468485
end
469486

470487
def on_hash_change
471-
@editor.value = get_code_from_url
488+
@editor.value, engine = get_state_from_url
489+
update_engine(engine) if engine
472490
end
473491

474492
def on_editor_change

0 commit comments

Comments
 (0)