@atlaskit/editor-plugin-interactivity
Version:
Interactivity plugin for @atlaskit/editor-core
142 lines (140 loc) • 8.27 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.interactivityPlugin = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = require("react");
var _error = require("@atlaskit/editor-common/monitoring/error");
var _interactivityCollector = require("@atlaskit/inp-collector/interactivity-collector");
var _react2 = require("@atlaskit/inp-collector/react");
var _fireInteractivityEvent = require("./analytics/fire-interactivity-event");
var _excluded = ["regions", "sessionKeys"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
/**
* Reports session-to-date interaction latency distributions for full page editor sessions
* as the `editor interactivity` operational event.
*
* The session runs for as long as the plugin's hook stays mounted, which is one editor
* mount: `MountPluginHooks` in editor-core keys hook fibers by plugin name, so a preset
* reconfigure — which destroys and recreates ProseMirror plugin views — leaves this hook,
* and the session, in place.
*/
var interactivityPlugin = exports.interactivityPlugin = function interactivityPlugin(_ref) {
var api = _ref.api;
return {
name: 'interactivity',
usePluginHook: function usePluginHook(_ref2) {
var editorView = _ref2.editorView,
wrapperElement = _ref2.wrapperElement;
// The provider resolves asynchronously after mount, and `contextIdentifierPlugin` resets its
// state to the configured provider on transactions that do not carry a new one, so the id
// goes missing for a moment on a document that never changed.
var readObjectId = function readObjectId() {
var _api$contextIdentifie;
return api === null || api === void 0 || (_api$contextIdentifie = api.contextIdentifier) === null || _api$contextIdentifie === void 0 || (_api$contextIdentifie = _api$contextIdentifie.sharedState.currentState()) === null || _api$contextIdentifie === void 0 || (_api$contextIdentifie = _api$contextIdentifie.contextIdentifierProvider) === null || _api$contextIdentifie === void 0 ? void 0 : _api$contextIdentifie.objectId;
};
var readSessionMode = function readSessionMode() {
var _api$editorViewMode;
var mode = api === null || api === void 0 || (_api$editorViewMode = api.editorViewMode) === null || _api$editorViewMode === void 0 || (_api$editorViewMode = _api$editorViewMode.sharedState.currentState()) === null || _api$editorViewMode === void 0 ? void 0 : _api$editorViewMode.mode;
if (mode === undefined) {
return undefined;
}
return mode === 'view' ? 'reading' : 'editing';
};
// Created during render, so the region is there for the hook below on the first one. It
// touches nothing until `start`, so a render React throws away leaves nothing behind.
var collector = (0, _react.useMemo)(function () {
return new _interactivityCollector.InteractivityCollector({
regions: ['editor'],
endReasons: ['navigation', 'modeChange', 'unmount'],
// Confluence live pages navigate and switch between reading and editing without
// remounting the editor. A session covers one document in one mode, because
// interactions with a read-only page are a different population from interactions
// while editing.
sessionKeys: {
objectId: readObjectId(),
sessionMode: readSessionMode()
},
onError: function onError(error) {
return void (0, _error.logException)(error, {
location: 'editor-plugin-interactivity/collector'
});
},
emit: function emit(_ref3, _ref4) {
var _api$analytics;
var immediate = _ref4.immediate;
var regions = _ref3.regions,
sessionKeys = _ref3.sessionKeys,
snapshot = (0, _objectWithoutProperties2.default)(_ref3, _excluded);
return (0, _fireInteractivityEvent.fireInteractivityEvent)(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions, _objectSpread(_objectSpread({}, snapshot), {}, {
objectId: sessionKeys.objectId,
sessionMode: sessionKeys.sessionMode,
editor: regions.editor.all,
editorTyping: regions.editor.typing,
editorPointer: regions.editor.pointer,
editorOther: regions.editor.other,
// A destroyed view still answers, with the document it was destroyed with, so
// both report nothing rather than a size the editor no longer has.
nodeSize: editorView.isDestroyed ? undefined : editorView.state.doc.nodeSize,
// `getElementsByTagName` counts descendants in the browser engine, so this
// cannot overflow the stack on very large documents.
editorDomSize: editorView.isDestroyed ? undefined : editorView.dom.getElementsByTagName('*').length
}), {
immediate: immediate
});
}
});
},
// `api` is deliberately not a dependency: a preset reconfigure hands out a new proxy
// object, but the one captured here keeps resolving plugins from the same live registry,
// and a new collector on a reconfigure would split one editor session in two.
[editorView]);
(0, _react2.useInteractivityRegionElement)({
region: collector.region('editor'),
element: wrapperElement
});
(0, _react.useEffect)(function () {
try {
var _api$contextIdentifie2, _api$editorViewMode2;
if (!collector.start()) {
return;
}
var unsubscribeFromObjectId = api === null || api === void 0 || (_api$contextIdentifie2 = api.contextIdentifier) === null || _api$contextIdentifie2 === void 0 ? void 0 : _api$contextIdentifie2.sharedState.onChange(function () {
return collector.setSessionKeys({
values: {
objectId: readObjectId()
},
endReason: 'navigation'
});
});
var unsubscribeFromViewMode = api === null || api === void 0 || (_api$editorViewMode2 = api.editorViewMode) === null || _api$editorViewMode2 === void 0 ? void 0 : _api$editorViewMode2.sharedState.onChange(function () {
return collector.setSessionKeys({
values: {
sessionMode: readSessionMode()
},
endReason: 'modeChange'
});
});
return function () {
unsubscribeFromObjectId === null || unsubscribeFromObjectId === void 0 || unsubscribeFromObjectId();
unsubscribeFromViewMode === null || unsubscribeFromViewMode === void 0 || unsubscribeFromViewMode();
collector.stop({
endReason: 'unmount'
});
};
} catch (error) {
// Instrumentation must not fail the editor around it, but a failure that hits every
// browser of one engine would be invisible without this.
void (0, _error.logException)(error, {
location: 'editor-plugin-interactivity/start'
});
}
// `api` as above.
}, [collector]);
}
};
};