We use cookies to make our website more effective. By using our website you agree to our privacy policy.

Getting Started

with Aloha Editor

Leap 1: Include & Invoke Aloha

Leap 2: Add & Bind a Button

Leap 3: Add More Buttons

Leap 4: Multiple Editables

Leap 5: Update Button States

Leap 6: Custom Commands


Leap 1: Include & Invoke Aloha

To replace ContentEditable with Aloha Editor is as simple as 1 line of markup and 1 line of JavaScript:

<script src="http://www.alohaeditor.org/download/aloha.min.js"></script>

aloha() transforms the element given to it into a rich-text editing canvas.

Deactivate editing by invoking aloha.mahalo() on the same element that was given to aloha().

You can initialize multiple editables at once with this line of code:

aloha.dom.query('.editable', document).forEach(<&aloha&>);

Once an editable is initialized you will see that Aloha Editor uses a blinking DIV element as the caret. This caret is the minimal visual user interface (which you can style and animate with CSS, by the way).

Sooner or later you'll need more for UI than just a caret, so the next section shows you how to add a simple bold button to your UI.

Leap 2: Add & Bind a Button

These instructions will show you how to add a button that, when clicked, will format the selected content in an editable to bold.

Begin by putting the following markup somewhere in the body your document:

<button id="big-bad-bold-button">B</button>

The next step is to bind the bold command to this element.

NB: We will use the jQuery API for these code snippets because it's terse and familiar to most. This is not a requirement for Aloha Editor, however; you are free to pick your own poison.
$('#big-bad-bold-button') .on('click', <&aloha.ui.command&>(<&aloha.ui.commands.bold&>));

The two notable components of the code above are aloha.ui.command() and aloha.ui.commands.bold.

aloha.ui.command is a function that generates an event handler, and uses a map of properties—defined through aloha.ui.commands.bold—to determine what that event handler should do when the button is clicked. In this case, any content that is selected inside of an editable when this button is clicked will be formatted bold.

Try it in this editable...

There are a number of other formatting commands that come as defaults with Aloha Editor. You've just seen the aloha.ui.commands.bold command in action, but there are a few more. The next section explains a convenient way to bind multiple buttons.

Leap 3: Add More Buttons

aloha.ui.commands contains a set of editing commands that can be bound to elements in order to be executed from event handlers, which aloha.ui.commands.bold is one of.

These commands are just objects with properties that help to define a formatting operation. For example, this is what the the bold command looks like:

{ node : 'b', override : 'bold' }

Now let's bind 4 of these commands to buttons at once. First the HTML...

<button class="action-{&bold&}">B</button> <button class="action-{&italic&}">I</button> <button class="action-{&underline&}">U</button> <button class="action-{&unformat&}">✘</button>

... then some JavaScript...

for (var <&command&> in aloha.ui.commands) { $('.action-' + <&command&>).on( 'click', aloha.ui.command(aloha.ui.commands[<&command&>]) ); }

... and done.

Now, try it in this editable...

Leap 4: Multiple Editables

But what if we want the underline button to be able to effect one editable and not the other? We can do this with a little more code. We will need to pass 2 arguments into aloha.ui.command instead of 1.

First let's add some suffixes to our button classnames. We add 1 to demark buttons that should effect editable 1 and 2 to demark buttons that should effect editable editable 2.

<button class="action-bold-{&1&}">B</button> <button class="action-underline-{&2&}">U</button>
var <&editables&> = aloha.dom.query('.editable', document).map(aloha); editables.forEach(editable) { for (var command in aloha.ui.commands) { $('.action-' + command + '-' + <&editable.id&>).on( 'click', <&aloha.ui.command&>(editable, aloha.ui.commands[command]) ); } })

Bare in mind that aloha.ui.command can also take an array of editables as its first argument to bind a single button to more than just one editables.

Third editable on page--automatically assigned id "3"...

Fourth editable on page--automatically assigned id "4"..

Leap 5: Update Button States

As well as formatting the selection inside of an editable, you will want your buttons to also reflect the formatting at the selection inside of an editable.

You can achieve this by adding a function (called "middleware") at the end of aloha.editor.stack array of functions.

All such middleware functions receive an event object that is threaded through aloha.editor.stack everytime there is an editing interaction.

function <&middleware&>(<&event&>) { $('.active').removeClass('active'); if ('leave' !== event.type) { var states = <&aloha.ui.states&>(<&aloha.ui.commands&>, <&event&>); for (var selector in states) { $('#.action-' + selector) .toggleClass('active', states[selector]); } } return event; } <&aloha.editor.stack&>.unshift(<&middleware&>);

Lots of formatting here—click around!

Leap 6: Custom Commands

You are not restricted to the default commands found in aloha.ui.commands. You can also use your own custom commands.

You do this by specifying your own command object that contains an action function, and any other properties you want. When the event to which this command is bound is fired, this command's action function will be called, and will receive an array of boundaries that represent the current boundary selection, the selection context object, and the the command itself. The last parameter is the event which trigger the command to be executed.

var <&command&> = { <&crazyCoolProperty&>: 'You selected: ', <&action&>: function (<&boundaries&>, <&selection&>, <&command&>, <&event&>) { console.log(<&command.crazyCoolProperty&> + aloha.markers.hint(<&boundaries&>)); } }; $('#crazy-cool-custom-command') .on('click', <&aloha.ui.command(command)&>);

Select some text here, and click that button below.

Now build something amazing!