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

92 lines (91 loc) 4.48 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.parseStepAttributeData = exports.groupAttributesPerPosition = exports.batchSteps = void 0; var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _steps = require("@atlaskit/adf-schema/steps"); var _transform = require("@atlaskit/editor-prosemirror/transform"); 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) { (0, _defineProperty2.default)(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; } var parseStepAttributeData = exports.parseStepAttributeData = function parseStepAttributeData(step) { if (step instanceof _steps.SetAttrsStep) { return { position: step.pos, attrs: step.attrs }; } return { position: step.pos, attrs: (0, _defineProperty2.default)({}, step.attr, step.value) }; }; var groupAttributesPerPosition = exports.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 = (0, _slicedToArray2.default)(_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 _steps.SetAttrsStep; var isAttrStep = step instanceof _transform.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. */ var batchSteps = exports.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 _steps.BatchAttrsStep(data); };