Skip to content

Commit 31643cd

Browse files
authored
Fix: Move createHighlightDialog.isVisible check to DocAnnotator.js (#13)
1 parent d3a351a commit 31643cd

File tree

4 files changed

+87
-25
lines changed

4 files changed

+87
-25
lines changed

src/Annotator.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,6 @@ class Annotator extends EventEmitter {
265265

266266
this.destroyPendingThreads();
267267

268-
if (this.createHighlightDialog.isVisible) {
269-
document.getSelection().removeAllRanges();
270-
this.createHighlightDialog.hide();
271-
}
272-
273268
// No specific mode available for annotation type
274269
if (!(mode in this.modeButtons)) {
275270
return;

src/__tests__/Annotator-test.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -384,22 +384,6 @@ describe('Annotator', () => {
384384
expect(stubs.exitModes).to.not.be.called;
385385
});
386386

387-
it('should hide the highlight dialog and remove selection if it is visible', () => {
388-
const getSelectionStub = sandbox.stub(document, 'getSelection').returns({
389-
removeAllRanges: sandbox.stub()
390-
});
391-
392-
annotator.toggleAnnotationHandler(TYPES.highlight);
393-
expect(annotator.createHighlightDialog.hide).to.not.be.called;
394-
expect(getSelectionStub).to.not.be.called;
395-
396-
annotator.createHighlightDialog.isVisible = true;
397-
398-
annotator.toggleAnnotationHandler(TYPES.highlight);
399-
expect(annotator.createHighlightDialog.hide).to.be.called;
400-
expect(getSelectionStub).to.be.called;
401-
});
402-
403387
it('should turn annotation mode on if it is off', () => {
404388
stubs.annotationMode.returns(false);
405389

src/doc/DocAnnotator.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,29 @@ class DocAnnotator extends Annotator {
421421
});
422422
}
423423

424+
/**
425+
* Toggles annotation modes on and off. When an annotation mode is
426+
* on, annotation threads will be created at that location.
427+
*
428+
* @param {string} mode - Current annotation mode
429+
* @param {HTMLEvent} event - DOM event
430+
* @return {void}
431+
*/
432+
toggleAnnotationHandler(mode, event = {}) {
433+
if (!this.isModeAnnotatable(mode)) {
434+
return;
435+
}
436+
437+
this.destroyPendingThreads();
438+
439+
if (this.createHighlightDialog && this.createHighlightDialog.isVisible) {
440+
document.getSelection().removeAllRanges();
441+
this.createHighlightDialog.hide();
442+
}
443+
444+
super.toggleAnnotationHandler(mode, event);
445+
}
446+
424447
//--------------------------------------------------------------------------
425448
// Protected
426449
//--------------------------------------------------------------------------
@@ -554,7 +577,7 @@ class DocAnnotator extends Annotator {
554577
return null;
555578
}
556579

557-
if (this.createHighlightDialog) {
580+
if (this.createHighlightDialog && this.createHighlightDialog.isVisble) {
558581
this.createHighlightDialog.hide();
559582
}
560583

@@ -838,7 +861,7 @@ class DocAnnotator extends Annotator {
838861
this.highlighter.removeAllHighlights();
839862
}
840863

841-
if (this.createHighlightDialog.isVisible) {
864+
if (this.createHighlightDialog && this.createHighlightDialog.isVisible) {
842865
this.createHighlightDialog.hide();
843866
document.getSelection().removeAllRanges();
844867
}

src/doc/__tests__/DocAnnotator-test.js

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,55 @@ describe('doc/DocAnnotator', () => {
636636
});
637637
});
638638

639+
describe('toggleAnnotationHandler()', () => {
640+
beforeEach(() => {
641+
stubs.destroyStub = sandbox.stub(annotator, 'destroyPendingThreads');
642+
stubs.annotationMode = sandbox.stub(annotator, 'isInAnnotationMode');
643+
stubs.exitModes = sandbox.stub(annotator, 'exitAnnotationModesExcept');
644+
stubs.disable = sandbox.stub(annotator, 'disableAnnotationMode');
645+
stubs.enable = sandbox.stub(annotator, 'enableAnnotationMode');
646+
sandbox.stub(annotator, 'getAnnotateButton');
647+
stubs.isAnnotatable = sandbox.stub(annotator, 'isModeAnnotatable').returns(true);
648+
649+
annotator.modeButtons = {
650+
point: { selector: 'point_btn' },
651+
draw: { selector: 'draw_btn' }
652+
};
653+
654+
annotator.createHighlightDialog = {
655+
isVisible: false,
656+
hide: sandbox.stub(),
657+
destroy: sandbox.stub()
658+
}
659+
});
660+
661+
afterEach(() => {
662+
annotator.modeButtons = {};
663+
});
664+
665+
it('should do nothing if specified annotation type is not annotatable', () => {
666+
stubs.isAnnotatable.returns(false);
667+
annotator.toggleAnnotationHandler('bleh');
668+
expect(stubs.destroyStub).to.not.be.called;
669+
});
670+
671+
it('should hide the highlight dialog and remove selection if it is visible', () => {
672+
const getSelectionStub = sandbox.stub(document, 'getSelection').returns({
673+
removeAllRanges: sandbox.stub()
674+
});
675+
676+
annotator.toggleAnnotationHandler(TYPES.highlight);
677+
expect(annotator.createHighlightDialog.hide).to.not.be.called;
678+
expect(getSelectionStub).to.not.be.called;
679+
680+
annotator.createHighlightDialog.isVisible = true;
681+
682+
annotator.toggleAnnotationHandler(TYPES.highlight);
683+
expect(annotator.createHighlightDialog.hide).to.be.called;
684+
expect(getSelectionStub).to.be.called;
685+
});
686+
});
687+
639688
describe('setupAnnotations()', () => {
640689
const setupFunc = Annotator.prototype.setupAnnotations;
641690

@@ -1079,7 +1128,7 @@ describe('doc/DocAnnotator', () => {
10791128
expect(annotator.highlighter.removeAllHighlights).to.be.called;
10801129
});
10811130

1082-
it('should hide the highlight dialog and clear selection if it is visible', () => {
1131+
it('should not hide the highlight dialog and clear selection if the CreateHighlightDialog is not visible', () => {
10831132
annotator.createHighlightDialog = {
10841133
isVisible: false,
10851134
hide: sandbox.stub(),
@@ -1094,8 +1143,19 @@ describe('doc/DocAnnotator', () => {
10941143
annotator.highlightMouseupHandler({ x: 0, y: 0 });
10951144
expect(annotator.createHighlightDialog.hide).to.not.be.called;
10961145
expect(getSelectionStub).to.not.be.called;
1146+
});
10971147

1098-
annotator.createHighlightDialog.isVisible = true;
1148+
it('should hide the highlight dialog and clear selection if the CreateHighlightDialog is visible', () => {
1149+
annotator.createHighlightDialog = {
1150+
isVisible: true,
1151+
hide: sandbox.stub(),
1152+
removeListener: sandbox.stub(),
1153+
destroy: sandbox.stub()
1154+
}
1155+
1156+
const getSelectionStub = sandbox.stub(document, 'getSelection').returns({
1157+
removeAllRanges: sandbox.stub()
1158+
});
10991159

11001160
annotator.highlightMouseupHandler({ x: 0, y: 0 });
11011161
expect(annotator.createHighlightDialog.hide).to.be.called;

0 commit comments

Comments
 (0)