UNPKG

@atlaskit/editor-plugin-block-menu

Version:

BlockMenu plugin for @atlaskit/editor-core

69 lines 3.19 kB
import { expandSelectionToBlockRange } from '@atlaskit/editor-common/selection'; import { Fragment } from '@atlaskit/editor-prosemirror/model'; import { isTransformDisabledBasedOnStepsConfig } from '../editor-commands/transform-node-utils/transform'; import { toNodeTypeValue } from '../editor-commands/transform-node-utils/types'; import { getBlockNodesInRange, getTargetNodeTypeNameInContext } from '../editor-commands/transform-node-utils/utils'; export const canParentContainNodeType = (schema, selectedNodeTypeName, parentNode, nodeTypeName, nodeTypeAttrs) => { const adjustedNodeTypeName = getTargetNodeTypeNameInContext(nodeTypeName, true, parentNode); if (!adjustedNodeTypeName) { return false; } const nodeType = schema.nodes[adjustedNodeTypeName]; let content = null; const nodesThatCantBeNestedInNestedExpand = ['blockCard', 'embedCard', 'table']; if (nodesThatCantBeNestedInNestedExpand.includes(selectedNodeTypeName) && (adjustedNodeTypeName === 'expand' || adjustedNodeTypeName === 'nestedExpand')) { const node = schema.nodes[selectedNodeTypeName]; content = node.createAndFill(); } return parentNode.type.validContent(Fragment.from(nodeType.createAndFill(nodeTypeAttrs, content))); }; const isHeadingToHeadingTransformEnabled = (selectedNode, targetNodeTypeAttrs) => { var _selectedNode$attrs; const selectedLevel = (_selectedNode$attrs = selectedNode.attrs) === null || _selectedNode$attrs === void 0 ? void 0 : _selectedNode$attrs.level; const targetLevel = targetNodeTypeAttrs === null || targetNodeTypeAttrs === void 0 ? void 0 : targetNodeTypeAttrs.level; if (selectedLevel === undefined || targetLevel === undefined) { return false; } return selectedLevel !== targetLevel; }; const isTransformEnabledForNode = (node, targetNodeTypeName, targetNodeTypeAttrs, isNested, parent, schema) => { const selectedNodeTypeName = toNodeTypeValue(node.type.name); if (!selectedNodeTypeName) { return false; } const isDisabledByStepsConfig = isTransformDisabledBasedOnStepsConfig(selectedNodeTypeName, targetNodeTypeName); if (isDisabledByStepsConfig) { return false; } if (selectedNodeTypeName === 'heading' && targetNodeTypeName === 'heading') { return isHeadingToHeadingTransformEnabled(node, targetNodeTypeAttrs); } if (isNested && !canParentContainNodeType(schema, selectedNodeTypeName, parent, targetNodeTypeName, targetNodeTypeAttrs)) { return false; } return true; }; export const isTransformToTargetDisabled = ({ selection, targetNodeTypeName, targetNodeTypeAttrs }) => { const { range } = expandSelectionToBlockRange(selection); if (!range) { return false; } const selectedNodes = getBlockNodesInRange(range); const parent = range.parent; const isNested = range.depth >= 1; const { schema } = selection.$from.doc.type; const supportedTargetNodeTypeName = toNodeTypeValue(targetNodeTypeName); if (!supportedTargetNodeTypeName) { return true; } const isEnabledForAnyNode = selectedNodes.some(node => isTransformEnabledForNode(node, supportedTargetNodeTypeName, targetNodeTypeAttrs, isNested, parent, schema)); return !isEnabledForAnyNode; };