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

feat(scroll): emit 'annotations_initialized' when annotations first initializes. #515

Merged
merged 8 commits into from
Jun 16, 2020
5 changes: 3 additions & 2 deletions src/@types/events.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
enum Event {
ACTIVE_CHANGE = 'annotations_active_change',
ACTIVE_SET = 'annotations_active_set',
ANNOTATION_CREATE = 'annotations_create',
ANNOTATION_FETCH_ERROR = 'annotations_fetch_error',
ANNOTATIONS_INITIALIZED = 'annotations_initialized',
ANNOTATION_REMOVE = 'annotations_remove',
ACTIVE_CHANGE = 'annotations_active_change',
ACTIVE_SET = 'annotations_active_set',
VISIBLE_SET = 'annotations_visible_set',
}

Expand Down
11 changes: 11 additions & 0 deletions src/common/BaseAnnotator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import EventEmitter from './EventEmitter';
import i18n from '../utils/i18n';
import messages from '../messages';
import { Event, IntlOptions, LegacyEvent, Permissions } from '../@types';
import { getAnnotations } from '../store';
import './BaseAnnotator.scss';

export type Container = string | HTMLElement;
Expand Down Expand Up @@ -37,6 +38,8 @@ export type Options = {
export default class BaseAnnotator extends EventEmitter {
container: Container;

initialized = false;
mickr marked this conversation as resolved.
Show resolved Hide resolved

intl: IntlShape;

rootEl?: HTMLElement | null;
Expand Down Expand Up @@ -151,4 +154,12 @@ export default class BaseAnnotator extends EventEmitter {
this.store.dispatch<any>(store.fetchAnnotationsAction()); // eslint-disable-line @typescript-eslint/no-explicit-any
this.store.dispatch<any>(store.fetchCollaboratorsAction()); // eslint-disable-line @typescript-eslint/no-explicit-any
}

protected isInitialized(): void {
mickr marked this conversation as resolved.
Show resolved Hide resolved
if (!this.initialized) {
this.initialized = true;

this.emit(Event.ANNOTATIONS_INITIALIZED, { annotations: getAnnotations(this.store.getState()) });
}
}
}
4 changes: 3 additions & 1 deletion src/document/DocumentAnnotator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import BaseAnnotator from '../common/BaseAnnotator';
import BaseManager from '../common/BaseManager';
import { CLASS_ANNOTATIONS_LOADED } from '../constants';
import { getAnnotation } from '../store/annotations';
import { centerRegion, isRegion, RegionManager } from '../region';
import { getAnnotation } from '../store/annotations';
import './DocumentAnnotator.scss';

export const SCROLL_THRESHOLD = 1000; // pixels
Expand Down Expand Up @@ -63,6 +63,8 @@ export default class DocumentAnnotator extends BaseAnnotator {
this.annotatedEl.classList.add(CLASS_ANNOTATIONS_LOADED);

this.render();

this.isInitialized();
}

render(): void {
Expand Down
28 changes: 21 additions & 7 deletions src/document/__tests__/DocumentAnnotator-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ describe('DocumentAnnotator', () => {
const getPage = (pageNumber = 1): HTMLElement => {
return container.querySelector(`[data-page-number="${pageNumber}"]`) as HTMLElement;
};

const payload = {
entries: regions as Annotation[],
limit: 1000,
next_marker: null,
previous_marker: null,
};

let annotator = getAnnotator();

beforeEach(() => {
Expand Down Expand Up @@ -135,6 +143,19 @@ describe('DocumentAnnotator', () => {
expect(annotator.annotatedEl).toBe(container.querySelector('.bp-doc'));
expect(annotator.annotatedEl!.className).toContain(CLASS_ANNOTATIONS_LOADED); // eslint-disable-line @typescript-eslint/no-non-null-assertion
});

test('should emit annotations_initialized event once', () => {
mickr marked this conversation as resolved.
Show resolved Hide resolved
annotator.emit = jest.fn();
annotator.store.dispatch(fetchAnnotationsAction.fulfilled(payload, 'test', undefined));

annotator.init(1);
annotator.init(1);
annotator.init(1);

expect(annotator.initialized).toEqual(true);
expect(annotator.emit).toBeCalledWith('annotations_initialized', { annotations: regions });
expect(annotator.emit).toBeCalledTimes(1);
});
});

describe('render()', () => {
Expand Down Expand Up @@ -168,13 +189,6 @@ describe('DocumentAnnotator', () => {

describe('scrollToAnnotation()', () => {
beforeEach(() => {
const payload = {
entries: regions as Annotation[],
limit: 1000,
next_marker: null,
previous_marker: null,
};

annotator.scrollToLocation = jest.fn();
annotator.store.dispatch(fetchAnnotationsAction.fulfilled(payload, 'test', undefined));
});
Expand Down