@atlaskit/editor-plugin-block-menu
Version:
BlockMenu plugin for @atlaskit/editor-core
74 lines (73 loc) • 3.75 kB
JavaScript
import { isNodeTypeValidChildOf } from '@atlaskit/editor-common/utils/node-type-utils';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { getTargetNodeTypeNameInContext } from '../transform-node-utils/utils';
import { TRANSFORMATION_MATRIX, TRANSFORMATION_MATRIX_PANEL_C1 } from './TRANSFORMATION_MATRIX';
import { getNodeName, toNodeTypeValue } from './types';
// Upgrade broken-out panel nodes to panel_c1 if the parent node allows it
export var upgradePanelNodesToPanelC1 = function upgradePanelNodesToPanelC1(nodes, parentNode, schema) {
if (!schema.nodes['panel_c1'] || !expValEquals('platform_editor_nest_table_in_panel', 'isEnabled', true)) {
return nodes;
}
return nodes.map(function (node) {
if (node.type.name === 'panel') {
var shouldUsePanelC1 = !parentNode || 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
*/
export var 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 = toNodeTypeValue(getNodeName(sourceNodes));
var initialTargetNodeTypeName = toNodeTypeValue(targetNodeType.name);
var targetNodeTypeName = getTargetNodeTypeNameInContext(initialTargetNodeTypeName, isNested, parentNode, schema);
if (!selectedNodeTypeName || !targetNodeTypeName) {
return sourceNodes;
}
var steps = expValEquals('platform_editor_nest_table_in_panel', 'isEnabled', true) ? TRANSFORMATION_MATRIX_PANEL_C1[selectedNodeTypeName][targetNodeTypeName] : 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);
};
export var isTransformDisabledBasedOnStepsConfig = function isTransformDisabledBasedOnStepsConfig(selectedNodeType, targetNodeType) {
var steps = expValEquals('platform_editor_nest_table_in_panel', 'isEnabled', true) ? TRANSFORMATION_MATRIX_PANEL_C1[selectedNodeType][targetNodeType] : TRANSFORMATION_MATRIX[selectedNodeType][targetNodeType];
return !steps || steps.length === 0;
};