Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 1x 20x 10x 10x 6x 14x 7x 7x 3x 11x 5x 5x 2x 9x | import type { DOMHelper, DOMInsertPoint } from 'roosterjs-content-model-types';
/**
* Get insertion point from coordinate.
* @param doc Parent document object
* @param domHelper The DOM helper of the editor
* @param x The cursor coordinate for the x-axis
* @param y The cursor coordinate for the y-axis
*/
export function getNodePositionFromEvent(
doc: Document,
domHelper: DOMHelper,
x: number,
y: number
): DOMInsertPoint | null {
if ('caretPositionFromPoint' in doc) {
// Firefox, Chrome, Edge, Safari, Opera
const pos = (doc as any).caretPositionFromPoint(x, y);
if (pos && domHelper.isNodeInEditor(pos.offsetNode)) {
return { node: pos.offsetNode, offset: pos.offset };
}
}
if (doc.caretRangeFromPoint) {
// Safari
const range = doc.caretRangeFromPoint(x, y);
if (range && domHelper.isNodeInEditor(range.startContainer)) {
return { node: range.startContainer, offset: range.startOffset };
}
}
if (doc.elementFromPoint) {
// Fallback
const element = doc.elementFromPoint(x, y);
if (element && domHelper.isNodeInEditor(element)) {
return { node: element, offset: 0 };
}
}
return null;
}
|