@sanity/desk-tool
Version:
Tool for managing all sorts of content in a structured manner
63 lines (61 loc) • 2.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Reconstruction = void 0;
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
/**
* A reconstruction represents a single reconstruction of a
*/
class Reconstruction {
constructor(timeline, doc, start, end) {
_defineProperty(this, "timeline", void 0);
_defineProperty(this, "start", void 0);
_defineProperty(this, "end", void 0);
_defineProperty(this, "doc", void 0);
_defineProperty(this, "_startDocument", void 0);
_defineProperty(this, "_endDocument", void 0);
_defineProperty(this, "_diff", void 0);
this.timeline = timeline;
this.start = start;
this.end = end;
this.doc = doc;
}
same(start, end) {
return this.start === start && this.end === end;
}
/** Returns the attributes as seen at the end of the range. */
endAttributes() {
return getAttrs(this.endDocument());
}
endDocument() {
if (!this._endDocument) {
this._endDocument = this.timeline.replayBackwardsUntil(this.end.end, this.doc);
}
return this._endDocument;
}
/** Returns the attributes as seen at the end of the range. */
startAttributes() {
return getAttrs(this.startDocument());
}
startDocument() {
if (!this.start) throw new Error('start required');
if (!this._startDocument) {
this._startDocument = this.timeline.replayBackwardsBetween(this.start.end, this.end.end - 1, this.endDocument());
}
return this._startDocument;
}
diff() {
if (!this._diff) {
if (!this.start) throw new Error('start required');
this._diff = this.timeline.calculateDiff(this.startDocument(), this.endDocument(), this.start.index + 1, this.end.index);
}
return this._diff;
}
}
exports.Reconstruction = Reconstruction;
function getAttrs(doc) {
return doc.draft || doc.published;
}