UNPKG

@atlaskit/editor-plugin-show-diff

Version:

ShowDiff plugin for @atlaskit/editor-core

135 lines (127 loc) 7.58 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.smartChangeLevel = exports.rangesOverlap = exports.mergeOverlappingByNewDocRange = exports.makePromotedChange = exports.createSpans = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); 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) { (0, _defineProperty2.default)(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; } function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } /** * The granularity level a `smart` change was promoted to. Stored on span `data` so the * decoration pipeline can render each level differently (e.g. node-level deletions shown * below the new content). `null` for non-promoted (inline) changes. */ /** Span `data` payload carried by promoted `smart` changes. */ /** * Build the `deleted`/`inserted` span arrays a `Change` needs. A length of 0 yields an * empty array (no change on that side). */ var createSpans = exports.createSpans = function createSpans(length) { var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; return length > 0 ? [{ length: length, data: data }] : []; }; /** * Build a promoted `Change` spanning `[fromA,toA)` in the original doc and `[fromB,toB)` * in the new doc, with both sides marked changed (mirrors `groupChangesByBlock` so the * decoration pipeline renders both the inserted-block decoration and the deleted-node * widget). * * `level` tags the change's spans so downstream decoration logic can treat node-level * promotions specially (see `smartChangeLevel`). */ var makePromotedChange = exports.makePromotedChange = function makePromotedChange(fromA, toA, fromB, toB, level) { var data = level ? { smartLevel: level } : null; // `deleted` and `inserted` must be independent arrays: downstream code may mutate one // side (push/splice), which would silently corrupt the other if they shared a reference. return { fromA: fromA, toA: toA, fromB: fromB, toB: toB, deleted: createSpans(Math.max(0, toA - fromA), data), inserted: createSpans(Math.max(0, toB - fromB), data) }; }; /** * Read the `smartLevel` tag off a change's spans, if present. Returns `undefined` for * changes not produced by the `smart` classifier (e.g. inline/block/step diff types). */ var smartChangeLevel = exports.smartChangeLevel = function smartChangeLevel(change) { var spans = [].concat((0, _toConsumableArray2.default)(change.inserted), (0, _toConsumableArray2.default)(change.deleted)); var _iterator = _createForOfIteratorHelper(spans), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var span = _step.value; var data = span.data; if (data && (0, _typeof2.default)(data) === 'object' && 'smartLevel' in data) { return data.smartLevel; } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return undefined; }; /** True when a change has no original-side (deleted) content — a pure insertion. */ var isPureInsertion = function isPureInsertion(change) { return change.toA <= change.fromA; }; /** * Coalesce changes that overlap in new-doc (B) coordinates. * * Merging two changes takes the UNION of their A (original-doc) ranges. That is only safe when * their A ranges actually touch/overlap — OR when at least one is a pure insertion (empty A). * Otherwise, merging a deletion at A[110,152] with a later edit at A[162,168] would fabricate an * A span [110,168] covering the untouched gap, which then overlaps a *different* change's A range * (making the same original content appear deleted twice). So we require B-overlap AND * (A-overlap OR a pure insertion) before coalescing. */ var mergeOverlappingByNewDocRange = exports.mergeOverlappingByNewDocRange = function mergeOverlappingByNewDocRange(changes) { if (changes.length <= 1) { return changes; } var sorted = (0, _toConsumableArray2.default)(changes).sort(function (l, r) { return l.fromB - r.fromB; }); var merged = []; var current = _objectSpread({}, sorted[0]); for (var i = 1; i < sorted.length; i++) { var next = sorted[i]; var bOverlaps = next.fromB <= current.toB; // A ranges may be unioned only if they touch, or if either side contributes no A content. var aMergeable = isPureInsertion(current) || isPureInsertion(next) || rangesOverlap(current.fromA, current.toA, next.fromA, next.toA) || next.fromA <= current.toA; // adjacency (sorted by B, A usually monotonic) if (bOverlaps && aMergeable) { current = { fromA: Math.min(current.fromA, next.fromA), toA: Math.max(current.toA, next.toA), fromB: Math.min(current.fromB, next.fromB), toB: Math.max(current.toB, next.toB), deleted: [].concat((0, _toConsumableArray2.default)(current.deleted), (0, _toConsumableArray2.default)(next.deleted)), inserted: [].concat((0, _toConsumableArray2.default)(current.inserted), (0, _toConsumableArray2.default)(next.inserted)) }; } else { merged.push(current); current = _objectSpread({}, next); } } merged.push(current); return merged; }; /** True when two half-open ranges overlap. */ var rangesOverlap = exports.rangesOverlap = function rangesOverlap(aFrom, aTo, bFrom, bTo) { return aFrom < bTo && bFrom < aTo; };