@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
119 lines (117 loc) • 6.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.computeDiffChanges = void 0;
var _prosemirrorChangeset = require("prosemirror-changeset");
var _attrAwareTokenEncoder = require("./attrAwareTokenEncoder");
var _diffBySteps = require("./diffBySteps");
var _groupChangesByBlock = require("./groupChangesByBlock");
var _optimizeChanges = require("./optimizeChanges");
var _simplifySteps = require("./simplifySteps");
var _classifySmartChanges = require("./smart/classifySmartChanges");
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; } /**
* Generic, editor-state-free diff-change computation.
*
* Turns an `originalDoc` + forward `steps` into the classified `Change[]` for the
* requested `diffType`, plus the reconstructed new document — the same classifier
* the diff overlay uses (`calculateDiffDecorations` → `getChanges`), exposed as a
* reusable utility (e.g. to derive reviewable segments without rendering
* decorations). It runs the requested `diffType` DIRECTLY and does NOT apply the
* overlay's `platform_editor_ai_smart_diff` gate, so a `smart` result here can
* differ from what the overlay draws when that gate is off (the overlay falls back
* to inline); callers that need overlay parity must account for that gate.
*
* Pure: no `EditorState`, no transactions, no React. The new document is
* reconstructed by applying the (simplified) steps to `originalDoc`.
*/
/**
* Apply the (simplified) steps to `originalDoc` to reconstruct the new doc, and
* compute the classified `Change[]` for the requested `diffType`. Mirrors the
* body of `calculateDiffDecorations` → `getChanges` so callers stay in lock-step
* with the rendered diff. Returns an empty change list (and `originalDoc` as the
* new doc) when there are no steps.
*/
var computeDiffChanges = exports.computeDiffChanges = function computeDiffChanges(_ref) {
var originalDoc = _ref.originalDoc,
steps = _ref.steps,
_ref$diffType = _ref.diffType,
diffType = _ref$diffType === void 0 ? 'smart' : _ref$diffType,
_ref$locale = _ref.locale,
locale = _ref$locale === void 0 ? 'en' : _ref$locale,
smartThresholds = _ref.smartThresholds;
if (!steps || steps.length === 0) {
return {
changes: [],
newDoc: originalDoc
};
}
var simplifiedSteps = (0, _simplifySteps.simplifySteps)(steps, originalDoc);
var steppedDoc = originalDoc;
var stepMaps = [];
try {
var _iterator = _createForOfIteratorHelper(simplifiedSteps),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var step = _step.value;
var result = step.apply(steppedDoc);
if (result.failed === null && result.doc) {
stepMaps.push(step.getMap());
steppedDoc = result.doc;
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
} catch (_unused) {
// A step's positions fell outside the current doc — `ReplaceStep.apply`
// throws a RangeError (rather than returning a failed result) when a
// position exceeds the doc size. This can happen for pathological or
// inconsistent step sequences. Bail with no changes rather than letting the
// error propagate to the caller (which, for the PSR, would crash the
// toolbar); the consumer then falls back to its non-diff segmenting path.
return {
changes: [],
newDoc: originalDoc
};
}
// The attribute-aware encoder only affects the `smart` classification, so it is
// applied only for that type. (This utility intentionally applies no feature
// gate — see the file docstring — but a caller requesting `smart` is already
// behind the smart-diff gate.)
var tokenEncoder = diffType === 'smart' ? _attrAwareTokenEncoder.attrAwareTokenEncoder : undefined;
var changeset = _prosemirrorChangeset.ChangeSet.create(originalDoc, undefined, tokenEncoder).addSteps(steppedDoc, stepMaps, steppedDoc);
if (diffType === 'smart') {
return {
changes: (0, _classifySmartChanges.classifySmartChanges)({
changes: (0, _prosemirrorChangeset.simplifyChanges)(changeset.changes, steppedDoc),
originalDoc: originalDoc,
newDoc: steppedDoc,
locale: locale,
thresholds: smartThresholds
}),
newDoc: steppedDoc
};
}
if (diffType === 'block') {
return {
changes: (0, _groupChangesByBlock.groupChangesByBlock)(changeset.changes, originalDoc, steppedDoc),
newDoc: steppedDoc
};
}
if (diffType === 'step') {
return {
changes: (0, _diffBySteps.diffBySteps)(originalDoc, simplifiedSteps),
newDoc: steppedDoc
};
}
return {
changes: (0, _optimizeChanges.optimizeChanges)((0, _prosemirrorChangeset.simplifyChanges)(changeset.changes, steppedDoc)),
newDoc: steppedDoc
};
};