UNPKG

@atlaskit/editor-plugin-collab-edit

Version:

Collab Edit plugin for @atlaskit/editor-core

64 lines 3.1 kB
import { isDirtyTransaction } from '@atlaskit/editor-common/collab'; import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { PluginKey } from '@atlaskit/editor-prosemirror/state'; import { AddMarkStep } from '@atlaskit/editor-prosemirror/transform'; import { originalTransactionHasMeta } from './utils'; export const trackNCSInitializationPluginKey = new PluginKey('collabTrackNCSInitializationPlugin'); export const createPlugin = () => { return new SafePlugin({ key: trackNCSInitializationPluginKey, state: { init() { return { collabInitialisedAt: null, firstChangeAfterInitAt: null, firstContentBodyChangeAfterInitAt: null }; }, apply(transaction, prevPluginState, oldState) { if (Boolean(transaction.getMeta('collabInitialised'))) { return { collabInitialisedAt: Date.now(), firstChangeAfterInitAt: null, firstContentBodyChangeAfterInitAt: null }; } const shouldCheckDocument = prevPluginState.collabInitialisedAt && !prevPluginState.firstContentBodyChangeAfterInitAt; if (!shouldCheckDocument) { return prevPluginState; } const isRemote = originalTransactionHasMeta(transaction, 'isRemote'); const isDocumentReplaceFromRemote = isRemote && originalTransactionHasMeta(transaction, 'replaceDocument'); if (isDocumentReplaceFromRemote) { return prevPluginState; } if (isDirtyTransaction(transaction)) { return prevPluginState; } if (transaction.docChanged && !transaction.doc.eq(oldState.doc)) { // For analytics purposes, inline comment annotations are not considered as edits to the document body // Transaction may contain other steps, but we know that they won't be user-generated (non synthetic) steps // Additionally, for analytics purposes we do not want to trigger on other participants' changes (i.e. remote changes) const isAnnotationStep = !!transaction.steps.find(step => { var _step$mark, _step$mark$type; return step instanceof AddMarkStep && ((_step$mark = step.mark) === null || _step$mark === void 0 ? void 0 : (_step$mark$type = _step$mark.type) === null || _step$mark$type === void 0 ? void 0 : _step$mark$type.name) === 'annotation'; }); return { collabInitialisedAt: prevPluginState.collabInitialisedAt, firstChangeAfterInitAt: Date.now(), firstContentBodyChangeAfterInitAt: isAnnotationStep || isRemote ? prevPluginState.firstContentBodyChangeAfterInitAt : Date.now() }; } return prevPluginState; } }, props: { attributes(editorState) { const trackPluginState = trackNCSInitializationPluginKey.getState(editorState); return { ['data-has-collab-initialised']: `${Boolean(trackPluginState === null || trackPluginState === void 0 ? void 0 : trackPluginState.collabInitialisedAt)}` }; } } }); };