@atlaskit/editor-plugin-interactivity
Version:
Interactivity plugin for @atlaskit/editor-core
125 lines (124 loc) • 6.38 kB
JavaScript
import { useEffect, useMemo } from 'react';
import { logException } from '@atlaskit/editor-common/monitoring/error';
import { InteractivityCollector } from '@atlaskit/inp-collector/interactivity-collector';
import { useInteractivityRegionElement } from '@atlaskit/inp-collector/react';
import { fireInteractivityEvent } from './analytics/fire-interactivity-event';
/**
* 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.
*/
export const interactivityPlugin = ({
api
}) => ({
name: 'interactivity',
usePluginHook({
editorView,
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.
const readObjectId = () => {
var _api$contextIdentifie, _api$contextIdentifie2, _api$contextIdentifie3;
return api === null || api === void 0 ? void 0 : (_api$contextIdentifie = api.contextIdentifier) === null || _api$contextIdentifie === void 0 ? void 0 : (_api$contextIdentifie2 = _api$contextIdentifie.sharedState.currentState()) === null || _api$contextIdentifie2 === void 0 ? void 0 : (_api$contextIdentifie3 = _api$contextIdentifie2.contextIdentifierProvider) === null || _api$contextIdentifie3 === void 0 ? void 0 : _api$contextIdentifie3.objectId;
};
const readSessionMode = () => {
var _api$editorViewMode, _api$editorViewMode$s;
const mode = api === null || api === void 0 ? void 0 : (_api$editorViewMode = api.editorViewMode) === null || _api$editorViewMode === void 0 ? void 0 : (_api$editorViewMode$s = _api$editorViewMode.sharedState.currentState()) === null || _api$editorViewMode$s === void 0 ? void 0 : _api$editorViewMode$s.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.
const collector = useMemo(() => new 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: error => void logException(error, {
location: 'editor-plugin-interactivity/collector'
}),
emit: ({
regions,
sessionKeys,
...snapshot
}, {
immediate
}) => {
var _api$analytics;
return fireInteractivityEvent(api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions, {
...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
});
}
}),
// `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]);
useInteractivityRegionElement({
region: collector.region('editor'),
element: wrapperElement
});
useEffect(() => {
try {
var _api$contextIdentifie4, _api$editorViewMode2;
if (!collector.start()) {
return;
}
const unsubscribeFromObjectId = api === null || api === void 0 ? void 0 : (_api$contextIdentifie4 = api.contextIdentifier) === null || _api$contextIdentifie4 === void 0 ? void 0 : _api$contextIdentifie4.sharedState.onChange(() => collector.setSessionKeys({
values: {
objectId: readObjectId()
},
endReason: 'navigation'
}));
const unsubscribeFromViewMode = api === null || api === void 0 ? void 0 : (_api$editorViewMode2 = api.editorViewMode) === null || _api$editorViewMode2 === void 0 ? void 0 : _api$editorViewMode2.sharedState.onChange(() => collector.setSessionKeys({
values: {
sessionMode: readSessionMode()
},
endReason: 'modeChange'
}));
return () => {
unsubscribeFromObjectId === null || unsubscribeFromObjectId === void 0 ? void 0 : unsubscribeFromObjectId();
unsubscribeFromViewMode === null || unsubscribeFromViewMode === void 0 ? 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 logException(error, {
location: 'editor-plugin-interactivity/start'
});
}
// `api` as above.
}, [collector]);
}
});