Skip to content

Trigger events from plugin

Jiuqing Song edited this page Mar 9, 2018 · 1 revision

Go back to Create your own plugin

We have 4 types of events now: PluginDomEvent, ContentChangedEvent, ExtractContentEvent, BeforePasteEvent.

PluginDomEvent should only be triggered by user actions from Editor class, while all 3 other types of events can be triggered from plugin by Editor.triggerEvent() method.

Editor.triggerEvent() method

public triggerEvent(pluginEvent: PluginEvent, broadcast: boolean = true);

This is the method to trigger any type of event. People who invoke this method need to take the responsibility to make sure the event object matches its event type. Please reference to Plugin events section about the mapping of plugin event type and event object type.

pluginEvent parameter is the event object to trigger. Other plugins will receive this event object in EditorPlugin.onPluginEvent() method.

broadcast is a parameter to specify if we want to broadcast this event to all plugins. When set to true (default value), all plugins are able to handle this event and no one can do exclusive handling. If set to false, editor will obey the rule that only plugin who returns true from EditorPlugin.willHandleEventExclusively() method can really handle this event.

For example, Paste plugin uses this method to trigger a BeforePaste event when the clipboard data is ready to be pasted into editor content, so that other plugins can handle this event and change paste content if they want:

let event: BeforePasteEvent = {
    eventType: PluginEventType.BeforePaste,
    clipboardData: clipboardData,
    fragment: fragment,
    pasteOption: pasteOption,
};

this.editor.triggerEvent(event, true /*broadcast*/);

Editor.triggerContentChangedEvent() method

If you need to trigger ContentChangedEvent, there is a shortcut method:

public triggerContentChangedEvent(
    source: ChangeSource | string = ChangeSource.SetContent,
    data?: any
);

source parameter is the change source string, you can use ChangeSource enum for the predefined change sources or literal string for customized change source. Default value is ChangeSource.SetContent. Other plugins can check this source string to know where is this event come from.

data is an optional object to set related data for this event. Please reference to ContentChangedEvent page to see what kind of related data are used for predefined change source.

For example, Paste plugin uses this method to trigger ContentChanged event with source 'Paste' and related clipboardData:

this.editor.triggerContentChangedEvent(ChangeSource.Paste, clipboardData);