UNPKG

@atlaskit/editor-plugin-block-menu

Version:

BlockMenu plugin for @atlaskit/editor-core

80 lines (78 loc) 4.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.upgradePanelNodesToPanelC1 = exports.isTransformDisabledBasedOnStepsConfig = exports.convertNodesToTargetType = void 0; var _nodeTypeUtils = require("@atlaskit/editor-common/utils/node-type-utils"); var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals"); var _utils = require("../transform-node-utils/utils"); var _TRANSFORMATION_MATRIX = require("./TRANSFORMATION_MATRIX"); var _types = require("./types"); // Upgrade broken-out panel nodes to panel_c1 if the parent node allows it var upgradePanelNodesToPanelC1 = exports.upgradePanelNodesToPanelC1 = function upgradePanelNodesToPanelC1(nodes, parentNode, schema) { if (!schema.nodes['panel_c1'] || !(0, _expValEquals.expValEquals)('platform_editor_nest_table_in_panel', 'isEnabled', true)) { return nodes; } return nodes.map(function (node) { if (node.type.name === 'panel') { var shouldUsePanelC1 = !parentNode || (0, _nodeTypeUtils.isNodeTypeValidChildOf)('panel_c1', parentNode, schema); if (shouldUsePanelC1) { var _schema$nodes$panel_c; return (_schema$nodes$panel_c = schema.nodes['panel_c1'].createAndFill(node.attrs, node.content, node.marks)) !== null && _schema$nodes$panel_c !== void 0 ? _schema$nodes$panel_c : node; } } return node; }); }; /** * Convert a list of nodes to a target node type. * If no steps are found, the source nodes are returned unchanged. * If steps are found, they are applied to the source nodes in order. * If a step returns an empty array, the source nodes are returned. * If a step returns a non-empty array, that array is returned. * @param args - The conversion arguments * @param args.sourceNodes - The list of nodes to convert * @param args.targetNodeType - The type of node to convert into * @param args.schema - The schema to use for the conversion * @param args.isNested - Whether the conversion is nested * @param args.targetAttrs - The attributes to use for the conversion * @param args.parentNode - The parent node of the selected node * @returns The converted list of nodes */ var convertNodesToTargetType = exports.convertNodesToTargetType = function convertNodesToTargetType(_ref) { var sourceNodes = _ref.sourceNodes, targetNodeType = _ref.targetNodeType, schema = _ref.schema, isNested = _ref.isNested, targetAttrs = _ref.targetAttrs, parentNode = _ref.parentNode; var sourceNode = sourceNodes.at(0); if (!sourceNode) { return sourceNodes; } var selectedNodeTypeName = (0, _types.toNodeTypeValue)((0, _types.getNodeName)(sourceNodes)); var initialTargetNodeTypeName = (0, _types.toNodeTypeValue)(targetNodeType.name); var targetNodeTypeName = (0, _utils.getTargetNodeTypeNameInContext)(initialTargetNodeTypeName, isNested, parentNode, schema); if (!selectedNodeTypeName || !targetNodeTypeName) { return sourceNodes; } var steps = (0, _expValEquals.expValEquals)('platform_editor_nest_table_in_panel', 'isEnabled', true) ? _TRANSFORMATION_MATRIX.TRANSFORMATION_MATRIX_PANEL_C1[selectedNodeTypeName][targetNodeTypeName] : _TRANSFORMATION_MATRIX.TRANSFORMATION_MATRIX[selectedNodeTypeName][targetNodeTypeName]; var context = { // sourceNode is incorrect now - what to do here? fromNode: sourceNode, targetNodeTypeName: targetNodeTypeName, schema: schema, targetAttrs: targetAttrs }; if (!steps || steps.length === 0) { return sourceNodes; } var resultNodes = steps.reduce(function (nodes, step) { return step(nodes, context); }, sourceNodes); return upgradePanelNodesToPanelC1(resultNodes, parentNode, schema); }; var isTransformDisabledBasedOnStepsConfig = exports.isTransformDisabledBasedOnStepsConfig = function isTransformDisabledBasedOnStepsConfig(selectedNodeType, targetNodeType) { var steps = (0, _expValEquals.expValEquals)('platform_editor_nest_table_in_panel', 'isEnabled', true) ? _TRANSFORMATION_MATRIX.TRANSFORMATION_MATRIX_PANEL_C1[selectedNodeType][targetNodeType] : _TRANSFORMATION_MATRIX.TRANSFORMATION_MATRIX[selectedNodeType][targetNodeType]; return !steps || steps.length === 0; };