UNPKG

@atlaskit/editor-plugin-show-diff

Version:

ShowDiff plugin for @atlaskit/editor-core

114 lines (113 loc) 5.83 kB
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`. */ import { ChangeSet, simplifyChanges } from 'prosemirror-changeset'; import { attrAwareTokenEncoder } from './attrAwareTokenEncoder'; import { diffBySteps } from './diffBySteps'; import { groupChangesByBlock } from './groupChangesByBlock'; import { optimizeChanges } from './optimizeChanges'; import { simplifySteps } from './simplifySteps'; import { classifySmartChanges } from './smart/classifySmartChanges'; /** * 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. */ export var 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 = 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 : undefined; var changeset = ChangeSet.create(originalDoc, undefined, tokenEncoder).addSteps(steppedDoc, stepMaps, steppedDoc); if (diffType === 'smart') { return { changes: classifySmartChanges({ changes: simplifyChanges(changeset.changes, steppedDoc), originalDoc: originalDoc, newDoc: steppedDoc, locale: locale, thresholds: smartThresholds }), newDoc: steppedDoc }; } if (diffType === 'block') { return { changes: groupChangesByBlock(changeset.changes, originalDoc, steppedDoc), newDoc: steppedDoc }; } if (diffType === 'step') { return { changes: diffBySteps(originalDoc, simplifiedSteps), newDoc: steppedDoc }; } return { changes: optimizeChanges(simplifyChanges(changeset.changes, steppedDoc)), newDoc: steppedDoc }; };