UNPKG

@atlaskit/editor-plugin-selection-extension

Version:

editor-plugin-selection-extension plugin for @atlaskit/editor-core

35 lines (34 loc) 1.03 kB
/** * Calculates the bounding box coordinates of a text selection within an editor view. * * @param view - The editor view instance. * @param from - The starting position of the selection. * @param to - The ending position of the selection. * @param offset - Optional offset to adjust the top and bottom coordinates of the bounding box. * @returns An object containing the top, left, bottom, and right coordinates of the bounding box. */ export const getBoundingBoxFromSelection = (view, from, to, offset = { top: 0, bottom: 0 }) => { let top = Infinity, left = Infinity, bottom = -Infinity, right = -Infinity; // initial version for (let pos = from; pos <= to; pos++) { const coords = view.coordsAtPos(pos); top = Math.min(top, coords.top); left = Math.min(left, coords.left); bottom = Math.max(bottom, coords.bottom); right = Math.max(right, coords.right); } top = top - offset.top; bottom = bottom - offset.bottom; return { top, left, bottom, right }; };