@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
40 lines (37 loc) • 1.41 kB
JavaScript
import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
import { selectTableClosestToPos } from '@atlaskit/editor-tables/utils';
/**
* Returns a NodeSelection for the node at `start`.
* Matches the `platform_editor_block_menu=true` path in block-controls:
* mediaGroup with a single child → select the child; all others → select the node.
* Returns false when no node exists at `start`.
*/
export var getNodeSelectionForPos = function getNodeSelectionForPos(doc, start) {
var node = doc.nodeAt(start);
if (!node) {
return false;
}
if (node.type.name === 'mediaGroup' && node.childCount === 1) {
return new NodeSelection(doc.resolve(start + 1));
}
return new NodeSelection(doc.resolve(start));
};
/** Applies a CellSelection to `tr` for the table node at `tableNodePos`. */
export var selectTableNodeAtPos = function selectTableNodeAtPos(tr, tableNodePos) {
selectTableClosestToPos(tr, tr.doc.resolve(tableNodePos + 1));
return tr;
};
/**
* Selects the node at `nodePos` without any plugin-API dependency.
* Tables use CellSelection; all other nodes use NodeSelection.
*/
export var selectNodeAtPos = function selectNodeAtPos(tr, nodePos, nodeType) {
if (nodeType === 'table') {
return selectTableNodeAtPos(tr, nodePos);
}
var selection = getNodeSelectionForPos(tr.doc, nodePos);
if (selection) {
tr.setSelection(selection);
}
return tr;
};