@atlaskit/editor-plugin-metrics
Version:
Metrics plugin for @atlaskit/editor-core
113 lines (112 loc) • 5.16 kB
JavaScript
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 var metricsPlugin = function metricsPlugin(_ref) {
var config = _ref.config,
api = _ref.api;
return {
name: 'metrics',
pmPlugins: function pmPlugins() {
return [{
name: 'metrics',
plugin: function plugin() {
return createPlugin(api, config === null || config === void 0 ? void 0 : config.userPreferencesProvider);
}
}];
},
commands: {
setContentMoved: function setContentMoved() {
return function (_ref2) {
var tr = _ref2.tr;
return tr.setMeta(metricsKey, {
contentMoved: true
});
};
},
startActiveSessionTimer: function startActiveSessionTimer() {
return function (_ref3) {
var tr = _ref3.tr;
var 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: function stopActiveSession() {
return function (_ref4) {
var tr = _ref4.tr;
if (!api) {
return tr;
}
var 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;
var toolbarDocking;
if (expValEqualsNoExposure('platform_editor_controls', 'cohort', 'variant1')) {
var _api$userPreferences, _config$userPreferenc;
toolbarDocking = toolbarDocking = fg('platform_editor_use_preferences_plugin') ? api === null || api === void 0 || (_api$userPreferences = api.userPreferences) === null || _api$userPreferences === void 0 || (_api$userPreferences = _api$userPreferences.sharedState.currentState()) === null || _api$userPreferences === void 0 || (_api$userPreferences = _api$userPreferences.preferences) === null || _api$userPreferences === void 0 ? void 0 : _api$userPreferences.toolbarDockingPosition : config === null || config === void 0 || (_config$userPreferenc = config.userPreferencesProvider) === null || _config$userPreferenc === void 0 ? void 0 : _config$userPreferenc.getPreference('toolbarDockingInitialPosition');
}
var payloadToSend = getAnalyticsPayload({
currentContent: tr.doc.content,
pluginState: pluginState,
toolbarDocking: toolbarDocking || undefined
});
api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 || _api$analytics.actions.attachAnalyticsEvent(payloadToSend)(tr);
}
tr.setMeta(metricsKey, {
stopActiveSession: true
});
tr.setMeta('scrollIntoView', false);
tr.setMeta('addToHistory', false);
return tr;
};
},
handleIntentToStartEdit: function handleIntentToStartEdit(_ref5) {
var newSelection = _ref5.newSelection,
_ref5$shouldStartTime = _ref5.shouldStartTimer,
shouldStartTimer = _ref5$shouldStartTime === void 0 ? true : _ref5$shouldStartTime,
shouldPersistActiveSession = _ref5.shouldPersistActiveSession;
return function (_ref6) {
var tr = _ref6.tr;
if (!api) {
return tr;
}
var 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: shouldPersistActiveSession
});
}
if (!pluginState || pluginState.intentToStartEditTime) {
return tr;
}
tr.setMeta(metricsKey, {
intentToStartEditTime: performance.now(),
shouldStartTimer: shouldStartTimer,
newSelection: newSelection,
shouldPersistActiveSession: shouldPersistActiveSession
});
return tr;
};
}
},
getSharedState: function getSharedState(editorState) {
if (!editorState) {
return initialPluginState;
}
return metricsKey.getState(editorState);
}
};
};