canmove, Confirmed users, Bureaucrats and Sysops emeriti
1,093
edits
No edit summary |
No edit summary |
||
Line 61: | Line 61: | ||
* Plugins should be loaded lazily to improve startup time. For example, a plugin that provides a syntax highlighter shouldn't be loaded until that highlighter is needed. | * Plugins should be loaded lazily to improve startup time. For example, a plugin that provides a syntax highlighter shouldn't be loaded until that highlighter is needed. | ||
== | == Plugin Definition and Loading == | ||
Here we will talk about the first goal, the | Here we will talk about the first goal, the ability for plugins to load and be activated. | ||
=== A one-file plugin === | === A one-file plugin === | ||
Line 107: | Line 107: | ||
// do something | // do something | ||
}) | }) | ||
} | |||
// The deactivate function is optional and provides a way | |||
// for the plugin to cleanup after itself. | |||
exports.deactivate = function() { | |||
// do something | |||
} | } | ||
Line 130: | Line 136: | ||
It will be possible to have a plugin load immediately, but the goal is to seek out ways to allow plugins to load more lazily. | It will be possible to have a plugin load immediately, but the goal is to seek out ways to allow plugins to load more lazily. | ||
=== | === Singletons === | ||
Plugins are singletons; there is only one instance of a plugin in memory at a time. | |||
=== Disabling Plugins === | |||
Adding ?disable=ALL to the editor URL will turn off all plugins. Adding ?disable=PluginName will disable a single plugin. This can be used as an escape hatch if a plugin is ill-behaved and renders Bespin inoperable. | |||
== Plugin API == | |||
Functions available within the plugin's loading scope: | |||
// require() loads a module. Modules are loaded only once, so calling | |||
// require a second time will simply return the same object. | |||
// var module = require("./modulename"); Example: | |||
var narcissus = require("narcissus"); | |||
var ast = narcissus.parse("1+1"); | |||
// resourceURL() computes URLs to resources relative to code modules. | |||
// If only one parameter is provided, the URL is relative to the plugin.json file. | |||
// Example: | |||
var url = resourceURL("bespin", "../../images/foo.png"); | |||
var url = resourceURL("images/foo.png"); // images directory at same level as plugin.json | |||
// subscribe() is like bespin.subscribe, except the plugin handler automatically | |||
// keeps track of subscriptions and will unsubscribe if the plugin needs to | |||
// be reloaded or deactivated. | |||
subscribe("plugin:activated", function(plugin) { | |||
// do something | |||
}); | |||
= | |||
=== Bespin Extension API === | |||
The sections above describe the basic framework for handling plugins. The API for extending Bespin will be developed over time, based entirely upon the development of real-world plugins. Documentation in this spot will grow along with that API. |