Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverted breaking API change, removal of the denormalizeRectWithOffset #182

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ReactNode, useEffect, useMemo, useRef, useState } from 'react';
import { useAnnotator, useSelection } from '@annotorious/react';
import {
NOT_ANNOTATABLE_CLASS,
toViewportBounds,
toDomRectList,
type TextAnnotation,
type TextAnnotator,
Expand Down Expand Up @@ -49,12 +50,6 @@ export interface TextAnnotationPopupContentProps {

}

const toViewportBounds = (annotationBounds: DOMRect, container: HTMLElement): DOMRect => {
const { left, top, right, bottom } = annotationBounds;
const containerBounds = container.getBoundingClientRect();
return new DOMRect(left + containerBounds.left, top + containerBounds.top, right - left, bottom - top);
}

export const TextAnnotationPopup = (props: TextAnnotationPopupProps) => {

const r = useAnnotator<TextAnnotator>();
Expand Down Expand Up @@ -107,16 +102,17 @@ export const TextAnnotationPopup = (props: TextAnnotationPopupProps) => {
if (isOpen && annotation?.id) {
refs.setPositionReference({
getBoundingClientRect: () => {
// Annotation bounds are relative to the document element
const bounds = r.state.store.getAnnotationBounds(annotation.id);
return bounds
? toViewportBounds(bounds, r.element)
? toViewportBounds(bounds, r.element.getBoundingClientRect())
: new DOMRect();
},
getClientRects: () => {
const rects = r.state.store.getAnnotationRects(annotation.id);
const viewportRects = rects.map(rect => toViewportBounds(rect, r.element));
return toDomRectList(viewportRects);
const denormalizedRects = rects.map((rect) =>
toViewportBounds(rect, r.element.getBoundingClientRect())
);
return toDomRectList(denormalizedRects);
}
});
} else {
Expand Down
3 changes: 1 addition & 2 deletions packages/text-annotator/src/state/TextAnnotatorState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ export const createTextAnnotatorState = <I extends TextAnnotation = TextAnnotati

const getAnnotationBounds = (id: string): DOMRect | undefined => {
const rects = tree.getAnnotationRects(id);
if (rects.length === 0) return;
return tree.getAnnotationBounds(id);
return rects.length > 0 ? tree.getAnnotationBounds(id) : undefined;
}

const getIntersecting = (
Expand Down
17 changes: 11 additions & 6 deletions packages/text-annotator/src/state/spatialTree.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import RBush from 'rbush';
import type { Store } from '@annotorious/core';
import type { TextAnnotation, TextAnnotationTarget } from '../model';
import { isRevived, reviveSelector, mergeClientRects } from '../utils';
import {
isRevived,
reviveSelector,
mergeClientRects,
toParentBounds
} from '../utils';
import type { AnnotationRects } from './TextAnnotationStore';

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

const merged = mergeClientRects(rects)
// Offset the merged client rects so that coords
// are relative to the parent container
.map(({ left, top, right, bottom }) =>
new DOMRect(left - offset.left, top - offset.top, right - left, bottom - top));
/**
* Offset the merged client rects so that coords
* are relative to the parent container
*/
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';
9 changes: 9 additions & 0 deletions packages/text-annotator/src/utils/rectsToBounds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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 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);
}