@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
45 lines (42 loc) • 1.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.selectTableNodeAtPos = exports.selectNodeAtPos = exports.getNodeSelectionForPos = void 0;
var _state = require("@atlaskit/editor-prosemirror/state");
var _utils = require("@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`.
*/
var getNodeSelectionForPos = exports.getNodeSelectionForPos = function getNodeSelectionForPos(doc, start) {
var node = doc.nodeAt(start);
if (!node) {
return false;
}
if (node.type.name === 'mediaGroup' && node.childCount === 1) {
return new _state.NodeSelection(doc.resolve(start + 1));
}
return new _state.NodeSelection(doc.resolve(start));
};
/** Applies a CellSelection to `tr` for the table node at `tableNodePos`. */
var selectTableNodeAtPos = exports.selectTableNodeAtPos = function selectTableNodeAtPos(tr, tableNodePos) {
(0, _utils.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.
*/
var selectNodeAtPos = exports.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;
};