@atlaskit/editor-plugin-block-menu
Version:
BlockMenu plugin for @atlaskit/editor-core
103 lines (98 loc) • 5.29 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.convertEachNodeStep = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _transform = require("../transform");
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
/**
* Handles the edge case where transforming multi-selection to decisionList results in
* all content breaking out (no valid children for the target). In this case, creates an empty
* decisionList to ensure the target type exists.
*
* Similar to handleEmptyContainerEdgeCase in wrapMixedContentStep, but specifically for
* multi-to-decisionList transformations where blockquotes break out.
*
* @param result - The current result nodes after processing
* @param hasCreatedDecisionList - Whether a decisionList was already created during processing
* @param targetNodeTypeName - The target node type name
* @param schema - The schema
* @returns The result nodes with an empty decisionList prepended if needed, or the original result
*/
var handleEmptyDecisionListEdgeCase = function handleEmptyDecisionListEdgeCase(result, hasCreatedDecisionList, targetNodeTypeName, schema) {
// Only apply this edge case for decisionList target
if (targetNodeTypeName !== 'decisionList') {
return result;
}
// If no decisionList was created but we have nodes in result, all content broke out
var allContentBrokeOut = !hasCreatedDecisionList && result.length > 0;
if (!allContentBrokeOut) {
return result;
}
// Create an empty decisionList
var decisionListType = schema.nodes.decisionList;
var decisionItemType = schema.nodes.decisionItem;
if (decisionListType && decisionItemType) {
var emptyDecisionItem = decisionItemType.createAndFill();
if (emptyDecisionItem) {
var emptyDecisionList = decisionListType.createAndFill({}, [emptyDecisionItem]);
if (emptyDecisionList) {
return [emptyDecisionList].concat((0, _toConsumableArray2.default)(result));
}
}
}
return result;
};
var convertEachNodeStep = exports.convertEachNodeStep = function convertEachNodeStep(nodes, context) {
var schema = context.schema,
targetNodeTypeName = context.targetNodeTypeName,
targetAttrs = context.targetAttrs;
var targetNodeType = schema.nodes[targetNodeTypeName];
if (!targetNodeType) {
return nodes;
}
var blockquoteType = schema.nodes.blockquote;
var resultNodes = [];
var hasCreatedDecisionList = false;
var _iterator = _createForOfIteratorHelper(nodes),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var node = _step.value;
// Edge case: blockquotes should break out when transforming to decisionList
if (targetNodeTypeName === 'decisionList' && blockquoteType && node.type === blockquoteType) {
resultNodes.push(node);
continue;
}
var transformedNodes = (0, _transform.convertNodesToTargetType)({
sourceNodes: [node],
targetNodeType: targetNodeType,
schema: schema,
isNested: false,
targetAttrs: targetAttrs
});
if (transformedNodes.length > 0) {
resultNodes.push.apply(resultNodes, (0, _toConsumableArray2.default)(transformedNodes));
// Track if we created a decisionList
if (targetNodeTypeName === 'decisionList' && transformedNodes.some(function (n) {
return n.type === targetNodeType;
})) {
hasCreatedDecisionList = true;
}
} else {
resultNodes.push(node);
}
}
// Handle edge case: if no decisionList was created, create an empty one
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
var finalResult = handleEmptyDecisionListEdgeCase(resultNodes, hasCreatedDecisionList, targetNodeTypeName, schema);
return finalResult;
};