@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
69 lines (66 loc) • 2.73 kB
JavaScript
import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments';
var IGNORE_NODES = ['tableRow', 'listItem', 'caption', 'media'];
var blockLeafNodes = ['blockCard', 'rule', 'extension'];
var blockLeafNodeNext = ['blockCard', 'rule', 'extension', 'syncBlock'];
var DISABLE_CHILD_DROP_TARGET = ['orderedList', 'bulletList'];
/**
* This function returns the surrounding nodes of a given resolved position in the editor.
* It provides the position, node, parent, before and after nodes, index, and depth.
* @param state current editor state
* @param $pos a resolved position in the editor state.
* @returns {SurroundingNodes} An object containing the surrounding nodes information.
* @example
* const surroundingNodes = findSurroundingNodes(state, $pos);
*/
var _findSurroundingNodes = function findSurroundingNodes(state, $pos, nodeType) {
var depth = $pos.depth;
var blockLeafNodeList = editorExperiment('platform_synced_block_patch_6', true, {
exposure: true
}) ? blockLeafNodeNext : blockLeafNodes;
// special cases like hr rule here
if (blockLeafNodeList.includes(nodeType || '') || $pos.pos === 0) {
var _parent = $pos.node(depth);
var _node = $pos.nodeAfter;
var _index = $pos.index();
var _before = _index > 0 ? _parent.child(_index - 1) : null;
var _after = _index < _parent.childCount - 1 ? _parent.child(_index + 1) : null;
return {
pos: $pos.pos,
node: _node,
parent: _parent,
before: _before,
after: _after,
index: _index,
depth: depth
};
}
var isRootNode = depth === 1;
var node = $pos.node(depth);
// go through the path to find the first node that is not allow child drop target
// From top to bottom, we check the node types at each depth
for (var i = 1; i < depth; i++) {
var _nodeType = $pos.node(i).type.name;
if (DISABLE_CHILD_DROP_TARGET.includes(_nodeType)) {
return _findSurroundingNodes(state, state.doc.resolve($pos.before(i + 1)), _nodeType);
}
}
if (IGNORE_NODES.includes(node.type.name) && !isRootNode) {
// If the node is an ignored node, we return the surrounding nodes of its parent
return _findSurroundingNodes(state, state.doc.resolve($pos.before(depth - 1)));
}
var pos = depth > 0 ? $pos.before(depth) : 0;
var parent = isRootNode ? state.doc : $pos.node(depth - 1);
var index = $pos.index(depth - 1);
var before = index > 0 ? parent.child(index - 1) : null;
var after = index < parent.childCount - 1 ? parent.child(index + 1) : null;
return {
pos: pos,
node: node,
parent: parent,
before: before,
after: after,
index: index,
depth: depth
};
};
export { _findSurroundingNodes as findSurroundingNodes };