UNPKG

@atlaskit/editor-plugin-collab-edit

Version:

Collab Edit plugin for @atlaskit/editor-core

85 lines (82 loc) 4.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sendTransaction = void 0; var _steps = require("@atlaskit/adf-schema/steps"); var _experiments = require("@atlaskit/tmp-editor-statsig/experiments"); var _actions = require("../actions"); var _pluginKey = require("../main/plugin-key"); var sendTransaction = exports.sendTransaction = function sendTransaction(_ref) { var originalTransaction = _ref.originalTransaction, transactions = _ref.transactions, oldEditorState = _ref.oldEditorState, newEditorState = _ref.newEditorState, useNativePlugin = _ref.useNativePlugin, viewMode = _ref.viewMode, hideTelecursorOnLoad = _ref.hideTelecursorOnLoad; return function (provider) { var docChangedTransaction = transactions.find(function (tr) { return tr.docChanged; }); var currentPluginState = _pluginKey.pluginKey.getState(newEditorState); var trNoAnalytics = oldEditorState.tr; docChangedTransaction === null || docChangedTransaction === void 0 || docChangedTransaction.steps.forEach(function (step) { if (!(step instanceof _steps.AnalyticsStep)) { trNoAnalytics.step(step); } }); if (!(currentPluginState !== null && currentPluginState !== void 0 && currentPluginState.isReady)) { return; } var newTransaction = (0, _experiments.editorExperiment)('platform_editor_reduce_noisy_steps_ncs', true, { exposure: true }) ? trNoAnalytics : docChangedTransaction; var shouldSendStepForSynchronyCollabProvider = !originalTransaction.getMeta('isRemote') && // TODO: ED-8995 - We need to do this check to reduce the number of race conditions when working with tables. // This metadata is coming from the scaleTable command in table-resizing plugin !originalTransaction.getMeta('scaleTable') && ((0, _experiments.editorExperiment)('platform_editor_reduce_noisy_steps_ncs', true) ? newTransaction === null || newTransaction === void 0 ? void 0 : newTransaction.docChanged : true); if (useNativePlugin || shouldSendStepForSynchronyCollabProvider) { provider.send(newTransaction, oldEditorState, newEditorState); } var prevPluginState = _pluginKey.pluginKey.getState(oldEditorState); var _ref2 = prevPluginState || {}, prevActiveParticipants = _ref2.activeParticipants; var activeParticipants = currentPluginState.activeParticipants, sessionId = currentPluginState.sessionId; var selectionChanged = !oldEditorState.selection.eq(newEditorState.selection); var participantsChanged = prevActiveParticipants && !prevActiveParticipants.eq(activeParticipants); if (!sessionId || viewMode !== 'edit') { return; } // uiEvent is standard metdata (docs: https://prosemirror.net/docs/ref/#state.Transaction) var isPaste = (docChangedTransaction === null || docChangedTransaction === void 0 ? void 0 : docChangedTransaction.getMeta('uiEvent')) === 'paste'; // If this metadata is truthy then it means a selection bookmark might be declared as the meta value OR the transaction // doesn't want the tr.selection to be sent to remote users at all. var remoteSelectionBookmark = originalTransaction.getMeta('useSelectionBookmarkForRemote'); if (!!remoteSelectionBookmark) { if (remoteSelectionBookmark !== true && 'resolve' in remoteSelectionBookmark) { var selection = remoteSelectionBookmark.resolve(newEditorState.doc); var message = { type: 'telepointer', selection: (0, _actions.getSendableSelection)(selection), sessionId: sessionId }; provider.sendMessage(message); } } else if ( // Broadcast the position if the selection has changed, and the doc hasn't changed (it is mapped // by the receiver). // If we're pasting content though make an exception (as doc has changed) // as on a ranged selection it results in not clearing the ranged selection after the paste selectionChanged && (!docChangedTransaction || isPaste) || participantsChanged && !hideTelecursorOnLoad) { var _selection = (0, _actions.getSendableSelection)(newEditorState.selection); var _message = { type: 'telepointer', selection: _selection, sessionId: sessionId }; provider.sendMessage(_message); } }; };