UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

55 lines (53 loc) 1.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withAnalytics = withAnalytics; /** * Callback accepts EditorState * * @remarks * The plugin using the Analytics may using an Optional<Analytics> dependency * It can be undefined during testing, we may not have included analytics * Also in cases where Analytics fails to load, the editor doesn't crash. * * @param state - EditorState, passed to the callback */ /** * Analytics API * @example * ``` * withAnalytics({ * eventType: EVENT_TYPE.TRACK, * action: ACTION.REPLACED_ONE, * actionSubject: ACTION_SUBJECT.TEXT, * attributes: { * triggerMethod, * }, * })(replace(replaceText)) * ``` * * @param analyticsApi - EditorAnalyticsAPI, undefined in tests or if analytics fails to load * @param payload - AnalyticsEventPayload | AnalyticsEventPayloadCallback - payload to be attached to the transaction * @param channel - string - channel to be used for analytics */ function withAnalytics(analyticsApi, payload, channel) { return function (command) { return function (state, dispatch, view) { return command(state, function (tr) { if (dispatch) { if (payload instanceof Function) { var dynamicPayload = payload(state); if (dynamicPayload) { analyticsApi === null || analyticsApi === void 0 || analyticsApi.attachAnalyticsEvent(dynamicPayload, channel)(tr); dispatch(tr); } } else { analyticsApi === null || analyticsApi === void 0 || analyticsApi.attachAnalyticsEvent(payload, channel)(tr); dispatch(tr); } } }, view); }; }; }