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.
-
#icon(name, additional_classes = []) ⇒ Object
Creates a string representing a Emojione 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
-
#live_reload_template(file) ⇒ Object
Reloads a template every time the file changes.
-
#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.
-
#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.
4 5 6 |
# File 'lib/flammarion/writeable.rb', line 4 def engraving @engraving end |
Instance Method Details
#break(options = {}) ⇒ Object
Adds a horizontal rule
278 279 280 |
# File 'lib/flammarion/writeable.rb', line 278 def break( = {}) send_json({action:'break'}.merge()) end |
#button(label, options = {}, &block) ⇒ Object
Adds a clickable button which will call block
when clicked
141 142 143 144 145 146 |
# File 'lib/flammarion/writeable.rb', line 141 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.
443 444 445 446 |
# File 'lib/flammarion/writeable.rb', line 443 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).
164 165 166 167 168 |
# File 'lib/flammarion/writeable.rb', line 164 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.
264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/flammarion/writeable.rb', line 264 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
96 97 98 99 |
# File 'lib/flammarion/writeable.rb', line 96 def clear send_json({action:'clear'}) return nil end |
#close ⇒ Object
Closes the pane or window
102 103 104 105 |
# File 'lib/flammarion/writeable.rb', line 102 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
240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/flammarion/writeable.rb', line 240 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).
153 154 155 156 157 |
# File 'lib/flammarion/writeable.rb', line 153 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
526 527 528 529 |
# File 'lib/flammarion/writeable.rb', line 526 def emoji @@emoji ||= JSON.parse(File.read(File.join(File.dirname(__FILE__), "../html/build/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.
479 480 481 482 483 484 |
# File 'lib/flammarion/writeable.rb', line 479 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.
377 378 379 |
# File 'lib/flammarion/writeable.rb', line 377 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
128 129 130 131 132 133 |
# File 'lib/flammarion/writeable.rb', line 128 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
283 284 285 |
# File 'lib/flammarion/writeable.rb', line 283 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 Emojione or Font Awesome icon. The string can be included in text for text-accepting methods (such as #puts
, #table
, etc).
175 176 177 178 179 180 181 182 |
# File 'lib/flammarion/writeable.rb', line 175 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="emojione" alt="#{name}" src="images/emoji/#{e['unicode'].last.downcase}.png">| end end |
#image(url, options = {}) ⇒ Object
184 185 186 |
# File 'lib/flammarion/writeable.rb', line 184 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.
219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/flammarion/writeable.rb', line 219 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 @engraving.callbacks[id] = Proc.new {|v| d.__setobj__ v["text"] } return d end end |
#js(text, options = {}, &block) ⇒ Object
307 308 309 |
# File 'lib/flammarion/writeable.rb', line 307 def js(text, = {}, &block) script(text, .merge(coffee:false), &block) end |
#live_reload_template(file) ⇒ Object
Reloads a template every time the file changes. This is mostly just useful when developing a template file.
354 355 356 |
# File 'lib/flammarion/writeable.rb', line 354 def live_reload_template(file) FileWatcher.new(file).watch {|file| template(file) } 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.
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
# File 'lib/flammarion/writeable.rb', line 498 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.
363 364 365 366 367 368 369 370 371 372 |
# File 'lib/flammarion/writeable.rb', line 363 def markdown(text, = {}) markdown_html = Redcarpet::Markdown.new(Redcarpet::Render::HTML, { tables: true, fenced_code_blocks: true, autolink: true, strikethrough: true, superscript: true, }.merge([:markdown_extensions] || {})).render(text) send_json({action:'markdown', text: markdown_html}.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.
433 434 435 436 |
# File 'lib/flammarion/writeable.rb', line 433 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.
418 419 420 421 |
# File 'lib/flammarion/writeable.rb', line 418 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.
112 113 114 115 116 117 |
# File 'lib/flammarion/writeable.rb', line 112 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.
81 82 83 84 85 |
# File 'lib/flammarion/writeable.rb', line 81 def puts(str = "", = {}) send str, send "\n" return nil end |
#replace(str, options = {}) ⇒ Object
Replaces the contents of the writeable area with text
90 91 92 93 |
# File 'lib/flammarion/writeable.rb', line 90 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.
292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/flammarion/writeable.rb', line 292 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
311 312 313 314 315 316 317 |
# File 'lib/flammarion/writeable.rb', line 311 def script_src(src) if File.exist?(src) then html("<script>#{File.read(src)}</script>") else html("<script src='#{src}'></src>") end end |
#search(string) ⇒ Object
Searches for and highlights the string in the engraving.
521 522 523 |
# File 'lib/flammarion/writeable.rb', line 521 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.
73 74 75 |
# File 'lib/flammarion/writeable.rb', line 73 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.
66 67 68 |
# File 'lib/flammarion/writeable.rb', line 66 def send_json(hash) @engraving.send_json({target: @pane_name}.merge(hash)) end |
#show ⇒ Object
Shows a hidden pane.
383 384 385 |
# File 'lib/flammarion/writeable.rb', line 383 def show send_json({action:'showpane'}) end |
#status(str, position) ⇒ Object #status(str, options = {}) ⇒ Object
Displays a message to the bottom status bar.
455 456 457 458 |
# File 'lib/flammarion/writeable.rb', line 455 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.
327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/flammarion/writeable.rb', line 327 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.
405 406 407 408 |
# File 'lib/flammarion/writeable.rb', line 405 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
470 471 472 |
# File 'lib/flammarion/writeable.rb', line 470 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.
346 347 348 349 |
# File 'lib/flammarion/writeable.rb', line 346 def template(file) data = Slim::Template.new(file).render send_json({action:'replace', text:data, raw:true}) end |