@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
45 lines (44 loc) • 1.49 kB
JavaScript
/**
* 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
*/
export function withAnalytics(analyticsApi, payload, channel) {
return command => (state, dispatch, view) => command(state, tr => {
if (dispatch) {
if (payload instanceof Function) {
const dynamicPayload = payload(state);
if (dynamicPayload) {
analyticsApi === null || analyticsApi === void 0 ? void 0 : analyticsApi.attachAnalyticsEvent(dynamicPayload, channel)(tr);
dispatch(tr);
}
} else {
analyticsApi === null || analyticsApi === void 0 ? void 0 : analyticsApi.attachAnalyticsEvent(payload, channel)(tr);
dispatch(tr);
}
}
}, view);
}