UNPKG

@atlaskit/editor-plugin-analytics

Version:

Analytics plugin for @atlaskit/editor-core

43 lines (41 loc) 1.37 kB
import { AnalyticsStep } from '@atlaskit/adf-schema/steps'; import { ACTION } from '@atlaskit/editor-common/analytics'; import { getStateContext } from './editor-state-context'; import { mapActionSubjectIdToAttributes } from './map-attributes'; const actionsToIgnore = [ACTION.INVOKED, ACTION.OPENED]; export const createAttachPayloadIntoTransaction = selection => ({ payload, tr, channel }) => attachPayloadIntoTransaction({ payload, selection, tr, channel }); // This utils was taken as reference in packages/editor/editor-plugin-ai/src/analytics/utils.ts // to create new util attachPayloadIntoTransaction in above file. // If you make a change here, please review attachPayloadIntoTransaction in above // file and update it as well if needed. const attachPayloadIntoTransaction = ({ payload, selection, tr, channel }) => { payload = getStateContext(selection, payload, tr); payload = mapActionSubjectIdToAttributes(payload); const { storedMarks } = tr; const pos = tr.mapping.map(selection.$from.pos, -1); tr.step(new AnalyticsStep([{ payload, channel }], actionsToIgnore, pos) // We need to create the step based on a position, this prevent split history for relative changes. ); // When you add a new step all the storedMarks are removed it if (storedMarks) { tr.setStoredMarks(storedMarks); } };