-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathjs_controller.js.rb
63 lines (52 loc) · 1.63 KB
/
js_controller.js.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# control the JS (read-only) editor.
class JSController < DemoController
def source
@source ||= findController type: RubyController,
element: document.querySelector(element.dataset.source)
end
async def setup()
await codemirror_ready
# create another editor below the output
@outputDiv = document.createElement('div')
@outputDiv.classList.add('editor', 'js')
element.appendChild(@outputDiv)
@jsEditor = CodeMirror.jsEditor(@outputDiv)
@jspre = element.querySelector('pre.js')
if @jspre
contents = @jspre.value
else
@jspre = document.createElement('pre')
@jspre.classList.add 'js'
element.appendChild(@jspre)
# set initial contents from markdown code area, then hide the code
nextSibling = element.nextElementSibling
if nextSibling and nextSibling.classList.contains('language-js')
contents = nextSibling.textContent.rstrip()
nextSibling.style.display = 'none'
end
end
element.style.display = 'block'
end
# update contents
def contents=(script)
return unless @jsEditor
@jsEditor.dispatch(
changes: {from: 0, to: @jsEditor.state.doc.length, insert: script}
)
@jspre.classList.remove 'exception'
@jspre.style.display = 'none'
@outputDiv.style.display = 'block'
end
# display an error
def exception=(message)
return unless @jsEditor
@jspre.textContent = message
@jspre.classList.add 'exception'
@jspre.style.display = 'block'
@outputDiv.style.display = 'none'
end
# remove editor on disconnect
def teardown()
element.querySelector('.editor.js').remove()
end
end