/**
* @module plugins/keyboard
*/
import type { Nullable } from 'jodit/types';
import { Dom } from 'jodit/core/dom';
/**
* Finds the nearest neighbor that would be in the maximum nesting depth.
* Ie if neighbor `
Text` then return Text node.
*/
export function findMostNestedNeighbor(
node: Node,
right: boolean,
root: HTMLElement,
onlyInlide: boolean = false
): Nullable {
const nextChild = (node: Node): Nullable =>
right ? node.firstChild : node.lastChild;
let next = Dom.findNotEmptyNeighbor(node, !right, root);
if (onlyInlide && Dom.isElement(next) && !Dom.isInlineBlock(next)) {
return null;
}
if (next) {
do {
if (nextChild(next)) {
next = nextChild(next);
} else {
return next;
}
} while (next);
}
return null;
}