Skip to content

Commit 111cbd1

Browse files
committed
Annotation filtering
1 parent 0ab1452 commit 111cbd1

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

packages/text-annotator/src/TextAnnotator.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ export const createTextAnnotator = <E extends unknown = TextAnnotation>(
5151

5252
const getUser = () => currentUser;
5353

54-
const setFilter = (filter: Filter) => {
55-
// TODO
56-
}
54+
const setFilter = (filter?: Filter) =>
55+
highlightLayer.setFilter(filter);
5756

5857
const setStyle = (drawingStyle: DrawingStyle | ((annotation: TextAnnotation) => DrawingStyle) | undefined) =>
5958
highlightLayer.setDrawingStyle(drawingStyle);

packages/text-annotator/src/highlight/highlightLayer.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { DrawingStyle, ViewportState } from '@annotorious/core';
1+
import type { DrawingStyle, Filter, ViewportState } from '@annotorious/core';
22
import type { TextAnnotation } from '../model';
33
import type { TextAnnotatorState } from '../state';
44
import { defaultPainter, type HighlightPainter } from './HighlightPainter';
@@ -52,6 +52,8 @@ export const createHighlightLayer = (
5252

5353
let currentStyle: DrawingStyle | ((annotation: TextAnnotation, selected?: boolean) => DrawingStyle) | undefined;
5454

55+
let currentFilter: Filter | undefined;
56+
5557
let currentPainter: HighlightPainter = defaultPainter;
5658

5759
const onDraw = trackViewport(viewport);
@@ -69,11 +71,14 @@ export const createHighlightLayer = (
6971

7072
container.addEventListener('pointermove', (event: PointerEvent) => {
7173
const {x, y} = container.getBoundingClientRect();
72-
const hovered = store.getAt(event.clientX - x, event.clientY - y);
73-
if (hovered) {
74-
if (hover.current !== hovered.id) {
74+
75+
const hit = store.getAt(event.clientX - x, event.clientY - y);
76+
const isVisibleHit = hit && (!currentFilter || currentFilter(hit));
77+
78+
if (isVisibleHit) {
79+
if (hover.current !== hit.id) {
7580
container.classList.add('hovered');
76-
hover.set(hovered.id);
81+
hover.set(hit.id);
7782
}
7883
} else {
7984
if (hover.current) {
@@ -123,8 +128,10 @@ export const createHighlightLayer = (
123128
const redraw = () => requestAnimationFrame(() => {
124129
const { top, left, minX, minY, maxX, maxY } = getViewport();
125130

126-
const annotationsInView = store.getIntersectingRects(minX, minY, maxX, maxY);
127-
131+
const annotationsInView = currentFilter
132+
? store.getIntersectingRects(minX, minY, maxX, maxY).filter(({ annotation }) => currentFilter(annotation))
133+
: store.getIntersectingRects(minX, minY, maxX, maxY);
134+
128135
const { width, height } = fgCanvas;
129136

130137
// Get current selection
@@ -161,9 +168,15 @@ export const createHighlightLayer = (
161168
redraw();
162169
}
163170

171+
const setFilter = (filter?: Filter) => {
172+
currentFilter = filter;
173+
redraw();
174+
}
175+
164176
return {
165177
redraw,
166178
setDrawingStyle,
179+
setFilter,
167180
setPainter: (painter: HighlightPainter) => currentPainter = painter
168181
}
169182

packages/text-annotator/test/index.html

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
<body>
4545
<div id="buttons">
46-
<button id="jump">Jump to annotation</button>
46+
<button id="filter">Toggle Filter</button>
4747
</div>
4848

4949
<div id="content">
@@ -140,13 +140,8 @@ <h1>Homer: The Odyssey</h1>
140140
window.onload = async function() {
141141
var r = createTextAnnotator(document.getElementById('content'));
142142

143-
let annotations;
143+
r.loadAnnotations('annotations.json');
144144

145-
r.loadAnnotations('annotations.json').then(a => {
146-
annotations = a;
147-
});
148-
149-
/*
150145
r.on('createAnnotation', annotation => {
151146
console.log('createAnnotation', annotation);
152147
});
@@ -166,16 +161,22 @@ <h1>Homer: The Odyssey</h1>
166161
r.on('viewportIntersect', (annotations) => {
167162
console.log('viewport', annotations);
168163
});
169-
*/
170164

171-
let idx = 0;
165+
var filterActive = false;
172166

173-
document.getElementById('jump').addEventListener('click', () => {
174-
const next = annotations[idx];
167+
document.getElementById('filter').addEventListener('click', () => {
168+
if (filterActive) {
169+
r.setFilter();
175170

176-
idx = (idx + 1) % annotations.length;
171+
filterActive = false;
172+
} else {
173+
r.setFilter(annotation => {
174+
// Dummy filter - just filter randomly
175+
return Math.random() < 0.5;
176+
});
177177

178-
r.scrollIntoView(next);
178+
filterActive = true;
179+
}
179180
});
180181
}
181182
</script>

0 commit comments

Comments
 (0)