Octavo API

Main object

Octavo exports an object called octavo into the main JavaScript namespace. The following sections document some of the stuff that you can do with it.

All

octavo.command

Set a new command, e.g.

octavo.command "Category", "command name",
  "Documentation for the command", (element) ->
    element.doSomething()

element is a jQuery object, though depending on the Category value, you might get different things passed to the command such as an Octavo position object or nothing at all.

octavo.shortcuts

This is a mapping of key combinations to functions that do things.

You could use this to map a key combination to a command:

octavo.shortcuts["Ctrl+U"] = (position) ->
  octavo.commands["Category"]["command name"] position.block

At the moment, you can't easily reassign shortcuts to other existing shortcuts. Say you wanted to swap the action of ⌃D and ⌃G for example. You'd have to cache the values and then swap them.

Ctrl_D = octavo.shortcuts["Ctrl+D"]
Ctrl_G = octavo.shortcuts["Ctrl+G"]

octavo.shortcuts["Ctrl+D"] = Ctrl_G
octavo.shortcuts["Ctrl+G"] = Ctrl_D

octavo.silently

Do stuff, but remember where the caret was!

octavo.silently ->
  octavo.commands["Category"]["command name"] position.block

octavo.input

This gets user input and then does something with it using a callback.

octavo.input (value) ->
  console.log "#{value} is what the user entered"

octavo.change

Changes an element.

octavo.change position.block, "diamonds"
# Your ticket is now DIAMONDS

octavo.selectStart
octavo.selectAll
octavo.selectEnd

These take a node or jQuery object and do pretty much what they say on the tin.

octavo.saveCaret
octavo.restoreCaret

These do what octavo.silently does, but in two stages. What kind of crazy buffalo would do such a thing? Well, sometimes you need to defer restoration of the caret—for example when you're making a call to octavo.input. Also, saveCaret and restoreCaret remember the most recent change only, so you don't end up with Rangy accidentally replacing your caret boundary marker and then not being able to restore it.

octavo.editing

A boolean saying whether Octavo is in edit mode or not.

octavo.options.visibleBoundaries
octavo.options.markdownShortcuts

Booleans representing internal options. Don't set these both to true, that would be wacky.

octavo.commands

This is a hash of the commands.

octavo.categories

This maps commands to their documentation. Maybe it should be called octavo.documentation. Too logical? What do you think?

octavo.on
octavo.off
octavo.toggle

Turn Octavo's editor mode on, off, or toggle it.

octavo.blocks
octavo.phrases

This is metadata about what Octavo considers to be block elements and phrasal elements.

Use them as follows:

if element.tagName.toLowerCase() of octavo.blocks
  console.log "#{element.tagName} is an Octavo block"