diff --git a/packages/text-annotator-react/src/TextAnnotationPopup/TextAnnotationPopup.tsx b/packages/text-annotator-react/src/TextAnnotationPopup/TextAnnotationPopup.tsx index 6372624c..f452359a 100644 --- a/packages/text-annotator-react/src/TextAnnotationPopup/TextAnnotationPopup.tsx +++ b/packages/text-annotator-react/src/TextAnnotationPopup/TextAnnotationPopup.tsx @@ -2,7 +2,7 @@ import { ReactNode, useEffect, useMemo, useRef, useState } from 'react'; import { useAnnotator, useSelection } from '@annotorious/react'; import { NOT_ANNOTATABLE_CLASS, - denormalizeRectWithOffset, + toViewportBounds, toDomRectList, type TextAnnotation, type TextAnnotator, @@ -104,13 +104,13 @@ export const TextAnnotationPopup = (props: TextAnnotationPopupProps) => { getBoundingClientRect: () => { const bounds = r.state.store.getAnnotationBounds(annotation.id); return bounds - ? denormalizeRectWithOffset(bounds, r.element.getBoundingClientRect()) + ? toViewportBounds(bounds, r.element.getBoundingClientRect()) : new DOMRect(); }, getClientRects: () => { const rects = r.state.store.getAnnotationRects(annotation.id); const denormalizedRects = rects.map((rect) => - denormalizeRectWithOffset(rect, r.element.getBoundingClientRect()) + toViewportBounds(rect, r.element.getBoundingClientRect()) ); return toDomRectList(denormalizedRects); } diff --git a/packages/text-annotator/src/state/spatialTree.ts b/packages/text-annotator/src/state/spatialTree.ts index 118ba853..d742fb5f 100644 --- a/packages/text-annotator/src/state/spatialTree.ts +++ b/packages/text-annotator/src/state/spatialTree.ts @@ -5,7 +5,7 @@ import { isRevived, reviveSelector, mergeClientRects, - normalizeRectWithOffset + toParentBounds } from '../utils'; import type { AnnotationRects } from './TextAnnotationStore'; @@ -46,7 +46,7 @@ export const createSpatialTree = (store: Store, con * Offset the merged client rects so that coords * are relative to the parent container */ - const merged = mergeClientRects(rects).map(rect => normalizeRectWithOffset(rect, offset)); + const merged = mergeClientRects(rects).map(rect => toParentBounds(rect, offset)); return merged.map(rect => { const { x, y, width, height } = rect; diff --git a/packages/text-annotator/src/utils/index.ts b/packages/text-annotator/src/utils/index.ts index 5d536dd2..599fc737 100644 --- a/packages/text-annotator/src/utils/index.ts +++ b/packages/text-annotator/src/utils/index.ts @@ -14,4 +14,4 @@ export * from './reviveSelector'; export * from './reviveTarget'; export * from './splitAnnotatableRanges'; export * from './trimRangeToContainer'; - +export * from './rectsToBounds'; diff --git a/packages/text-annotator/src/utils/normalizeRects.ts b/packages/text-annotator/src/utils/rectsToBounds.ts similarity index 60% rename from packages/text-annotator/src/utils/normalizeRects.ts rename to packages/text-annotator/src/utils/rectsToBounds.ts index 461f222f..ba9fd14a 100644 --- a/packages/text-annotator/src/utils/normalizeRects.ts +++ b/packages/text-annotator/src/utils/rectsToBounds.ts @@ -1,9 +1,9 @@ -export const normalizeRectWithOffset = (rect: DOMRect, offset: DOMRect): DOMRect => { +export const toParentBounds = (rect: DOMRect, offset: DOMRect): DOMRect => { const { left, top, right, bottom } = rect; return new DOMRect(left - offset.left, top - offset.top, right - left, bottom - top); }; -export const denormalizeRectWithOffset = (rect: DOMRect, offset: DOMRect): DOMRect => { +export const toViewportBounds = (rect: DOMRect, offset: DOMRect): DOMRect => { const { left, top, right, bottom } = rect; return new DOMRect(left + offset.left, top + offset.top, right - left, bottom - top); }