Bespin Embedded Guide

Introduction

Important Note

The current Bespin Embedded release is a preview release, and the API has not yet been finalized. Changes from release-to-release are still possible at this stage, and will be noted in the release notes for each release.

The Two Flavors of Bespin Embedded

Bespin is designed to scale up from simple text area replacement to a full-blown, powerful editing environment. This is accomplished through plugins. The Bespin Embedded package comes in two flavors:

With the Drop In flavor, you get a single .js and a single .css file that you can include on your server simply. You don't need anything else to use it.

With the Customizable flavor, you are able to tailor which plugins are installed for use with your Bespin.

The instructions on this page tell you how to deploy Bespin on your site, and apply regardless of which flavor of Bespin Embedded you're using. If you are using the Customizable flavor, you can take a look at the building instructions for information on how to change what is built into your Bespin.

Compressed vs. Uncompressed

The Drop In package comes with both compressed and uncompressed JavaScript and CSS files. For live site use, you will likely want to use the compressed JavaScript because it is much smaller than the uncompressed version. If you are trying to troubleshoot a problem, you should use the uncompressed version.

BespinEmbedded.js is uncompressed. BespinEmbedded.compressed.js is, unsurprisingly, compressed.

I suggest that you have your HTML refer to BespinEmbedded.js and just put the version of the file you need in place at that URL. The examples that follow all make the assumption that you're doing just that.

How to embed Bespin in your site

Level 1: Upgrade an element

The easiest thing to do to get Bespin working on your website is to simply include the Bespin script in your page, and mark the elements that you wish to use with Bespin with the class="bespin attribute. Download the Bespin Embedded release and put these two files on your web server:

To use Bespin on your page, you would then add lines like the following to the <head> element on your page:

<link href="/path/to/BespinEmbedded.css" rel="stylesheet" 
  type="text/css">
<script src="/path/to/BespinEmbedded.js"></script>

Then, elsewhere on your page, you can transform an element (such as a <div> or <textarea>) into a Bespin editor:

<div class="bespin">Initial contents</div>

There are a number of options to customize how Bespin loads. You can request Bespin to use these as follows:

<div class="bespin" data-bespinoptions='{ "stealFocus":true, "syntax": "js" }'>
</div>

data-bespinoptions uses a JSON structure (so make sure you follow the rules about escaping strings).

The element to be upgraded does not have to be a div, though there is a known issue that other element types such as textarea are not working right now.

The Bespin startup options are documented elsewhere.

Bespin does not allow multiple elements in a page to become Bespin editors - there can only be one.

Level 2: Manual Upgrade

Sometimes the element to upgrade might be dynamically created, or you might want to have Bespin as an option that is only loaded when the user selects a 'Use Bespin' option. In this case just inserting class="bespin after page load won't work, and you'll need to tell Bespin to use an element:

<script src="/path/to/BespinEmbedded.js"><script>
<script>
var embed = tiki.require("Embedded");
var node = document.getElementById("edit");
var bespin = embed.useBespin(node);
</script>

<textarea id="edit">Initial contents</textarea>

Rather than passing in a node, you can also simply pass in an string identifier as follows:

tiki.require("Embedded").useBespin("edit");

And as with level 1 above, you can also use options to customize the display:

tiki.require("Embedded").useBespin("edit", {
    stealFocus: true
});

Because this is JavaScript, the strict demands of JSON are not applicable here, where they are when using data-bespinoptions.

The Embedded API

It is possible to interact with a Bespin instance on a page, to alter contents for example.

When using manual upgrade of an element, the useBespin() function returns a bespin object which can be manipulated as follows:

var bespin = embed.useBespin("edit");
bespin.value = "Initial Content\nWith 2 lines";
bespin.setLineNumber(2);

When using element upgrading (with the class="bespin" attribute), you don't instantly have access to the Bespin Component. Fortunately, you can get access to it fairly easily:

<div id="edit" class="bespin">Initial contents</div>
<script>
var bespin = document.getElementById("edit").bespin;
bespin.value = "Hello, World!";
</script>

The DOM node that contains the editor gets a "bespin" property on it with the embedded editor convenience API.

Dimensions

Bespin always has to know the absolute position and size of the element it's contained in. The Bespin code will try to figure out the position of the element and keep it updated whenever the window size changes, so normally this all occurs behind the scenes and you don't need to worry about it. However, if you're altering elements in the DOM through JavaScript in ways that might cause the Bespin editor to move around, then you'll need to let Bespin know that its position might have changed. You can use the dimensionsChanged() function to do this. Pass in the editor object that you received from the useBespin() call whenever you programmatically update the absolute position of the editor on the page, whether directly (through altering the style of the Bespin element itself) or indirectly (through altering the style of some other element that triggers a reflow). For example, suppose we have this page:

<div id="box" style="display: inline-block; width: 100px;">Hello</div>
<div id="bespin"
    style="display: inline-block; width: 640px; height: 480px;">Bespin</div>

And this JavaScript:

bespin = embed.useBespin("bespin");

Notice that the position of the Bespin editor on the page is determined by the width of the box next to it, since both elements have inline-block layout. So, if the width of the adjacent box ever changes, dimensionsChanged() will need to be called. For example:

var box = document.getElementById('box');
box.style.width = '200px';                  // will cause Bespin to move
bespin.dimensionsChanged();                 // tell Bespin

Development Mode and onBespinLoad

When you use a simple <script> tag to include the Bespin components, you can be sure that Bespin is ready for action on page load. However, what happens if you are in development mode where the components are loaded asynchronously? Then, it's hard to know when Bespin is ready for action. Bespin fires an onBespinLoad event when it is ready for action.

For example:

<script>
window.onBespinLoad = function() {
    bespin = document.getElementById("editor").bespin;
};
<input type="button" value="Clear Bespin" 
    onclick="bespin.value='';">
</script>

If you are using Bespin via a normal script tag, then you don't need to use the onBespinLoad() function.