UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

66 lines (63 loc) 2.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isSelectionTableNestedInTable = exports.getPositionAfterTopParentNodeOfType = exports.getParentOfTypeCount = void 0; var _editorTables = require("@atlaskit/editor-tables"); /* * Returns the level of nesting of a given `nodeType` in the current position. * eg. table > table is nested 1 level, table > table > table is nested 2 levels. * * ```typescript * const nestingLevel = getParentOfTypeCount(schema.nodes.table)(position); * ``` */ var getParentOfTypeCount = exports.getParentOfTypeCount = function getParentOfTypeCount(nodeType) { return function ($pos) { var count = 0; // Loop through parent nodes from bottom to top for (var depth = $pos.depth; depth > 0; depth--) { var node = $pos.node(depth); // Count the table nodes if (node.type === nodeType) { count++; } } return count; }; }; /* * Returns the (absolute) position directly after the top parent node of a given `nodeType` in the current position. * * ```typescript * const nestingLevel = getEndTopParentNodeOfType(schema.nodes.table)(position); * ``` */ var getPositionAfterTopParentNodeOfType = exports.getPositionAfterTopParentNodeOfType = function getPositionAfterTopParentNodeOfType(nodeType) { return function ($pos) { // Loop through parent nodes from top to bottom for (var depth = 1; depth <= $pos.depth; depth++) { var node = $pos.node(depth); // Count the table nodes if (node.type === nodeType) { return $pos.after(depth); } } }; }; /* * Returns true if the current selection is inside a table nested within a table. * * ```typescript * const isNestedTable = isSelectionTableNestedInTable(state); * ``` */ var isSelectionTableNestedInTable = exports.isSelectionTableNestedInTable = function isSelectionTableNestedInTable(state) { var table = (0, _editorTables.findTable)(state.selection); if (!table) { return false; } var parent = state.doc.resolve(table.pos).parent; var nodeTypes = state.schema.nodes; return [nodeTypes.tableHeader, nodeTypes.tableCell].includes(parent.type); };