Plugin object

The main plugin code is placed to a .js file which describes what and how must be done by the plugin. This file is placed to the plugin root folder together with the config.json and index.html files.

How it works

Any plugin has window.Asc.plugin object which in its turn has several methods for it to interact with ONLYOFFICE document, spreadsheet and presentation editors.

Please note that starting from version 7.1, the access to the window and document objects and the alert function is restricted from the plugin command code because the "use strict" mode was applied to the plugin scripts. Don't forget to declare variables before using them so that the plugins work correctly.

For the plugin to work the developer must specify two obligatory events for the window.Asc.plugin object: window.Asc.plugin.init and window.Asc.plugin.button. After that the window.Asc.plugin.callCommand method is used to send the data to the editors using the in-built ONLYOFFICE Document Builder API features.

If the plugin operates with an OLE object, window.Asc.plugin.callCommand method is used to manage it.

Let's see how this is done in the helloworld.js plugin:

(function (window, undefined) {
    window.Asc.plugin.init = function () {
        this.callCommand(function() {
            var oDocument = Api.GetDocument();
            var oParagraph = Api.CreateParagraph();
            oParagraph.AddText("Hello world!");
            oDocument.InsertContent([oParagraph]);
        }, true);
    };
    window.Asc.plugin.button = function (id) {
    };
})(window, undefined);

When the plugin object is being initialized (window.Asc.plugin.init = function () {...}), the editor forms a paragraph with the Hello World phrase and then uses ONLYOFFICE Document Builder API to create the document with this text in it (with the help of the window.Asc.plugin.callCommand method - this.callCommand(function() {...})).

Please note, that all ONLYOFFICE Document Builder features are already included into all the versions (including the open source one) of ONLYOFFICE document editors, this is how the plugins interact with the editors. The standalone ONLYOFFICE Document Builder version is required, however, in case you want to create documents without any document editors.

The only OK button (window.Asc.plugin.button = function (id) {...}) is used to create the text and finish the work with the plugin.

More existing open source plugin examples can be found here.