Module: Flammarion::Writeable
Defined Under Namespace
Classes: DeferredValue
Instance Attribute Summary collapse
- #engraving ⇒ Object readonly private
Instance Method Summary collapse
-
#break(options = {}) ⇒ Object
Adds a horizontal rule.
-
#button(label, options = {}, &block) ⇒ Object
Adds a clickable button which will call
block
when clicked. -
#button_box(name = "buttonbox") ⇒ Object
Pops up a little floating writeable pane for buttons and stuff.
-
#callback_link(label, options = {}, &block) ⇒ Object
Creates a string representing a hyperlink that when clicked will call the given block.
-
#checkbox(label, options = {}, &block) ⇒ Object
Creates a new checkbox which the user can click.
-
#clear ⇒ Object
Clears the contents of the writeable area.
-
#close ⇒ Object
Closes the pane or window.
-
#dropdown(items, options = {}, &block) ⇒ Object
Creates a dropdown menu for a user to choose a list of options.
-
#embedded_button(label, options = {}, &block) ⇒ Object
Creates a string representing a button will call the given block when it is clicked.
-
#emoji ⇒ Object
Returns the list of currently installed emoji from Emojione.
-
#gets(prompt = "", options = {}) ⇒ String
Prompts the user for a sting.
-
#hide ⇒ Object
Hides (but doesn’t close) the pane.
-
#highlight(text, options = {}) ⇒ Object
Adds a pretty-printed, colorful display of data or code.
-
#html(data) ⇒ Object
Renders raw html into the writeable area by replacing the previous content.
-
#icon(name, additional_classes = []) ⇒ Object
Creates a string representing a emoji or Font Awesome icon.
- #image(url, options = {}) ⇒ Object
-
#input(label, options = {}, &block) ⇒ Object
Creates a new text-input field into which the user can enter text.
- #js(text, options = {}, &block) ⇒ Object
-
#map(*args) ⇒ Object
Adds an interactive street map of a specified location.
-
#markdown(text, options = {}) ⇒ Object
Renders the given markdown text into the pane.
-
#orientation=(orientation) ⇒ Object
Changes the orientation of the panes within this Writeable area.
-
#pane(name, options = {}) ⇒ Pane
Creates a scaled pane within the current writeable area.
-
#plot(data, options = {}) ⇒ Object
Creates a new plot to display data.
-
#puts(str = "", options = {}) ⇒ Object
Adds text to the writeable area and appends a newline.
-
#raw(data) ⇒ Object
Adds raw html to the writeable area without appending a newline.
-
#replace(str, options = {}) ⇒ Object
Replaces the contents of the writeable area with text.
-
#script(text, options = {}, &block) ⇒ Object
Runs a script in the engraving window.
- #script_src(src) ⇒ Object
-
#search(string) ⇒ Object
Searches for and highlights the string in the engraving.
-
#send(str, options = {}) ⇒ Object
(also: #print)
Adds text to the writeable area without appending a newline.
- #send_json(hash) ⇒ Object private
-
#show ⇒ Object
Shows a hidden pane.
-
#status(str, options = {}) ⇒ Object
Displays a message to the bottom status bar.
-
#style(*args) ⇒ Object
Sets CSS styles attributes on the current pane.
-
#subpane(name, options = {}) ⇒ Pane
Creates a writeable area within the current writeable area.
-
#table(rows, options = {}) ⇒ Object
Displays a table of data.
-
#template(file) ⇒ Object
Will render the given Slim template into the Writeable area.
Instance Attribute Details
#engraving ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
6 7 8 |
# File 'lib/flammarion/writeable.rb', line 6 def engraving @engraving end |
Instance Method Details
#break(options = {}) ⇒ Object
Adds a horizontal rule
290 291 292 |
# File 'lib/flammarion/writeable.rb', line 290 def break( = {}) send_json({action:'break'}.merge()) end |
#button(label, options = {}, &block) ⇒ Object
Adds a clickable button which will call block
when clicked
150 151 152 153 154 155 |
# File 'lib/flammarion/writeable.rb', line 150 def (label, = {}, &block) id = @engraving.make_id send_json({action:'button', label:label, id:id}.merge()) @engraving.callbacks[id] = block id end |
#button_box ⇒ Pane #button_box(name) ⇒ Object
Pops up a little floating writeable pane for buttons and stuff.
452 453 454 455 |
# File 'lib/flammarion/writeable.rb', line 452 def (name = "buttonbox") send_json({action:'buttonbox', name:name}) return Pane.new(@engraving, name) end |
#callback_link(label, options = {}, &block) ⇒ Object
Don’t forget to set the :escape_html option to false when including this string.
Creates a string representing a hyperlink that when clicked will call the given block. The string can be included in text for text-accepting methods (such as #puts
, #table
, etc).
173 174 175 176 177 |
# File 'lib/flammarion/writeable.rb', line 173 def callback_link(label, = {}, &block) id = @engraving.make_id @engraving.callbacks[id] = block %|<a href="#" onClick="$ws.send({id:'#{id}', action:'callback', source:'link'})">#{label}</a>| end |
#checkbox(label, options = {}) ⇒ DeferredValue #checkbox(label, options = {}) {|message_hash| ... } ⇒ Object
Creates a new checkbox which the user can click.
276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/flammarion/writeable.rb', line 276 def checkbox(label, = {}, &block) id = @engraving.make_id send_json({action:'checkbox', label:label, id:id}.merge()) if block_given? @engraving.callbacks[id] = block else d = DeferredValue.new d.__setobj__([:value] || ['value']) @engraving.callbacks[id] = Proc.new {|v| d.__setobj__(v["checked"])} return d end end |
#clear ⇒ Object
Clears the contents of the writeable area
105 106 107 108 |
# File 'lib/flammarion/writeable.rb', line 105 def clear send_json({action:'clear'}) return nil end |
#close ⇒ Object
Closes the pane or window
111 112 113 114 |
# File 'lib/flammarion/writeable.rb', line 111 def close send_json({action:'closepane'}) return nil end |
#dropdown(items, options = {}) ⇒ DeferredValue #dropdown(item, options = {}) {|message_hash| ... } ⇒ Object
Creates a dropdown menu for a user to choose a list of options
252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/flammarion/writeable.rb', line 252 def dropdown(items, = {}, &block) id = @engraving.make_id send_json({action:'dropdown', id:id, options:items}.merge()) if block_given? @engraving.callbacks[id] = block else d = DeferredValue.new d.__setobj__(items[0]) if items.is_a? Array d.__setobj__(items[items.keys.first]) if items.is_a? Hash @engraving.callbacks[id] = Proc.new {|v| d.__setobj__ v["value"]} return d end end |
#embedded_button(label, options = {}, &block) ⇒ Object
Don’t forget to set the :escape_html option to false when including this string.
Creates a string representing a button will call the given block when it is clicked. The string can be included in text for text-accepting methods (such as #puts
, #table
, etc).
162 163 164 165 166 |
# File 'lib/flammarion/writeable.rb', line 162 def (label, = {}, &block) id = @engraving.make_id @engraving.callbacks[id] = block %|<a class="floating-button" href="#" onClick="$ws.send({id:'#{id}', action:'callback', source:'embedded_button'})">#{label}</a>| end |
#emoji ⇒ Object
Returns the list of currently installed emoji from Emojione
535 536 537 538 |
# File 'lib/flammarion/writeable.rb', line 535 def emoji @@emoji ||= JSON.parse(File.read(File.join(File.dirname(__FILE__), "../html/source/javascripts/vendor/emojione.js")).each_line.find{|l| l.start_with?(" ns.emojioneList")}.scan(/= (\{[^;]+);/).flatten.first) return @@emoji end |
#gets(prompt = "", options = {}) ⇒ String
Prompts the user for a sting. Blocks until a string has been entered.
488 489 490 491 492 493 |
# File 'lib/flammarion/writeable.rb', line 488 def gets(prompt = "", = {}) str = nil input(prompt, {once:true, focus:true}.merge()) {|msg| str = msg["text"]} sleep 0.1 while str.nil? return str end |
#hide ⇒ Object
Hides (but doesn’t close) the pane. This allows the pane to be written to without it opening up again.
386 387 388 |
# File 'lib/flammarion/writeable.rb', line 386 def hide send_json({action:'hidepane'}) end |
#highlight(data, options) ⇒ Object #highlight(text, options) ⇒ Object
Adds a pretty-printed, colorful display of data or code
137 138 139 140 141 142 |
# File 'lib/flammarion/writeable.rb', line 137 def highlight(text, = {}) output = text output = JSON.pretty_generate(text) if text.is_a? Hash or text.is_a? Array send_json({action:'highlight', text:output}.merge()) nil end |
#html(data) ⇒ Object
Renders raw html into the writeable area by replacing the previous content
295 296 297 |
# File 'lib/flammarion/writeable.rb', line 295 def html(data) send_json({action:'replace', text:data, raw:true}) end |
#icon(name, additional_classes = []) ⇒ Object
Don’t forget to set the :escape_html option to false when including this string.
Creates a string representing a emoji or Font Awesome icon. The string can be included in text for text-accepting methods (such as #puts
, #table
, etc).
184 185 186 187 188 189 190 191 |
# File 'lib/flammarion/writeable.rb', line 184 def icon(name, additional_classes = []) e = emoji[":#{name}:"] if e.nil? then return %|<i class="fa fa-#{name} #{additional_classes.collect{|c| "fa-#{c}"}.join(" ")}"></i>| else return %|<img class="emoji" alt="#{name}" src="images/emoji/#{e['unicode'].last.downcase}.png">| end end |
#image(url, options = {}) ⇒ Object
193 194 195 |
# File 'lib/flammarion/writeable.rb', line 193 def image(url, = {}) puts(%|<img src="#{url}">|, {escape_html: false}.merge()) end |
#input(label, options = {}) ⇒ DeferredValue #input(label, options = {}) {|message_hash| ... } ⇒ Object
Creates a new text-input field into which the user can enter text. If a block is given, the block will be called when ever the text is changed. If a block is not given, it will return a DeferredValue object which can be used to get the value at any time.
230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/flammarion/writeable.rb', line 230 def input(label, = {}, &block) id = @engraving.make_id send_json({action:'input', label:label, id:id}.merge()) if block_given? @engraving.callbacks[id] = block else d = DeferredValue.new d.__setobj__([:value] || ['value']) @engraving.callbacks[id] = Proc.new {|v| d.__setobj__ v["text"] } return d end end |
#js(text, options = {}, &block) ⇒ Object
319 320 321 |
# File 'lib/flammarion/writeable.rb', line 319 def js(text, = {}, &block) script(text, .merge(coffee:false), &block) end |
#map(options) ⇒ Object #map(address, options = {}) ⇒ Object #map(latitude, longitude, options = {}) ⇒ Object
Street map provided by openstreetmap.org
Adds an interactive street map of a specified location.
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/flammarion/writeable.rb', line 507 def map(*args) case (args.size) when 1 if args[0].respond_to? :keys then = args[0] else = {address:args[0].to_s} end when 2 if args[1].respond_to? :keys then = {address:args[0]}.merge(args[1]) else = {latitude:args[0], longitude:args[1]} end when 3 = {latitude:args[0], longitude:args[1]}.merge(args[2]) else raise ArgumentError.new("Expected 1..3 arguments") end send_json({action:'map'}.merge()) end |
#markdown(text, options = {}) ⇒ Object
Renders the given markdown text into the pane.
368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/flammarion/writeable.rb', line 368 def markdown(text, = {}) # if defined?(Redcarpet::Markdown) then # markdown_html = Redcarpet::Markdown.new(Redcarpet::Render::HTML, { # tables: true, # fenced_code_blocks: true, # autolink: true, # strikethrough: true, # superscript: true, # }.merge(options[:markdown_extensions] || {})).render(text) # else # markdown_html = Kramdown::Document.new(text, {input: 'GFM', hard_wrap: false, syntax_highlighter: nil}.merge(options)).to_html # end send_json({action:'markdown', text: text, hard_wrap:false}.merge()) end |
#orientation=(orientation) ⇒ Object
This applies only to panes, not to subpanes.
Changes the orientation of the panes within this Writeable area. If orientation
is :vertical
(the default), the panes will be stacked on top of each other, each occupying 1/nth of the height (where n is the number of panes) and 100% of the width. If orientation
is :horizontal
, the panes will be placed next to each other, with each pane occupying 1/n of the width (where n is the number of panes) and 100% of the heigh.
442 443 444 445 |
# File 'lib/flammarion/writeable.rb', line 442 def orientation=(orientation) raise ArgumentError.new("Orientation must be :horizontal or :vertical") unless [:horizontal, :vertical].include?(orientation) send_json({action:'reorient', orientation:orientation}) end |
#pane(name, options = {}) ⇒ Pane
The difference between pane and subpane is that a pane is automatically scaled depending on the number of other panes and the current orientation, while subpanes are automatically the size of their contents. Another way to think about it might be that pane creates structural layout elements, while subpane creates embedded sections within other panes.
Creates a scaled pane within the current writeable area. Where it goes depends on the orientation.
427 428 429 430 |
# File 'lib/flammarion/writeable.rb', line 427 def pane(name, = {}) send_json({action:'addpane', name:name}.merge()) return Pane.new(@engraving, name) end |
#plot(data, options = {}) ⇒ Object
Creates a new plot to display data. Uses Plotly javascript library for plotting, options are passed directly to Plotly, so all types of plots and options are supported.
121 122 123 124 125 126 |
# File 'lib/flammarion/writeable.rb', line 121 def plot(data, = {}) id = @engraving.make_id p = Plot.new(id, @pane_name, @engraving) p.plot(data, ) return p end |
#puts(str = "", options = {}) ⇒ Object
Adds text to the writeable area and appends a newline.
83 84 85 86 87 |
# File 'lib/flammarion/writeable.rb', line 83 def puts(str = "", = {}) send str, send "\n" return nil end |
#raw(data) ⇒ Object
Adds raw html to the writeable area without appending a newline.
92 93 94 |
# File 'lib/flammarion/writeable.rb', line 92 def raw(data) send data, raw: true end |
#replace(str, options = {}) ⇒ Object
Replaces the contents of the writeable area with text
99 100 101 102 |
# File 'lib/flammarion/writeable.rb', line 99 def replace(str, = {}) send_json({action:'replace', text:str}.merge()) return nil end |
#script(text, options = {}, &block) ⇒ Object
Runs a script in the engraving window.
304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/flammarion/writeable.rb', line 304 def script(text, = {}, &block) return script_src(text) if !.fetch(:no_file, false) && File.exist?(text) data = .fetch(:coffee, true) ? CoffeeScript.compile(text) : text id = @engraving.make_id d = nil if block_given? @engraving.callbacks[id] = block else d = DeferredValue.new @engraving.callbacks[id] = Proc.new {|v| d.__setobj__(v["result"])} end send_json({action:'script', data:data, id: id}.merge()) return d end |
#script_src(src) ⇒ Object
323 324 325 326 327 328 329 |
# File 'lib/flammarion/writeable.rb', line 323 def script_src(src) if File.exist?(src) then html("<script>#{File.read(src)}</script>") else html("<script src='#{src}'></script>") end end |
#search(string) ⇒ Object
Searches for and highlights the string in the engraving.
530 531 532 |
# File 'lib/flammarion/writeable.rb', line 530 def search(string) send_json({action:'search', text: string}) end |
#send(str, options = {}) ⇒ Object Also known as: print
Adds text to the writeable area without appending a newline.
75 76 77 |
# File 'lib/flammarion/writeable.rb', line 75 def send(str, = {}) @engraving.send_json({action:'append', text:str, target:@pane_name}.merge()) end |
#send_json(hash) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
68 69 70 |
# File 'lib/flammarion/writeable.rb', line 68 def send_json(hash) @engraving.send_json({target: @pane_name}.merge(hash)) end |
#show ⇒ Object
Shows a hidden pane.
392 393 394 |
# File 'lib/flammarion/writeable.rb', line 392 def show send_json({action:'showpane'}) end |
#status(str, position) ⇒ Object #status(str, options = {}) ⇒ Object
Displays a message to the bottom status bar.
464 465 466 467 |
# File 'lib/flammarion/writeable.rb', line 464 def status(str, = {}) = {position: } if .is_a? Symbol @engraving.send_json({action:'status', text: str}.merge()) end |
#style(attribute, value) ⇒ Object #style(attributes) ⇒ Object
Sets CSS styles attributes on the current pane.
339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/flammarion/writeable.rb', line 339 def style(*args) case args.length when 1 # @todo Pass this as a whole hash, rather than individually. args[0].each{|a,v| send_json({action: 'style', attribute: a, value: v})} when 2 send_json({action: 'style', attribute: args[0], value: args[1]}) else raise ArgumentError.new("Invalid number of arguments (Expected 1 or 2)") end end |
#subpane(name, options = {}) ⇒ Pane
The difference between pane and subpane is that a pane is automatically scaled depending on the number of other panes and the current orientation, while subpanes are automatically the size of their contents. Another way to think about it might be that pane creates structural layout elements, while subpane creates embedded sections within other panes.
Creates a writeable area within the current writeable area. This lets you update the contents of the writeable later, without disturbing the rest of the curent pane. If a pane or subpane with the given name already exists, it will just use that one instead.
414 415 416 417 |
# File 'lib/flammarion/writeable.rb', line 414 def subpane(name, = {}) send_json({action:'subpane', name:name}.merge()) return Pane.new(@engraving, name) end |
#table(rows, options = {}) ⇒ Object
Displays a table of data
479 480 481 |
# File 'lib/flammarion/writeable.rb', line 479 def table(rows, = {}) send_json({action:'table', rows: rows}.merge()) end |
#template(file) ⇒ Object
If you just want to include html (or outut from another template engine, you can just use #html)
Will render the given Slim template into the Writeable area. This is useful for creating complex or custom application layouts components beyond what’s built into Flammarion.
358 359 360 361 |
# File 'lib/flammarion/writeable.rb', line 358 def template(file) data = Slim::Template.new(file).render send_json({action:'replace', text:data, raw:true}) end |