All files / roosterjs-content-model-core/lib/coreApi/triggerEvent triggerEvent.ts

100% Statements 12/12
100% Branches 16/16
100% Functions 4/4
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44              1x                           1x 395x     35x   393x 3847x 2679x             35x 1x 1x     34x    
import type {
    EditorPlugin,
    PluginEvent,
    PluginEventType,
    TriggerEvent,
} from 'roosterjs-content-model-types';
 
const allowedEventsInShadowEdit: PluginEventType[] = [
    'editorReady',
    'beforeDispose',
    'extractContentWithDom',
    'zoomChanged',
];
 
/**
 * @internal
 * Trigger a plugin event
 * @param core The EditorCore object
 * @param pluginEvent The event object to trigger
 * @param broadcast Set to true to skip the shouldHandleEventExclusively check
 */
export const triggerEvent: TriggerEvent = (core, pluginEvent, broadcast) => {
    if (
        (!core.lifecycle.shadowEditFragment ||
            allowedEventsInShadowEdit.indexOf(pluginEvent.eventType as PluginEventType) >= 0) &&
        (broadcast || !core.plugins.some(plugin => handledExclusively(pluginEvent, plugin)))
    ) {
        core.plugins.forEach(plugin => {
            if (plugin.onPluginEvent) {
                plugin.onPluginEvent(pluginEvent);
            }
        });
    }
};
 
function handledExclusively(event: PluginEvent, plugin: EditorPlugin): boolean {
    if (plugin.onPluginEvent && plugin.willHandleEventExclusively?.(event)) {
        plugin.onPluginEvent(event);
        return true;
    }
 
    return false;
}