How to attach events

There are two options to attach events from the editor.

Option 1. Using the attachEvent method (works for all versions)

  1. In the config.json file, add the events parameter with the array of all the available events:

    {
      "events": ["onAddComment"]
    }
    
  2. In the plugin code, define the attachEvent method to add an event listener, a function that will be called whenever the specified event is delivered to the target:

    Asc.plugin.attachEvent(id, action)
    

    Parameters:

    NameTypeDescription
    idstringThe event name.
    actionfunctionThe event listener.

    Returns:

    This method doesn't return any data.

    Example:

    Asc.plugin.attachEvent("onAddComment", (data) => {
      console.log(data)
    })
    

    You can also use the event_{event-name} method, where event_ is a prefix used for each event method, and {event-name} is a name of any event. For example:

    Asc.plugin.event_onAddComment = (data) => {
      console.log(data)
    }
    

    Such methods work the same way as the attachEvent method.

Option 2. Using the attachEditorEvent method (works starting from version 8.2)

In the plugin code, define the attachEditorEvent method to add an event listener, a function that will be called whenever the specified event is delivered to the target:

Parameters:

NameTypeDescription
idstringThe event name.
actionfunctionThe event listener.

Returns:

This method doesn't return any data.

Example:

Asc.plugin.attachEditorEvent("onAddComment", (data) => {
  console.log(data)
})

Get Help

  • If you have any questions about ONLYOFFICE Docs, try the FAQ section first.
  • You can request a feature or report a bug by posting an issue on GitHub.
  • You can also ask our developers on ONLYOFFICE forum (registration required).