@atlaskit/editor-plugin-selection-extension
Version:
editor-plugin-selection-extension plugin for @atlaskit/editor-core
36 lines (35 loc) • 1.17 kB
JavaScript
/**
* 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 var getBoundingBoxFromSelection = function getBoundingBoxFromSelection(view, from, to) {
var offset = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {
top: 0,
bottom: 0
};
var top = Infinity,
left = Infinity,
bottom = -Infinity,
right = -Infinity;
// initial version
for (var pos = from; pos <= to; pos++) {
var 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: top,
left: left,
bottom: bottom,
right: right
};
};