UNPKG

@atlaskit/editor-plugin-block-menu

Version:

BlockMenu plugin for @atlaskit/editor-core

66 lines (58 loc) 2.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isNestedNode = void 0; var _state = require("@atlaskit/editor-prosemirror/state"); var _editorTables = require("@atlaskit/editor-tables"); /** * Determines if a node is nested (not at top-level) based on its depth and context. * * Simple rules: * - Depth 0-1: Always top-level (not nested) * - Depth 2: Top-level for blockquotes and task lists * - Depth 3: Top-level for list items only * - Depth 4+: Always nested * * @param selection - The current ProseMirror selection * @returns true if nested, false if top-level */ var isNestedNode = exports.isNestedNode = function isNestedNode(selection, menuTriggerBy) { if (!selection) { return false; } var $from = selection.$from; var depth = $from.depth; // When a file is in card view, selection.$from has a depth of 1, but it is not a nested node, should return false if ($from.depth === 1 && selection instanceof _state.NodeSelection && selection.node.type.name === 'media' && selection.node.attrs.type === 'file') { return false; } if ($from.depth > 0 && selection instanceof _state.NodeSelection) { return true; } // Depth 0-1: Always top-level if (depth <= 1) { return false; } // Depth 4+: Always nested if (depth > 4) { return true; } // Special case for table selection if (selection instanceof _editorTables.CellSelection) { return depth > 3; } // Check parent node type for depth 2-3 var parentNode = $from.node(depth - 1); if (!parentNode) { return true; } var parentType = parentNode.type.name; var isTriggeredByBlockquote = menuTriggerBy.includes('blockquote'); // Special cases where content is still top-level if (parentType === 'listItem' && depth === 3 || parentType === 'blockquote' && depth === 2 && isTriggeredByBlockquote || parentType === 'taskList' && depth === 2 || parentType === 'listItem' && depth === 4 && isTriggeredByBlockquote) { return false; } // Everything else at depth 2-3 is nested return true; };