Skip to content

Commit

Permalink
Renamed to the toParentBounds & toViewportBounds
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-danylchenko committed Nov 21, 2024
1 parent 8ad4fae commit 98532a3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/text-annotator/src/state/spatialTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
isRevived,
reviveSelector,
mergeClientRects,
normalizeRectWithOffset
toParentBounds
} from '../utils';
import type { AnnotationRects } from './TextAnnotationStore';

Expand Down Expand Up @@ -46,7 +46,7 @@ export const createSpatialTree = <T extends TextAnnotation>(store: Store<T>, 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;
Expand Down
2 changes: 1 addition & 1 deletion packages/text-annotator/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export * from './reviveSelector';
export * from './reviveTarget';
export * from './splitAnnotatableRanges';
export * from './trimRangeToContainer';

export * from './rectsToBounds';
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 98532a3

Please sign in to comment.