UNPKG

@atlaskit/editor-plugin-show-diff

Version:

ShowDiff plugin for @atlaskit/editor-core

114 lines (103 loc) 3.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.stepIsValidAttrChange = exports.getAttrChangeRanges = void 0; var _steps = require("@atlaskit/adf-schema/steps"); var _transform = require("@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 _transform.AttrStep) { return [step.attr]; } if (step instanceof _steps.SetAttrsStep && step.attrs) { return Object.keys(step.attrs); } return []; }; var getAttrChangeRanges = exports.getAttrChangeRanges = function getAttrChangeRanges(doc, steps) { return steps.map(function (step) { if (!(step instanceof _transform.AttrStep) && !(step instanceof _steps.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 */ var stepIsValidAttrChange = exports.stepIsValidAttrChange = function stepIsValidAttrChange(step, beforeDoc, afterDoc) { try { if (step instanceof _transform.AttrStep || step instanceof _steps.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; } };