Skip to content

Commit 1480fb7

Browse files
committed
highlight the selected annotation
This is related to #29.
1 parent 1d195b7 commit 1480fb7

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/seed-text-view.ts

+34-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import { storeConsumerMixin } from './store-consumer-mixin';
77
import { windowMixin, windowStyles } from './window-mixin';
88

99
import { seedTextViewContext } from "./seed-context";
10-
import { addAppListener } from "./redux/seed-store";
10+
import { addAppListener, SeedState } from "./redux/seed-store";
1111
import { initText, setText, TextState } from "./redux/textsSlice";
1212
import { initTextView, setText as setTextViewText, scrolledTo, fetchAnnotationsPerSegment } from "./redux/textViewsSlice";
13+
import { annotationSelected } from './redux/annotationsSlice';
1314
import { selectAnnotationsAtSegmentThunk, passByAnnotationsAtSegmentThunk } from "./redux/selectAnnotations";
1415
import { CSSDefinition } from './redux/cssTypes';
1516
import { scrolled, syncOthers } from './redux/synopsisSlice';
@@ -18,8 +19,6 @@ import { setScrollTarget } from './redux/synopsisMiddleware';
1819

1920
import log from "./logging";
2021

21-
import { SeedState } from './redux/seed-store';
22-
2322

2423

2524
// define the web component
@@ -47,6 +46,8 @@ export class SeedTextView extends windowMixin(storeConsumerMixin(LitElement)) {
4746
@state()
4847
doc: string | undefined;
4948

49+
protected annotationSelected: string | null = null;
50+
5051
protected docLoaded: boolean = false;
5152

5253
protected msgQueue: Array<any> = [];
@@ -117,6 +118,19 @@ export class SeedTextView extends windowMixin(storeConsumerMixin(LitElement)) {
117118
}
118119
}));
119120

121+
this.store?.dispatch(addAppListener({
122+
actionCreator: annotationSelected,
123+
effect: (_action, listenerApi): void => {
124+
// reset highlightning of priviously selected annotation
125+
if (this.annotationSelected) {
126+
this.colorizeAnnotation(listenerApi.getState(), this.annotationSelected, { "border": "none"});
127+
}
128+
// highlight currently selected annotation
129+
this.annotationSelected = listenerApi.getState().annotations.annotationSelected ?? "unknown";
130+
this.colorizeAnnotation(listenerApi.getState(), this.annotationSelected, { "border": "1px solid red"});
131+
}
132+
}));
133+
120134
// this.storeUnsubscribeListeners.push(subsc);
121135
}
122136

@@ -147,7 +161,7 @@ export class SeedTextView extends windowMixin(storeConsumerMixin(LitElement)) {
147161
}
148162

149163
/*
150-
* A callback for scrolling the text to `scrollTarget` position.
164+
* A callback for scrolling the text to `scrollTarget` position.
151165
*/
152166
scrollTextTo(scrollTarget: string): void {
153167
if (this.iframe) {
@@ -308,6 +322,22 @@ export class SeedTextView extends windowMixin(storeConsumerMixin(LitElement)) {
308322
}
309323
}
310324

325+
/*
326+
* Get the text segements included in an annotation given by
327+
* `annotId` and colorize them with the given CSS.
328+
*/
329+
protected colorizeAnnotation(state: SeedState, annotId: string, css: CSSDefinition): void {
330+
if (state.textViews[this.id].segmentsPerAnnotation.hasOwnProperty(annotId)) {
331+
log.debug("colorizing selected annotation in " + this.id + ": " + annotId);
332+
const segments: Array<string> = state.textViews[this.id].segmentsPerAnnotation[annotId];
333+
const cssPerSegments: { [segmentId: string]: CSSDefinition } = {};
334+
segments.forEach(seg => {
335+
cssPerSegments[seg] = css;
336+
});
337+
this.colorizeText(cssPerSegments);
338+
}
339+
}
340+
311341
static styles: CSSResultGroup = [
312342
windowStyles,
313343
css`

0 commit comments

Comments
 (0)