UNPKG

@atlaskit/editor-common

Version:

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

61 lines (58 loc) 1.86 kB
import { findTable } from '@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); * ``` */ export var 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); * ``` */ export var 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); * ``` */ export var isSelectionTableNestedInTable = function isSelectionTableNestedInTable(state) { var table = 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); };