UNPKG

@atlaskit/editor-plugin-block-menu

Version:

BlockMenu plugin for @atlaskit/editor-core

54 lines 2.38 kB
import { getTargetNodeTypeNameInContext } from '../transform-node-utils/utils'; import { TRANSFORMATION_MATRIX } from './TRANSFORMATION_MATRIX'; import { getNodeName, toNodeTypeValue } from './types'; /** * 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); if (!selectedNodeTypeName || !targetNodeTypeName) { return sourceNodes; } var steps = 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; } return steps.reduce(function (nodes, step) { return step(nodes, context); }, sourceNodes); }; export var isTransformDisabledBasedOnStepsConfig = function isTransformDisabledBasedOnStepsConfig(selectedNodeType, targetNodeType) { var steps = TRANSFORMATION_MATRIX[selectedNodeType][targetNodeType]; return !steps || steps.length === 0; };