This section explains how to write a plugin for Orion. It is intended for developers who want to extend Orion's functionality.

What is a plugin?

A plugin is an HTML file containing some JavaScript that knows how to connect to the Orion client. Plugins can be hosted on any web server and installed into Orion using their URL.

In order to be useful, a plugin should provide one or more services. When Orion needs a service contributed by a plugin, it loads the plugin inside an IFrame.

Orion currently supports a small set of extension points: service types that plugins can contribute to, in order to customize the client and add more functionality. These include things like:

For a full list of available services, see the Developer Guide.

What you need

Every plugin must include the following JavaScript library:

You can copy-paste its contents into a <script> tag in your plugin, or load it externally like so:

<script src="plugin.js"></script>

Writing the plugin

Let's make a plugin that adds a button to the toolbar of the Orion editor. When clicked, it will reverse the selected text in the editor. This is not a very useful feature, but it'll be a good introduction to the concepts involved.

Creating the HTML file

Create a new HTML file called reversePlugin.html with the following content:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <title>Reverse Plugin</title>
</head>
<body></body>
</html>

What we have now isn't a plugin yet. It's just a bare-bones HTML file. The next step is to include the API we'll need to talk to Orion. Grab the plugin.js file (see What you need) and put it in the same folder as reversePlugin.html. Then add this inside the <head> tags of the HTML file:

<script src="plugin.js"></script>

Making it a plugin

Next, we'll add some code that connects our file to Orion. Add the following, again inside the <head> tags:

<script>
  window.onload = function() {
    var provider = new eclipse.PluginProvider();
    provider.connect();
  };
</script>

At this point, we've got an honest-to-goodness Orion plugin, albeit one that does nothing. Let's go over the various parts in detail.

Registering the service

Now we're going to expose a service to Orion. We'll create and register a service with the orion.edit.command service type. Add the additional lines as shown:

window.onload = function() {
  var provider = new eclipse.PluginProvider();
  var serviceImpl = {  };
  var serviceProperties = {  };
  provider.registerServiceProvider("orion.edit.command", serviceImpl, serviceProperties);
  provider.connect();
};

Let's go over what we have now:

Implementing the service

We'll fill in the serviceImpl and serviceProperties objects with the actual details of the service. Change the serviceImpl object to look like this:

var serviceImpl = {
  run: function(text) {
    return text.split("").reverse().join("");
  }
};

Note that the functions defined in the service will depend on what service type you're contributing to. In our case, we're contributing to "orion.edit.command", which expects a run() function. (See the Developer Guide for a list of extension points and their API.)

Change the serviceProperties object to look like this:

var serviceProperties = { 
  name: "Reverse Text",
  key: ["e", true, true] // Ctrl+Shift+e
};

The finished plugin file

Make sure that your copy of reversePlugin.html looks like this:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <title>Reverse Plugin</title>
  <script src="plugin.js"></script>
  <script>
  window.onload = function() {
      var provider = new eclipse.PluginProvider();
      var serviceImpl = {
              run: function(text) {
                  return text.split("").reverse().join("");
              }
          };
      var serviceProperties = { 
              name: "Reverse Text",
              key: ["e", true, true] // Ctrl+Shift+e
          };
      provider.registerServiceProvider("orion.edit.command", serviceImpl, serviceProperties);
      provider.connect();
  };
</script>
</head>
<body></body>
</html>

Testing the plugin

First we need to host our plugin somewhere.

Now that you've got a URL for reversePlugin.html, install it into Orion:

Let's try it out.

Examples

Here are some existing plugins we've written. View their source code to see how they work:

http://orionhub.org/plugins/sampleCommandsPlugin.html
Contributes several sample actions to the Orion navigator by using the orion.navigate.command service type.
http://orionhub.org/plugins/webEditingPlugin.html
Contributes syntax highlighting support for HTML files by using the orion.edit.highlighter service type.
http://mamacdon.github.com/0.2/plugins/beautify/jsbeautify.html
Contributes a "Beautify JS" button to the editor toolbar by using the orion.edit.command service type. Visit http://mamacdon.github.com for a directory of other available plugins, organized by Orion version.
http://mamacdon.github.com/0.2/plugins/uglify/uglify-plugin.html
Contributes an "Uglify JS" (minify, in other words) button to the editor toolbar by using the orion.edit.command service type.

See also