UNPKG

@atlaskit/editor-plugin-metrics

Version:

Metrics plugin for @atlaskit/editor-core

106 lines (105 loc) 4.25 kB
import { fg } from '@atlaskit/platform-feature-flags'; import { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure'; import { createPlugin, initialPluginState, metricsKey } from './pm-plugins/main'; import { getAnalyticsPayload } from './pm-plugins/utils/analytics'; /** * Metrics plugin to be added to an `EditorPresetBuilder` and used with `ComposableEditor` * from `@atlaskit/editor-core`. */ export const metricsPlugin = ({ config, api }) => ({ name: 'metrics', pmPlugins() { return [{ name: 'metrics', plugin: () => createPlugin(api, config === null || config === void 0 ? void 0 : config.userPreferencesProvider) }]; }, commands: { setContentMoved: () => ({ tr }) => { return tr.setMeta(metricsKey, { contentMoved: true }); }, startActiveSessionTimer: () => ({ tr }) => { const pluginState = api === null || api === void 0 ? void 0 : api.metrics.sharedState.currentState(); if (!(pluginState !== null && pluginState !== void 0 && pluginState.intentToStartEditTime)) { return tr; } return tr.setMeta(metricsKey, { shouldStartTimer: true, shouldPersistActiveSession: false }); }, stopActiveSession: () => ({ tr }) => { if (!api) { return tr; } const pluginState = api === null || api === void 0 ? void 0 : api.metrics.sharedState.currentState(); if (pluginState !== null && pluginState !== void 0 && pluginState.shouldPersistActiveSession) { return tr; } if (pluginState && pluginState.totalActionCount > 0 && pluginState.activeSessionTime > 0) { var _api$analytics; let toolbarDocking; if (expValEqualsNoExposure('platform_editor_controls', 'cohort', 'variant1')) { var _api$userPreferences, _api$userPreferences$, _api$userPreferences$2, _config$userPreferenc; toolbarDocking = toolbarDocking = fg('platform_editor_use_preferences_plugin') ? api === null || api === void 0 ? void 0 : (_api$userPreferences = api.userPreferences) === null || _api$userPreferences === void 0 ? void 0 : (_api$userPreferences$ = _api$userPreferences.sharedState.currentState()) === null || _api$userPreferences$ === void 0 ? void 0 : (_api$userPreferences$2 = _api$userPreferences$.preferences) === null || _api$userPreferences$2 === void 0 ? void 0 : _api$userPreferences$2.toolbarDockingPosition : config === null || config === void 0 ? void 0 : (_config$userPreferenc = config.userPreferencesProvider) === null || _config$userPreferenc === void 0 ? void 0 : _config$userPreferenc.getPreference('toolbarDockingInitialPosition'); } const payloadToSend = getAnalyticsPayload({ currentContent: tr.doc.content, pluginState, toolbarDocking: toolbarDocking || undefined }); api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions.attachAnalyticsEvent(payloadToSend)(tr); } tr.setMeta(metricsKey, { stopActiveSession: true }); tr.setMeta('scrollIntoView', false); tr.setMeta('addToHistory', false); return tr; }, handleIntentToStartEdit: ({ newSelection, shouldStartTimer = true, shouldPersistActiveSession }) => ({ tr }) => { if (!api) { return tr; } const pluginState = api === null || api === void 0 ? void 0 : api.metrics.sharedState.currentState(); if (shouldPersistActiveSession && pluginState !== null && pluginState !== void 0 && pluginState.intentToStartEditTime) { return tr.setMeta(metricsKey, { shouldPersistActiveSession }); } if (!pluginState || pluginState.intentToStartEditTime) { return tr; } tr.setMeta(metricsKey, { intentToStartEditTime: performance.now(), shouldStartTimer, newSelection, shouldPersistActiveSession }); return tr; } }, getSharedState(editorState) { if (!editorState) { return initialPluginState; } return metricsKey.getState(editorState); } });