superdesk/superdesk-client-core

View on GitHub
scripts/core/internal-events.ts

Summary

Maintainability
B
4 hrs
Test Coverage
import {IArticle} from 'superdesk-api';
import {IPanelAction} from './interactive-article-actions-panel/interfaces';

interface IInternalEvents {
    addImage: {
        field: string;
        image: IArticle;
    };
    saveArticleInEditMode: void;
    dangerouslyOverwriteAuthoringData: Partial<IArticle>;
    replaceAuthoringDataWithChanges: Partial<unknown>;

    /**
     * Unsaved changes will be lost.
     * Only applies to authoring-react.
     */
    dangerouslyForceReloadAuthoring: void;

    changeUserPreferences: {[preferenceId: string]: any};
    openExportView: Array<IArticle['_id']>;
    extensionsHaveLoaded: true;

    interactiveArticleActionStart: IPanelAction;
    interactiveArticleActionEnd: void;
}

export function addInternalEventListener<T extends keyof IInternalEvents>(
    eventName: T,
    handler: (event: CustomEvent<IInternalEvents[T]>) => void,
) {
    window.addEventListener(eventName, handler);

    return () => {
        window.removeEventListener(eventName, handler);
    };
}

export function dispatchInternalEvent<T extends keyof IInternalEvents>(eventName: T, payload: IInternalEvents[T]) {
    window.dispatchEvent(new CustomEvent(eventName, {detail: payload}));
}