Skip to content

Commit

Permalink
Merge branch 'recogito#130-spatial-tree-events-emission' into staging…
Browse files Browse the repository at this point in the history
…-merge-candidate
  • Loading branch information
oleksandr-danylchenko committed Dec 16, 2024
2 parents 21babc1 + 720d3b7 commit 164abff
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 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.

3 changes: 2 additions & 1 deletion packages/text-annotator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"colord": "^2.9.3",
"dequal": "^2.0.3",
"hotkeys-js": "^3.13.9",
"nanoevents": "^9.0.0",
"rbush": "^4.0.1",
"uuid": "^11.0.3"
}
}
}
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 @@ -25,6 +29,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 @@ -130,6 +130,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 @@ -158,6 +159,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, reviveSelector, mergeClientRects } from '../utils';
import type { AnnotationRects } from './TextAnnotationStore';
Expand All @@ -24,12 +26,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 164abff

Please sign in to comment.