@atlaskit/editor-plugin-collab-edit
Version:
Collab Edit plugin for @atlaskit/editor-core
95 lines (92 loc) • 3.47 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.sanitizeStep = exports.groupSteps = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _batchAttrsStep = require("@atlaskit/adf-schema/steps/batch-attrs-step");
var _setAttrs = require("@atlaskit/adf-schema/steps/set-attrs");
var _transform = require("@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' }
* ```
*/
var sanitizeStep = exports.sanitizeStep = function sanitizeStep(step) {
var serializedStep = step.toJSON();
var sanitizedStep = {
stepType: serializedStep.stepType
};
if (step instanceof _transform.AttrStep || step instanceof _transform.DocAttrStep) {
sanitizedStep.attr = step.attr;
} else if (step instanceof _setAttrs.SetAttrsStep) {
// Combines all attrs keys separated by _ to one single string
sanitizedStep.attr = Object.keys(step.attrs).sort().join('_');
} else if (step instanceof _transform.AddMarkStep || step instanceof _transform.RemoveMarkStep || step instanceof _transform.RemoveNodeMarkStep || step instanceof _transform.AddNodeMarkStep) {
sanitizedStep.markType = step.mark.type.name;
} else if (step instanceof _batchAttrsStep.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 }
* ```
*/
var groupSteps = exports.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 = (0, _slicedToArray2.default)(_ref2, 2),
key = _ref3[0],
value = _ref3[1];
acc[key] = Array.isArray(value) ? value.length : 0;
return acc;
}, {});
};