UNPKG

@atlaskit/editor-plugin-batch-attribute-updates

Version:

Provider a runtime tool to merge an array of SetAttrsStep and/or AttrsSteps into an unique BatchAttrsStep

85 lines 4.03 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import { BatchAttrsStep, SetAttrsStep } from '@atlaskit/adf-schema/steps'; import { AttrStep } from '@atlaskit/editor-prosemirror/transform'; export var parseStepAttributeData = function parseStepAttributeData(step) { if (step instanceof SetAttrsStep) { return { position: step.pos, attrs: step.attrs }; } return { position: step.pos, attrs: _defineProperty({}, step.attr, step.value) }; }; export var groupAttributesPerPosition = function groupAttributesPerPosition(steps) { var mappedData = steps.reduce(function (acc, step) { var parsedStep = parseStepAttributeData(step); var previousAttributes = acc.get(parsedStep.position) || {}; acc.set(parsedStep.position, _objectSpread(_objectSpread({}, previousAttributes), parsedStep.attrs)); return acc; }, new Map()); return Array.from(mappedData, function (_ref) { var _ref2 = _slicedToArray(_ref, 2), position = _ref2[0], attrs = _ref2[1]; return { position: position, attrs: attrs }; }); }; var isValidSteps = function isValidSteps(steps) { var hasInvalidSteps = steps.some(function (step) { var isSetAttrsStep = step instanceof SetAttrsStep; var isAttrStep = step instanceof AttrStep; var isValidStep = isSetAttrsStep || isAttrStep; return !isValidStep; }); return !hasInvalidSteps; }; /** * Batches a series of attribute steps into a single `BatchAttrsStep`. * * This function takes a document and an array of steps, which should be instances of either * `SetAttrsStep` or `AttrStep`. It groups these steps by their position within the document * and aggregates their attributes. It then creates a `BatchAttrsStep` that applies all these * attribute changes at once. * * @param {Object} props - The properties required to batch steps. * @param {PMNode} props.doc - The ProseMirror document node in which the steps will be applied. * @param {Array<AttrStep | SetAttrsStep> | Array<Step>} props.steps - An array of steps that modify attributes. * * @returns {BatchAttrsStep} - A `BatchAttrsStep` that encapsulates all the attribute changes. * * @throws {Error} - Throws an error if any step in the steps array is not an attribute-related step. * @throws {Error} - Throws an error if a node does not exist at a given step position. */ export var batchSteps = function batchSteps(_ref3) { var doc = _ref3.doc, maybeAttrSteps = _ref3.steps; if (!isValidSteps(maybeAttrSteps)) { // This is important to avoid any bad usage to ending up with data loss throw new Error('BatchAttrsSteps error: Invalid BatchStep usage - non attribute step used'); } var positionsAndAttributesData = groupAttributesPerPosition(maybeAttrSteps); var data = positionsAndAttributesData.map(function (_ref4) { var position = _ref4.position, attrs = _ref4.attrs; var maybeNode = doc.nodeAt(position); if (!maybeNode) { throw new Error("BatchAttrsSteps error: Node does not exist at the position ".concat(position, ".")); } var nodeType = maybeNode.type.name; return { position: position, attrs: attrs, nodeType: nodeType }; }); return new BatchAttrsStep(data); };