@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
108 lines (98 loc) • 3.62 kB
JavaScript
import { SetAttrsStep } from '@atlaskit/adf-schema/steps';
import { AttrStep } from '@atlaskit/editor-prosemirror/transform';
var filterUndefined = function filterUndefined(x) {
return !!x;
};
// Attributes that indicate a change in media image
var mediaAttrs = ['id', 'collection', 'url'];
// Attribute that indicates a date change
var dateAttrs = ['timestamp'];
// Attribute that indicates a task item state change
var taskItemAttrs = ['state'];
// Attributes excluded from extension change detection (not meaningful content changes)
var extensionExcludedAttrs = ['localId'];
// Extension node type names
var extensionNodeNames = ['extension', 'inlineExtension', 'bodiedExtension'];
var getStepAttrs = function getStepAttrs(step) {
if (step instanceof AttrStep) {
return [step.attr];
}
if (step instanceof SetAttrsStep && step.attrs) {
return Object.keys(step.attrs);
}
return [];
};
export var getAttrChangeRanges = function getAttrChangeRanges(doc, steps) {
return steps.map(function (step) {
if (!(step instanceof AttrStep) && !(step instanceof SetAttrsStep)) {
return undefined;
}
var stepAttrs = getStepAttrs(step);
var $pos = doc.resolve(step.pos);
var nodeAtPos = doc.nodeAt(step.pos);
// date node: timestamp attribute change — highlight the date node itself (inline)
if (stepAttrs.some(function (v) {
return dateAttrs.includes(v);
}) && (nodeAtPos === null || nodeAtPos === void 0 ? void 0 : nodeAtPos.type.name) === 'date') {
return {
fromB: step.pos,
toB: step.pos + nodeAtPos.nodeSize,
isInline: true
};
}
// taskItem node: state attribute change — highlight the taskItem node
if (stepAttrs.some(function (v) {
return taskItemAttrs.includes(v);
}) && (nodeAtPos === null || nodeAtPos === void 0 ? void 0 : nodeAtPos.type.name) === 'taskItem') {
return {
fromB: step.pos,
toB: step.pos + nodeAtPos.nodeSize
};
}
// extension nodes: any attribute change except localId — highlight the node
if (nodeAtPos && extensionNodeNames.includes(nodeAtPos.type.name) && stepAttrs.some(function (v) {
return !extensionExcludedAttrs.includes(v);
})) {
var isInline = nodeAtPos.type.name === 'inlineExtension';
return {
fromB: step.pos,
toB: step.pos + nodeAtPos.nodeSize,
isInline: isInline
};
}
// media node: id/collection/url attribute change — highlight the mediaSingle parent
if (stepAttrs.some(function (v) {
return mediaAttrs.includes(v);
}) && $pos.parent.type === doc.type.schema.nodes.mediaSingle) {
var startPos = $pos.pos + $pos.parentOffset;
return {
fromB: startPos,
toB: startPos + $pos.parent.nodeSize - 1
};
}
return undefined;
}).filter(filterUndefined);
};
/**
* Check if the step was a valid attr change and affected the doc
*
* @param step Attr step to test
* @param beforeDoc Doc before the step
* @param afterDoc Doc after the step
* @returns Boolean if the change should show a decoration
*/
export var stepIsValidAttrChange = function stepIsValidAttrChange(step, beforeDoc, afterDoc) {
try {
if (step instanceof AttrStep || step instanceof SetAttrsStep) {
var attrStepAfter = afterDoc.nodeAt(step.pos);
var attrStepBefore = beforeDoc.nodeAt(step.pos);
// The change affected the document
if (attrStepAfter && attrStepBefore && !attrStepAfter.eq(attrStepBefore)) {
return true;
}
}
return false;
} catch (_unused) {
return false;
}
};