Skip to content

Commit 945c09d

Browse files
committed
Refactoring + removed unused API args
1 parent c87ed10 commit 945c09d

File tree

5 files changed

+18
-37
lines changed

5 files changed

+18
-37
lines changed

packages/text-annotator-react/src/TextAnnotationPopup/TextAnnotationPopup.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { ReactNode, useEffect, useMemo, useRef, useState } from 'react';
22
import { useAnnotator, useSelection } from '@annotorious/react';
33
import {
44
NOT_ANNOTATABLE_CLASS,
5-
denormalizeRectWithOffset,
65
toDomRectList,
76
type TextAnnotation,
87
type TextAnnotator,
@@ -50,6 +49,12 @@ export interface TextAnnotationPopupContentProps {
5049

5150
}
5251

52+
const toViewportBounds = (annotationBounds: DOMRect, container: HTMLElement): DOMRect => {
53+
const { left, top, right, bottom } = annotationBounds;
54+
const containerBounds = container.getBoundingClientRect();
55+
return new DOMRect(left + containerBounds.left, top + containerBounds.top, right - left, bottom - top);
56+
}
57+
5358
export const TextAnnotationPopup = (props: TextAnnotationPopupProps) => {
5459

5560
const r = useAnnotator<TextAnnotator>();
@@ -102,17 +107,16 @@ export const TextAnnotationPopup = (props: TextAnnotationPopupProps) => {
102107
if (isOpen && annotation?.id) {
103108
refs.setPositionReference({
104109
getBoundingClientRect: () => {
110+
// Annotation bounds are relative to the document element
105111
const bounds = r.state.store.getAnnotationBounds(annotation.id);
106112
return bounds
107-
? denormalizeRectWithOffset(bounds, r.element.getBoundingClientRect())
113+
? toViewportBounds(bounds, r.element)
108114
: new DOMRect();
109115
},
110116
getClientRects: () => {
111117
const rects = r.state.store.getAnnotationRects(annotation.id);
112-
const denormalizedRects = rects.map((rect) =>
113-
denormalizeRectWithOffset(rect, r.element.getBoundingClientRect())
114-
);
115-
return toDomRectList(denormalizedRects);
118+
const viewportRects = rects.map(rect => toViewportBounds(rect, r.element));
119+
return toDomRectList(viewportRects);
116120
}
117121
});
118122
} else {

packages/text-annotator/src/state/TextAnnotationStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface TextAnnotationStore<T extends TextAnnotation = TextAnnotation>
1313

1414
getAnnotationRects(id: string): DOMRect[];
1515

16-
getAnnotationBounds(id: string, hintX?: number, hintY?: number, buffer?: number): DOMRect | undefined;
16+
getAnnotationBounds(id: string): DOMRect | undefined;
1717

1818
getAnnotationRects(id: string): DOMRect[];
1919

packages/text-annotator/src/state/TextAnnotatorState.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,9 @@ export const createTextAnnotatorState = <I extends TextAnnotation = TextAnnotati
114114
return all ? filtered : filtered[0];
115115
}
116116

117-
const getAnnotationBounds = (id: string, x?: number, y?: number, buffer = 5): DOMRect | undefined => {
117+
const getAnnotationBounds = (id: string): DOMRect | undefined => {
118118
const rects = tree.getAnnotationRects(id);
119119
if (rects.length === 0) return;
120-
121-
if (x && y) {
122-
const match = rects.find(({ top, right, bottom, left }) =>
123-
x >= left - buffer && x <= right + buffer && y >= top - buffer && y <= bottom + buffer);
124-
125-
// Preferred bounds: the rectangle
126-
if (match) return match;
127-
}
128-
129120
return tree.getAnnotationBounds(id);
130121
}
131122

packages/text-annotator/src/state/spatialTree.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import RBush from 'rbush';
22
import type { Store } from '@annotorious/core';
33
import type { TextAnnotation, TextAnnotationTarget } from '../model';
4-
import {
5-
isRevived,
6-
reviveSelector,
7-
mergeClientRects,
8-
normalizeRectWithOffset
9-
} from '../utils';
4+
import { isRevived, reviveSelector, mergeClientRects } from '../utils';
105
import type { AnnotationRects } from './TextAnnotationStore';
116

127
interface IndexedHighlightRect {
@@ -42,11 +37,11 @@ export const createSpatialTree = <T extends TextAnnotation>(store: Store<T>, con
4237
return Array.from(revivedRange.getClientRects());
4338
});
4439

45-
/**
46-
* Offset the merged client rects so that coords
47-
* are relative to the parent container
48-
*/
49-
const merged = mergeClientRects(rects).map(rect => normalizeRectWithOffset(rect, offset));
40+
const merged = mergeClientRects(rects)
41+
// Offset the merged client rects so that coords
42+
// are relative to the parent container
43+
.map(({ left, top, right, bottom }) =>
44+
new DOMRect(left - offset.left, top - offset.top, right - left, bottom - top));
5045

5146
return merged.map(rect => {
5247
const { x, y, width, height } = rect;

packages/text-annotator/src/utils/normalizeRects.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)