@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
50 lines (49 loc) • 1.45 kB
JavaScript
import { AddMarkStep, RemoveMarkStep } from '@atlaskit/editor-prosemirror/transform';
import { getAttributionKey } from '../colorSchemes/attributions';
import { mapStepRangeToFinal } from './mapStepRangeToFinal';
const extractMarkStep = step => {
if (step instanceof AddMarkStep) {
return {
type: 'add',
from: step.from,
to: step.to,
markName: step.mark.type.name
};
}
if (step instanceof RemoveMarkStep) {
return {
type: 'remove',
from: step.from,
to: step.to,
markName: step.mark.type.name
};
}
return undefined;
};
export const getMarkChangeRanges = (steps, stepAttributions = [], stepMaps = []) => {
const resultRanges = [];
let lastOp;
for (const [stepIndex, step] of steps.entries()) {
const op = extractMarkStep(step);
if (!op) {
continue;
}
// Check if previous operation cancels this one
if (lastOp && lastOp.from === op.from && lastOp.to === op.to && lastOp.markName === op.markName && lastOp.type !== op.type) {
resultRanges.pop();
} else {
const finalRange = mapStepRangeToFinal(op.from, op.to, stepIndex, stepMaps);
if (!finalRange) {
lastOp = op;
continue;
}
resultRanges.push({
attributionKey: getAttributionKey(stepAttributions[stepIndex]),
fromB: finalRange.fromB,
toB: finalRange.toB
});
}
lastOp = op;
}
return resultRanges;
};