UNPKG

@atlaskit/adf-utils

Version:

Set of utilities to traverse, modify and create ADF documents.

171 lines (170 loc) 7.74 kB
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; 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 { traverse } from '../traverse/traverse'; import { tableRow, tableCell, paragraph, listItem } from '../builders'; import { isEmpty } from './helpers'; var getMaxColumnsCountForTable = function getMaxColumnsCountForTable(tableNode) { var _tableNode$content; var colsInRow = 1; (_tableNode$content = tableNode.content) === null || _tableNode$content === void 0 || _tableNode$content.forEach(function (childNode) { var _childNode$content; if ((childNode === null || childNode === void 0 ? void 0 : childNode.type) === 'tableRow' && typeof ((_childNode$content = childNode.content) === null || _childNode$content === void 0 ? void 0 : _childNode$content.length) === 'number' && childNode.content.length > colsInRow) { colsInRow = childNode.content.length; } }); return colsInRow; }; var createValidEmptyContent = function createValidEmptyContent(node) { switch (node.type) { case 'tableCell': return [paragraph()]; default: return []; } }; var fixIfTableCellInvalidEmpty = function fixIfTableCellInvalidEmpty(reportTransform) { return function (node) { if (isEmpty(node)) { reportTransform(); return _objectSpread(_objectSpread({}, node), {}, { content: createValidEmptyContent(node) }); } return; }; }; var hasNonListItemChildren = function hasNonListItemChildren(node) { var _node$content; return (_node$content = node.content) === null || _node$content === void 0 ? void 0 : _node$content.some(function (node) { return (node === null || node === void 0 ? void 0 : node.type) !== 'listItem'; }); }; var hasEmptyListItemChildren = function hasEmptyListItemChildren(node) { var _node$content2; return (_node$content2 = node.content) === null || _node$content2 === void 0 ? void 0 : _node$content2.some(function (childNode) { var _childNode$content2; return (childNode === null || childNode === void 0 ? void 0 : childNode.type) === 'listItem' && !((_childNode$content2 = childNode.content) !== null && _childNode$content2 !== void 0 && _childNode$content2.length); }); }; var tryCreateValidListItemWrappedChildren = function tryCreateValidListItemWrappedChildren(parentListNode) { var _parentListNode$conte; return (_parentListNode$conte = parentListNode.content) === null || _parentListNode$conte === void 0 ? void 0 : _parentListNode$conte.map(function (childNode) { if (childNode) { switch (childNode.type) { case 'listItem': { if (isEmpty(childNode)) { var result = listItem([paragraph()]); return result; } return childNode; } case 'text': return listItem([paragraph(childNode)]); default: // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any return listItem([childNode]); } } return childNode; }); }; var fixIfListParentWithInvalidListItemChildren = function fixIfListParentWithInvalidListItemChildren(reportTransform) { return function (node) { if (hasNonListItemChildren(node) || hasEmptyListItemChildren(node)) { reportTransform(); return _objectSpread(_objectSpread({}, node), {}, { content: tryCreateValidListItemWrappedChildren(node) }); } return; }; }; var hasNonTableRowChildren = function hasNonTableRowChildren(node) { var _node$content3; return (_node$content3 = node.content) === null || _node$content3 === void 0 ? void 0 : _node$content3.some(function (node) { return (node === null || node === void 0 ? void 0 : node.type) !== 'tableRow'; }); }; var tryCreateValidTableRowWrappedChildren = function tryCreateValidTableRowWrappedChildren(parentTableNode) { var _parentTableNode$cont; var maxColsCount = getMaxColumnsCountForTable(parentTableNode); return (_parentTableNode$cont = parentTableNode.content) === null || _parentTableNode$cont === void 0 ? void 0 : _parentTableNode$cont.map(function (childNode) { if (childNode) { switch (childNode.type) { case 'text': { return tableRow([tableCell({})(paragraph(childNode))].concat(_toConsumableArray(new Array(maxColsCount - 1).fill(tableCell({})(paragraph()))))); } case 'tableCell': { return tableRow([childNode]); } case 'tableRow': { if (isEmpty(childNode)) { return tableRow(_toConsumableArray(new Array(maxColsCount).fill(tableCell({})(paragraph())))); } return childNode; } default: return childNode; } } return childNode; }); }; var hasEmptyTableRowChildren = function hasEmptyTableRowChildren(node) { var _node$content4; return (node === null || node === void 0 || (_node$content4 = node.content) === null || _node$content4 === void 0 ? void 0 : _node$content4.some(function (node) { var _node$content5; return (node === null || node === void 0 ? void 0 : node.type) === 'tableRow' && (node === null || node === void 0 || (_node$content5 = node.content) === null || _node$content5 === void 0 ? void 0 : _node$content5.length) === 0; })) || undefined; }; var fixIfTableParentWithInvalidTableRowChildren = function fixIfTableParentWithInvalidTableRowChildren(reportTransform) { return function (node) { if (hasEmptyTableRowChildren(node) || hasNonTableRowChildren(node)) { reportTransform(); return _objectSpread(_objectSpread({}, node), {}, { content: tryCreateValidTableRowWrappedChildren(node) }); } return; }; }; var removeMediaSingleWithNoContent = function removeMediaSingleWithNoContent(reportTransform) { return function (node) { if (isEmpty(node)) { reportTransform(); return false; } return; }; }; export var transformNodesMissingContent = function transformNodesMissingContent(adf) { var isTransformed = false; var reportTransform = function reportTransform() { isTransformed = true; }; var transformedAdf = traverse(adf, { tableCell: fixIfTableCellInvalidEmpty(reportTransform) }); // Empty mediaSingle nodes get stripped from the document and so this transform // will create an empty listItem if the media node is in a list. Empty listItems // are invalid adf, so we need to transform the mediaSingle nodes first. transformedAdf = traverse(transformedAdf || adf, { mediaSingle: removeMediaSingleWithNoContent(reportTransform) }); transformedAdf = traverse(transformedAdf || adf, { bulletList: fixIfListParentWithInvalidListItemChildren(reportTransform), orderedList: fixIfListParentWithInvalidListItemChildren(reportTransform), table: fixIfTableParentWithInvalidTableRowChildren(reportTransform) }); return { transformedAdf: transformedAdf, isTransformed: isTransformed }; };