@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
36 lines (35 loc) • 1.89 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
/**
* Groups adjacent changes to reduce visual fragmentation in diffs.
* Merges consecutive insertions and deletions that are close together.
*/
export function optimizeChanges(changes) {
if (changes.length <= 1) {
return changes;
}
var optimized = [];
var current = _objectSpread({}, changes[0]);
for (var i = 1; i < changes.length; i++) {
var next = changes[i];
// Check if changes are adjacent or very close (within 2 positions)
var isAdjacent = next.fromB <= current.toB + 2;
if (isAdjacent) {
current = {
fromA: current.fromA,
toA: Math.max(current.toA, next.toA),
fromB: current.fromB,
toB: Math.max(current.toB, next.toB),
deleted: [].concat(_toConsumableArray(current.deleted), _toConsumableArray(next.deleted)),
inserted: [].concat(_toConsumableArray(current.inserted), _toConsumableArray(next.inserted))
};
} else {
optimized.push(current);
current = _objectSpread({}, next);
}
}
optimized.push(current);
return optimized;
}