@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
44 lines (42 loc) • 1.69 kB
JavaScript
/** U+0000 cannot occur in ProseMirror text content, so this cannot collide with document text. */
var NODE_MARKER = "\0";
/**
* Node types and text only; marks and attrs excluded.
*
* Text is unprefixed so adjacent runs concatenate — a run only splits because its marks differ, so
* `strong("hello")` and `strong("hel") + em("lo")` must match. Attrs are excluded because a
* reconstructed agent edit regenerates `localId`s; `getAttrChangeRanges` reports attrs separately.
*/
var markFreeSignature = function markFreeSignature(doc, from, to) {
var parts = [];
doc.nodesBetween(from, to, function (node, pos) {
if (node.isText) {
var _node$text;
var start = Math.max(pos, from) - pos;
var end = Math.min(pos + node.nodeSize, to) - pos;
parts.push(((_node$text = node.text) !== null && _node$text !== void 0 ? _node$text : '').slice(start, end));
} else {
parts.push("".concat(NODE_MARKER).concat(node.type.name));
}
return true;
});
return parts.join('');
};
/**
* Whether a change differs only in marks, with identical structure and text on both sides. Only
* produced when a mark-aware token encoder is in use.
*/
export var isMarkOnlyChange = function isMarkOnlyChange(_ref) {
var change = _ref.change,
newDoc = _ref.newDoc,
originalDoc = _ref.originalDoc;
var fromA = change.fromA,
toA = change.toA,
fromB = change.fromB,
toB = change.toB;
var isPureInsertionOrDeletion = toA === fromA || toB === fromB;
if (isPureInsertionOrDeletion || toA - fromA !== toB - fromB) {
return false;
}
return markFreeSignature(originalDoc, fromA, toA) === markFreeSignature(newDoc, fromB, toB);
};