@huridocs/react-text-selection-handler
Version:
React pdf handler allows to render a PDF and handle text selection and highlights.
27 lines (25 loc) • 657 B
text/typescript
export interface SelectionRectangle {
left: number;
top: number;
width: number;
height: number;
regionId?: string;
}
export interface TextSelection {
selectionRectangles: SelectionRectangle[];
text?: string;
}
export const domRectToSelectionRectangle = (
rectangle: DOMRect,
region: Element
): SelectionRectangle => {
const regionDomRect = region.getBoundingClientRect();
const regionId = region.getAttribute('data-region-selector-id');
return {
top: rectangle.y - regionDomRect.y,
left: rectangle.x - regionDomRect.x,
width: rectangle.width,
height: rectangle.height,
...(regionId ? { regionId } : {}),
};
};