UNPKG

@atlaskit/editor-common

Version:

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

54 lines (52 loc) 1.73 kB
/** * Meta information that can be passed to the analytics payload callback * * @param currentDoc - Current document node after the transaction */ /** * 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 * @param meta - Meta, 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, { currentDoc: tr.doc }); 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); }