@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
42 lines (41 loc) • 1.01 kB
JavaScript
import { AddMarkStep, RemoveMarkStep } from '@atlaskit/editor-prosemirror/transform';
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 => {
const resultRanges = [];
let lastOp;
for (const step of steps) {
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 {
resultRanges.push({
fromB: op.from,
toB: op.to
});
}
lastOp = op;
}
return resultRanges;
};