UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

74 lines 2.85 kB
/** * A plugin is created for collecting payload data for tableOverflowChanged analytics event */ import { ACTION_SUBJECT, EVENT_TYPE, TABLE_ACTION, TABLE_OVERFLOW_CHANGE_TRIGGER } from '@atlaskit/editor-common/analytics'; import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { PluginKey } from '@atlaskit/editor-prosemirror/state'; const pluginKey = new PluginKey('tableOverflowAnalyticsPlugin'); export const META_KEYS = { OVERFLOW_TRIGGER: 'tableOverflowTrigger', OVERFLOW_STATE_CHANGED: 'tableOverflowStateChanged' }; const createPlugin = (dispatch, dispatchAnalyticsEvent, tableResizingEnabled) => new SafePlugin({ key: pluginKey, state: { init() { return { lastTrigger: undefined }; }, apply(tr, pluginState) { const meta = tr.getMeta(META_KEYS.OVERFLOW_TRIGGER); const newState = { ...pluginState }; if (meta) { newState.lastTrigger = { ...meta }; dispatch(pluginKey, newState); return newState; } return pluginState; } }, appendTransaction: (transactions, oldState, newState) => { var _newPluginState$lastT; const newPluginState = pluginKey.getState(newState); let hasAnalyticsBeenDispatched = false; const lastTriggerName = (newPluginState === null || newPluginState === void 0 ? void 0 : (_newPluginState$lastT = newPluginState.lastTrigger) === null || _newPluginState$lastT === void 0 ? void 0 : _newPluginState$lastT.name) || // NOTE: We assume that we know and can correctly differentiate // between all triggers of table overflow state change. // The only trigger we can't identify is viewport width change. // However, since there is still a chance that there are other triggers we didn't think of, // all these unknown triggers and viwport width change trigger are captured as EXTERNAL. TABLE_OVERFLOW_CHANGE_TRIGGER.EXTERNAL; transactions.forEach(tr => { const payload = tr.getMeta(META_KEYS.OVERFLOW_STATE_CHANGED); if (payload) { dispatchAnalyticsEvent({ action: TABLE_ACTION.OVERFLOW_CHANGED, actionSubject: ACTION_SUBJECT.TABLE, actionSubjectId: null, eventType: EVENT_TYPE.TRACK, attributes: { editorWidth: payload.editorWidth, parentWidth: payload.parentWidth, isOverflowing: payload.isOverflowing, wasOverflowing: payload.wasOverflowing, width: payload.width, tableResizingEnabled, trigger: lastTriggerName } }); hasAnalyticsBeenDispatched = true; } }); if (hasAnalyticsBeenDispatched) { const tr = newState.tr; return tr.setMeta(META_KEYS.OVERFLOW_TRIGGER, {}); } return undefined; } }); export { createPlugin };