UNPKG

@atlaskit/editor-plugin-show-diff

Version:

ShowDiff plugin for @atlaskit/editor-core

122 lines (118 loc) • 5.06 kB
import { AnalyticsStep } from '@atlaskit/adf-schema/steps/analytics'; import { areNodesEqualIgnoreAttrs } from '@atlaskit/editor-common/utils/document'; import { ReplaceStep } from '@atlaskit/editor-prosemirror/transform'; import { getAttributionKey } from '../decorations/colorSchemes/attributions'; /** * Attempts to merge two consecutive ReplaceStep operations. * This merges steps where: * - Both steps replace content at the same starting position * - The second step's range encompasses the first step's range * - The second step has content to insert * Returns null if steps cannot be merged. */ function mergeReplaceSteps(step1, step2) { if (!(step1 instanceof ReplaceStep) || !(step2 instanceof ReplaceStep)) { return null; } // Confirm the step overlaps exactly with the previous const step2Length = step2.to - step2.from; if (step1.from === step2.from && step2.to >= step1.to && step2.slice.size >= step2Length && step2Length === step1.slice.size && step2.slice.size >= step1.slice.size && step1.slice.openEnd === step2.slice.openEnd && step1.slice.openStart === step2.slice.openStart) { return new ReplaceStep(step1.from, step1.to, step2.slice); } return null; } // Simplifies the steps to improve performance and reduce fragmentation in diffs export function simplifySteps(steps, originalDoc) { const stepsToFilter = removeUnusedSteps(steps, originalDoc); return stepsToFilter // Remove steps that don't affect document structure or content .filter(step => !(step instanceof AnalyticsStep)) // Merge consecutive steps where possible .reduce((acc, step) => { var _lastStep$merge, _lastStep$merge2; const lastStep = acc[acc.length - 1]; const merged = (_lastStep$merge = lastStep === null || lastStep === void 0 ? void 0 : (_lastStep$merge2 = lastStep.merge) === null || _lastStep$merge2 === void 0 ? void 0 : _lastStep$merge2.call(lastStep, step)) !== null && _lastStep$merge !== void 0 ? _lastStep$merge : mergeReplaceSteps(lastStep, step); if (merged) { acc[acc.length - 1] = merged; } else { acc.push(step); } return acc; }, []); } const mergeAttributions = (first, second) => { var _second$wasOffline; if (!first) { return second; } if (!second) { return first; } return { ...first, ...second, wasOffline: first.wasOffline === true || second.wasOffline === true ? true : (_second$wasOffline = second.wasOffline) !== null && _second$wasOffline !== void 0 ? _second$wasOffline : first.wasOffline }; }; /** * Attribution-preserving variant of `simplifySteps`. * * Steps may only merge when they have the same effective actor identity. Keeping the attribution * coupled to the step also ensures filtering a no-op/analytics step cannot shift array indexes. */ export function simplifyStepsWithAttribution(steps, stepAttributions, originalDoc) { const stepsToFilter = removeUnusedAttributedSteps(steps.map((step, index) => ({ step, stepAttribution: stepAttributions[index] })), originalDoc); return stepsToFilter.filter(({ step }) => !(step instanceof AnalyticsStep)).reduce((acc, current) => { var _previous$step$merge, _previous$step$merge2, _previous$step; const previous = acc[acc.length - 1]; const isSameIdentity = previous && getAttributionKey(previous.stepAttribution) === getAttributionKey(current.stepAttribution); const merged = isSameIdentity ? (_previous$step$merge = (_previous$step$merge2 = (_previous$step = previous.step).merge) === null || _previous$step$merge2 === void 0 ? void 0 : _previous$step$merge2.call(_previous$step, current.step)) !== null && _previous$step$merge !== void 0 ? _previous$step$merge : mergeReplaceSteps(previous.step, current.step) : null; if (merged) { acc[acc.length - 1] = { step: merged, stepAttribution: mergeAttributions(previous.stepAttribution, current.stepAttribution) }; } else { acc.push(current); } return acc; }, []); } /** * Does a first pass to remove steps that don't impact the document */ function removeUnusedAttributedSteps(stepsWithAttribution, originalDoc) { const finalSteps = []; let firstPassDoc = originalDoc; for (const stepWithAttribution of stepsWithAttribution) { const { step } = stepWithAttribution; const result = step.apply(firstPassDoc); if (result.failed === null && result.doc && !areNodesEqualIgnoreAttrs(firstPassDoc, result.doc, ['localId'])) { finalSteps.push(stepWithAttribution); firstPassDoc = result.doc; } } return finalSteps; } /** * Does a first pass to remove steps that don't impact the document */ function removeUnusedSteps(steps, originalDoc) { const finalSteps = []; let firstPassDoc = originalDoc; for (const step of steps) { const result = step.apply(firstPassDoc); if (result.failed === null && result.doc && !areNodesEqualIgnoreAttrs(firstPassDoc, result.doc, ['localId'])) { finalSteps.push(step); firstPassDoc = result.doc; } } return finalSteps; }