Skip to content

Commit

Permalink
Merge branch 'recogito#130-spatial-tree-events-emission' into stagin-…
Browse files Browse the repository at this point in the history
…candidate-2
  • Loading branch information
oleksandr-danylchenko committed Oct 9, 2024
2 parents f0c62e6 + 5a31edd commit f3ab8da
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/text-annotator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"colord": "^2.9.3",
"dequal": "^2.0.3",
"hotkeys-js": "^3.13.7",
"nanoevents": "^9.0.0",
"rbush": "^4.0.1",
"uuid": "^10.0.0"
}
Expand Down
6 changes: 6 additions & 0 deletions packages/text-annotator/src/state/TextAnnotationStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { Unsubscribe } from 'nanoevents';
import type { Filter, Origin, Store } from '@annotorious/core';

import type { TextAnnotation } from '../model';
import type { SpatialTreeEvents } from './spatialTree';


export interface TextAnnotationStore<T extends TextAnnotation = TextAnnotation> extends Omit<Store<T>, 'addAnnotation' | 'bulkAddAnnotation'> {

Expand All @@ -23,6 +27,8 @@ export interface TextAnnotationStore<T extends TextAnnotation = TextAnnotation>

recalculatePositions(): void;

onRecalculatePositions(callback: SpatialTreeEvents['recalculate']): Unsubscribe;

}

export interface AnnotationRects <T extends TextAnnotation = TextAnnotation> {
Expand Down
4 changes: 3 additions & 1 deletion packages/text-annotator/src/state/TextAnnotatorState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
SelectionState,
HoverState,
} from '@annotorious/core';
import { createSpatialTree } from './spatialTree';
import { createSpatialTree, type SpatialTreeEvents } from './spatialTree';
import type { TextAnnotation, TextAnnotationTarget } from '../model';
import type { AnnotationRects, TextAnnotationStore } from './TextAnnotationStore';
import { isRevived, reviveAnnotation, reviveTarget } from '../utils';
Expand Down Expand Up @@ -142,6 +142,7 @@ export const createTextAnnotatorState = <I extends TextAnnotation = TextAnnotati
const getAnnotationRects = (id: string): DOMRect[] => tree.getAnnotationRects(id);

const recalculatePositions = () => tree.recalculate();
const onRecalculatePositions = (callback: SpatialTreeEvents['recalculate']) => tree.on('recalculate', callback);

store.observe(({ changes }) => {
const deleted = (changes.deleted || []).filter(a => isRevived(a.target.selector));
Expand Down Expand Up @@ -170,6 +171,7 @@ export const createTextAnnotatorState = <I extends TextAnnotation = TextAnnotati
getIntersecting,
getAt,
recalculatePositions,
onRecalculatePositions,
updateTarget
},
selection,
Expand Down
19 changes: 17 additions & 2 deletions packages/text-annotator/src/state/spatialTree.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import RBush from 'rbush';
import type { Store } from '@annotorious/core';
import { createNanoEvents, type Unsubscribe } from 'nanoevents';

import type { TextAnnotation, TextAnnotationTarget } from '../model';
import {
isRevived,
Expand Down Expand Up @@ -29,12 +31,20 @@ interface IndexedHighlightRect {

}

export interface SpatialTreeEvents {

recalculate(): void;

}

export const createSpatialTree = <T extends TextAnnotation>(store: Store<T>, container: HTMLElement) => {

const tree = new RBush<IndexedHighlightRect>();

const index = new Map<string, IndexedHighlightRect[]>();

const emitter = createNanoEvents<SpatialTreeEvents>();

// Helper: converts a single text annotation target to a list of hightlight rects
const toItems = (target: TextAnnotationTarget, offset: DOMRect): IndexedHighlightRect[] => {
const rects = target.selector.flatMap(s => {
Expand Down Expand Up @@ -180,8 +190,12 @@ export const createSpatialTree = <T extends TextAnnotation>(store: Store<T>, con

const size = () => tree.all().length;

const recalculate = () =>
const recalculate = () => {
set(store.all().map(a => a.target), true);
emitter.emit('recalculate');
};

const on = <E extends keyof SpatialTreeEvents>(event: E, callback: SpatialTreeEvents[E]): Unsubscribe => emitter.on(event, callback);

return {
all,
Expand All @@ -195,7 +209,8 @@ export const createSpatialTree = <T extends TextAnnotation>(store: Store<T>, con
remove,
set,
size,
update
update,
on
}

}

0 comments on commit f3ab8da

Please sign in to comment.