@atlaskit/editor-plugin-collab-edit
Version:
Collab Edit plugin for @atlaskit/editor-core
46 lines (45 loc) • 2.67 kB
JavaScript
import { isOfflineMode } from '@atlaskit/editor-plugin-connectivity';
import { Transaction } from '@atlaskit/editor-prosemirror/state';
import { Rebaseable } from '@atlaskit/prosemirror-collab';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
const isLocked = step => {
if (step.origin instanceof Transaction) {
return step.origin.getMeta('mergeIsLocked');
}
return false;
};
/**
* Merge a set of steps together to reduce the total number of steps stored in memory.
*
* All steps passing through here should be "lockable" (ie. can be locked by the document service)
* so that it can be indicated they have already been sent (or are about to be sent) to the backend
* and cannot be merged.
*
* @param steps Rebaseable steps
* @returns Rebaseable steps
*/
export function mergeUnconfirmedSteps(steps, api) {
const mergedSteps = steps.reduce((acc, rebaseable) => {
var _api$connectivity, _api$connectivity$sha;
const lastStep = acc[acc.length - 1];
const isOffline = isOfflineMode(api === null || api === void 0 ? void 0 : (_api$connectivity = api.connectivity) === null || _api$connectivity === void 0 ? void 0 : (_api$connectivity$sha = _api$connectivity.sharedState.currentState()) === null || _api$connectivity$sha === void 0 ? void 0 : _api$connectivity$sha.mode);
const isOnlineMergeEnabled = expValEquals('platform_editor_enable_single_player_step_merging', 'isEnabled', true);
const isMergingEnabled = isOffline || isOnlineMergeEnabled;
if (isMergingEnabled && lastStep && !isLocked(lastStep) && !isLocked(rebaseable)) {
const mergedStep = lastStep.step.merge(rebaseable.step);
const inverted = rebaseable.inverted.merge(lastStep.inverted);
// Always take the origin of the new step.
// We use this in packages/editor/collab-provider/src/document/document-service.ts:commitUnconfirmedSteps
// to confirm that the last transaction has been sent. Since we're taking the latest this still works as expected
// As we want to wait until all the transactions have been pushed through
const origin = lastStep.origin;
if (mergedStep && inverted) {
acc[acc.length - 1] = new Rebaseable(mergedStep, inverted, origin instanceof Transaction ? origin.setMeta('isOffline', origin.getMeta('isOffline') === true || isOffline) : origin);
return acc;
}
}
acc.push(new Rebaseable(rebaseable.step, rebaseable.inverted, rebaseable.origin instanceof Transaction ? rebaseable.origin.setMeta('isOffline', rebaseable.origin.getMeta('isOffline') === true || isOffline) : rebaseable.origin));
return acc;
}, []);
return mergedSteps;
}