@sanity/desk-tool
Version:
Tool for managing all sorts of content in a structured manner
187 lines (182 loc) • 8.02 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.diffValue = diffValue;
var _diff = require("@sanity/diff");
var _utils = require("./utils");
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, 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 normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
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); }
class ArrayContentWrapper {
constructor(content, value, annotation, extractor) {
_defineProperty(this, "type", 'array');
_defineProperty(this, "value", void 0);
_defineProperty(this, "length", void 0);
_defineProperty(this, "annotation", void 0);
_defineProperty(this, "extractor", void 0);
_defineProperty(this, "content", void 0);
_defineProperty(this, "elements", []);
this.content = content;
this.value = value;
this.annotation = annotation;
this.extractor = extractor;
this.length = content.elements.length;
}
at(idx) {
if (idx >= this.length) throw new Error('out of bounds');
var input = this.elements[idx];
if (input) {
return input;
}
return this.elements[idx] = wrapValue(this.content.elements[idx], this.value[idx], this.extractor);
}
annotationAt(idx) {
var meta = this.content.metas[idx];
return this.extractor.fromMeta(meta);
}
}
class ObjectContentWrapper {
constructor(content, value, annotation, extractor) {
_defineProperty(this, "type", 'object');
_defineProperty(this, "value", void 0);
_defineProperty(this, "keys", void 0);
_defineProperty(this, "annotation", void 0);
_defineProperty(this, "extractor", void 0);
_defineProperty(this, "content", void 0);
_defineProperty(this, "fields", {});
this.content = content;
this.value = value;
this.annotation = annotation;
this.extractor = extractor;
this.keys = Object.keys(content.fields);
}
get(key) {
var input = this.fields[key];
if (input) {
return input;
}
var value = this.content.fields[key];
if (!value) return undefined;
return this.fields[key] = wrapValue(value, this.value[key], this.extractor);
}
}
class StringContentWrapper {
constructor(content, value, annotation, extractor) {
_defineProperty(this, "type", 'string');
_defineProperty(this, "value", void 0);
_defineProperty(this, "annotation", void 0);
_defineProperty(this, "extractor", void 0);
_defineProperty(this, "content", void 0);
this.content = content;
this.value = value;
this.annotation = annotation;
this.extractor = extractor;
}
sliceAnnotation(start, end) {
var result = [];
var idx = 0;
function push(text, annotation) {
if (result.length > 0) {
var lst = result[result.length - 1];
if ((0, _utils.isSameAnnotation)(lst.annotation, annotation)) {
lst.text += text;
return;
}
}
result.push({
text,
annotation
});
}
var _iterator = _createForOfIteratorHelper(this.content.parts),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var part = _step.value;
var length = part.value.length;
var subStart = Math.max(0, start - idx);
if (subStart < length) {
// The start of the slice is inside this part somewhere.
// Figure out where the end is:
var subEnd = Math.min(length, end - idx);
// If the end of the slice is before this part, then we're guaranteed
// that there are no more parts.
if (subEnd <= 0) break;
push(part.value.slice(subStart, subEnd), this.extractor.fromValue(part));
}
idx += length;
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return result;
}
}
function wrapValue(value, raw, extractor) {
var annotation = extractor.fromValue(value);
if (value.content) {
switch (value.content.type) {
case 'array':
return new ArrayContentWrapper(value.content, raw, annotation, extractor);
case 'object':
return new ObjectContentWrapper(value.content, raw, annotation, extractor);
case 'string':
return new StringContentWrapper(value.content, raw, annotation, extractor);
default:
// do nothing
}
}
return (0, _diff.wrap)(raw, annotation);
}
function extractAnnotationForFromInput(timeline, firstChunk, meta) {
if (meta) {
// The next transaction is where it disappeared:
return annotationForTransactionIndex(timeline, meta.transactionIndex + 1, meta.chunk.index);
} else if (firstChunk) {
return annotationForTransactionIndex(timeline, firstChunk.start, firstChunk.index);
}
return null;
}
function extractAnnotationForToInput(timeline, meta) {
if (meta) {
return annotationForTransactionIndex(timeline, meta.transactionIndex, meta.chunk.index);
}
return null;
}
function annotationForTransactionIndex(timeline, idx, chunkIdx) {
var tx = timeline.transactionByIndex(idx);
if (!tx) return null;
var chunk = timeline.chunkByTransactionIndex(idx, chunkIdx);
if (!chunk) return null;
return {
chunk,
timestamp: tx.timestamp,
author: tx.author
};
}
// eslint-disable-next-line max-params
function diffValue(timeline, firstChunk, from, fromRaw, to, toRaw) {
var fromInput = wrapValue(from, fromRaw, {
fromValue(value) {
return extractAnnotationForFromInput(timeline, firstChunk, value.endMeta);
},
fromMeta(meta) {
return extractAnnotationForFromInput(timeline, firstChunk, meta);
}
});
var toInput = wrapValue(to, toRaw, {
fromValue(value) {
return extractAnnotationForToInput(timeline, value.startMeta);
},
fromMeta(meta) {
return extractAnnotationForToInput(timeline, meta);
}
});
return (0, _diff.diffInput)(fromInput, toInput);
}