UNPKG

@atlaskit/editor-plugin-collab-edit

Version:

Collab Edit plugin for @atlaskit/editor-core

88 lines (86 loc) • 3.17 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import { BatchAttrsStep } from '@atlaskit/adf-schema/steps/batch-attrs-step'; import { SetAttrsStep } from '@atlaskit/adf-schema/steps/set-attrs'; import { AddMarkStep, AddNodeMarkStep, AttrStep, DocAttrStep, RemoveMarkStep, RemoveNodeMarkStep } from '@atlaskit/editor-prosemirror/transform'; function groupBy(array, keyGetter) { // Check group by exists, and that it's a function. If so, use the native browser code if ('groupBy' in Object && typeof Object.groupBy === 'function') { return Object.groupBy(array, keyGetter); } // Fallback to custom implementation var map = {}; array.forEach(function (item) { var key = keyGetter(item); if (!map[key]) { map[key] = []; } map[key].push(item); }); return map; } /** * Sanitizes a given ProseMirror step by extracting its type and non-UCG relevant attributes. * * @param {Step} step - The ProseMirror step to be sanitized. * @returns {SanitizedStep} - The sanitized step with only necessary information. * * @example * ``` * const step = new AttrStep(10, 'colwidth', [123, 451] ); * const sanitized = sanitizeStep(step); * * // Output: { stepType: 'attr', attr: 'example' } * ``` */ export var sanitizeStep = function sanitizeStep(step) { var serializedStep = step.toJSON(); var sanitizedStep = { stepType: serializedStep.stepType }; if (step instanceof AttrStep || step instanceof DocAttrStep) { sanitizedStep.attr = step.attr; } else if (step instanceof SetAttrsStep) { // Combines all attrs keys separated by _ to one single string sanitizedStep.attr = Object.keys(step.attrs).sort().join('_'); } else if (step instanceof AddMarkStep || step instanceof RemoveMarkStep || step instanceof RemoveNodeMarkStep || step instanceof AddNodeMarkStep) { sanitizedStep.markType = step.mark.type.name; } else if (step instanceof BatchAttrsStep) { var batched = step.data.map(function (_ref) { var nodeType = _ref.nodeType, attrs = _ref.attrs; return "".concat(nodeType, "_").concat(Object.keys(attrs).sort().join('_')); }); sanitizedStep.attr = batched.sort().join('_'); } return sanitizedStep; }; /** * Groups sanitized steps by their type and counts their occurrences. * * @param {SanitizedStep[]} sanitizedSteps - An array of sanitized steps. * @returns {Record<string, number>} - An object where keys are step types and values are their counts. * * @example * ``` * const input = [ * { stepType: 'attr', attr: 'colwidth' }, * { stepType: 'mark', markType: 'bold' }, * { stepType: 'attr', attr: 'colwidth' } * ]; * * const grouped = groupSteps(input); * // Output: { 'attr_example': 2, 'mark_bold': 1 } * ``` */ export var groupSteps = function groupSteps(sanitizedSteps) { var grouped = groupBy(sanitizedSteps, function (e) { return Object.values(e).join('_'); }); return Object.entries(grouped).reduce(function (acc, _ref2) { var _ref3 = _slicedToArray(_ref2, 2), key = _ref3[0], value = _ref3[1]; acc[key] = Array.isArray(value) ? value.length : 0; return acc; }, {}); };