Class: Hyperloop::Console::CodeEditor

Inherits:
CodeMirror show all
Defined in:
lib/hyperloop/console/client_components.rb

Constant Summary

Constants inherited from Hyperloop::Component

Hyperloop::Component::VERSION

Class Attribute Summary collapse

Attributes inherited from CodeMirror

#editor

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.editorObject

Returns the value of attribute editor.



151
152
153
# File 'lib/hyperloop/console/client_components.rb', line 151

def editor
  @editor
end

Class Method Details

.set_focusObject



153
154
155
# File 'lib/hyperloop/console/client_components.rb', line 153

def set_focus
  editor.set_focus_at_end if editor
end

Instance Method Details

#clear_errorObject



223
224
225
226
# File 'lib/hyperloop/console/client_components.rb', line 223

def clear_error
  `#{@error}.clear()` if @error
  @error = nil
end

#mark_error(message) ⇒ Object



206
207
208
209
# File 'lib/hyperloop/console/client_components.rb', line 206

def mark_error(message)
  from, to, title = parse_error(message)
  @error = `#{@editor}.markText(#{from.to_n}, #{to.to_n}, {className: 'syntax-error', title: title})`
end

#move_history(n) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/hyperloop/console/client_components.rb', line 174

def move_history(n)
  @history[@history_pos] = `#{@editor}.getValue()`
  @history_pos += n
  text = @history[@history_pos]
  lines = text.split("\n")
  `#{@editor}.setValue(#{text})`

  after(0) do
    `window.scrollTo(window.scrollX, 99999)`
    `#{@editor}.setCursor({line: #{lines.count}, ch: #{(lines.last || '').length}})`
  end
end

#on_changeObject



228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/hyperloop/console/client_components.rb', line 228

def on_change
  clear_error
  code = `#{@editor}.getValue()`
  if params.context.empty?
    compiled_code = Opal.compile(code, irb: true)
  else
    compiled_code = Opal.compile("#{params.context}.instance_eval {#{code}}")
  end
  DebugConsole.ready!(code, compiled_code)
rescue Exception => e
  mark_error(e.message)
end

#on_key_down(cm, e) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/hyperloop/console/client_components.rb', line 194

def on_key_down(cm, e)
  if `e.metaKey` || `e.ctrlKey`
    return unless `e.keyCode` == 13
    `#{@editor}.setValue(#{@editor}.getValue()+#{"\n"})`
    #on_change
  elsif `e.key` == 'ArrowUp'
    move_history(-1) if @history_pos > 0
  elsif `e.key` == 'ArrowDown'
    move_history(1) if @history_pos < @history.length-1
  end
end

#parse_error(message) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/hyperloop/console/client_components.rb', line 211

def parse_error(message)
  message = message.split("\n")
  position_info = message[3].split(':')
  last_line_number = `#{@editor}.lineCount()`
  last_line = `#{@editor}.getLine(last_line_number-1)`
  [
    {line: 0, ch: 0},
    {line: last_line_number, ch: last_line.length},
    message[2].split(':(file)')[0]
  ]
end

#set_focus_at_endObject



187
188
189
190
191
# File 'lib/hyperloop/console/client_components.rb', line 187

def set_focus_at_end
  `#{@editor}.focus()`
  lines = `#{@editor}.getValue()`.split("\n")
  `#{@editor}.setCursor({line: #{lines.count}, ch: #{(lines.last || '').length}})`
end