UNPKG

@atlaskit/editor-plugin-show-diff

Version:

ShowDiff plugin for @atlaskit/editor-core

50 lines (48 loc) 1.82 kB
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import { SetAttrsStep } from '@atlaskit/adf-schema/steps'; import { AttrStep } from '@atlaskit/editor-prosemirror/transform'; var filterUndefined = function filterUndefined(x) { return !!x; }; // Currently allow attributes that indicats a change in media image var allowedAttrs = ['id', 'collection', 'url']; export var getAttrChangeRanges = function getAttrChangeRanges(doc, steps) { return steps.map(function (step) { if (step instanceof AttrStep && allowedAttrs.includes(step.attr) || step instanceof SetAttrsStep && step.attrs && _toConsumableArray(Object.keys(step.attrs)).some(function (v) { return allowedAttrs.includes(v); })) { var $pos = doc.resolve(step.pos); if ($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; } };