Skip to content

Commit

Permalink
Fix: Exiting annotations modes properly re-binds handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
pramodsum committed Nov 2, 2018
1 parent d339a26 commit 2c0b162
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 24 deletions.
7 changes: 7 additions & 0 deletions src/controllers/AnnotationModeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,19 @@ class AnnotationModeController extends EventEmitter {
this.emit(CONTROLLER_EVENT.toggleMode);
}

/**
* @return {void}
*/
resetMode() {}

/**
* Disables the specified annotation mode
*
* @return {void}
*/
exit(): void {
this.resetMode();

if (this.currentThread) {
this.currentThread.unmountPopover();
}
Expand Down
3 changes: 1 addition & 2 deletions src/controllers/DrawingModeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class DrawingModeController extends AnnotationModeController {
}

/** @inheritdoc */
exit() {
resetMode() {
if (this.currentThread) {
this.currentThread.clearBoundary();
}
Expand All @@ -272,7 +272,6 @@ class DrawingModeController extends AnnotationModeController {
pageElements.forEach((pageEl) => clearCanvas(pageEl, CLASS_ANNOTATION_LAYER_DRAW_IN_PROGRESS));

this.annotatedElement.classList.remove(CLASS_ANNOTATION_DRAW_MODE);
super.exit();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/HighlightModeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class HighlightModeController extends AnnotationModeController {
}

/** @inheritdoc */
exit(): void {
resetMode(): void {
this.destroyPendingThreads();
window.getSelection().removeAllRanges();
this.unbindListeners(); // Disable mode
Expand Down
3 changes: 1 addition & 2 deletions src/controllers/PointModeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class PointModeController extends AnnotationModeController {
}

/** @inheritdoc */
exit(): void {
resetMode(): void {
if (this.buttonEl) {
this.buttonEl.classList.remove(CLASS_ACTIVE);
}
Expand All @@ -70,7 +70,6 @@ class PointModeController extends AnnotationModeController {
}

this.pendingThreadID = null;
super.exit();
}

/** @inheritdoc */
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/__tests__/HighlightModeController-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('controllers/HighlightModeController', () => {
});
});

describe('exit()', () => {
describe('resetMode()', () => {
it('should exit annotation mode', () => {
controller.destroyPendingThreads = jest.fn();
controller.unbindListeners = jest.fn();
Expand All @@ -75,7 +75,7 @@ describe('controllers/HighlightModeController', () => {
controller.annotatedElement = document.createElement('div');
controller.annotatedElement.classList.add(CLASS_ANNOTATION_MODE);

controller.exit();
controller.resetMode();
expect(controller.destroyPendingThreads).toBeCalled();
expect(controller.emit).toBeCalledWith(CONTROLLER_EVENT.bindDOMListeners);
expect(controller.unbindListeners).toBeCalled();
Expand Down
18 changes: 4 additions & 14 deletions src/controllers/__tests__/PointModeController-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,8 @@ describe('controllers/PointModeController', () => {
});
});

describe('exit()', () => {
describe('resetMode()', () => {
beforeEach(() => {
controller.destroyPendingThreads = jest.fn();
controller.unbindListeners = jest.fn();

// Set up annotation mode
controller.annotatedElement = document.createElement('div');
controller.annotatedElement.classList.add(CLASS_ANNOTATION_MODE);
Expand All @@ -104,19 +101,12 @@ describe('controllers/PointModeController', () => {
controller.buttonEl.classList.add(CLASS_ACTIVE);
});

it('should exit annotation mode', () => {
controller.exit();
expect(controller.destroyPendingThreads).toBeCalled();
expect(controller.emit).toBeCalledWith(CONTROLLER_EVENT.exit, expect.any(Object));
expect(controller.unbindListeners).toBeCalled();
expect(controller.hadPendingThreads).toBeFalsy();
});

it('should deactive mode button if available', () => {
it('should reset annotation mode', () => {
controller.buttonEl = document.createElement('button');
controller.buttonEl.classList.add(CLASS_ACTIVE);
controller.exit();
controller.resetMode();
expect(controller.buttonEl.classList).not.toContain(CLASS_ACTIVE);
expect(controller.hadPendingThreads).toBeFalsy();
});
});

Expand Down
3 changes: 0 additions & 3 deletions src/doc/DocAnnotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,6 @@ class DocAnnotator extends Annotator {
const location = this.getLocationFromEvent(this.lastHighlightEvent, highlightType);
const controller = this.modeControllers[highlightType];

this.highlighter.removeAllHighlights();
this.resetHighlightSelection(this.lastHighlightEvent);

if (!location || !controller) {
return null;
}
Expand Down

0 comments on commit 2c0b162

Please sign in to comment.