Skip to content

Commit

Permalink
Fix: Ensure onSelectionChange isn't triggered while creating highlights
Browse files Browse the repository at this point in the history
  • Loading branch information
pramodsum committed Nov 5, 2018
1 parent d763b6f commit bfc839e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
5 changes: 1 addition & 4 deletions src/doc/CreateHighlightDialog.js
Expand Up @@ -265,13 +265,10 @@ class CreateHighlightDialog extends EventEmitter {
onCommentClick = () => {
this.emit(CREATE_EVENT.comment);
this.renderAnnotationPopover(TYPES.highlight_comment);

// $FlowFixMe
document.getSelection().removeAllRanges();
};

/**
* Cancels adding a comment to the highlgiht annotation by rendering a plain highlight popover
* Cancels adding a comment to the highlight annotation by rendering a plain highlight popover
*
* @return {void}
*/
Expand Down
16 changes: 15 additions & 1 deletion src/doc/DocAnnotator.js
Expand Up @@ -247,6 +247,10 @@ class DocAnnotator extends Annotator {
* @return {void}
*/
renderPage(pageNum: number) {
// $FlowFixMe
document.getSelection().removeAllRanges();
this.highlighter.removeAllHighlights();

// Scale existing canvases on re-render
this.scaleAnnotationCanvases(pageNum);
super.renderPage(pageNum);
Expand Down Expand Up @@ -442,6 +446,7 @@ class DocAnnotator extends Annotator {

// $FlowFixMe
this.createHighlightDialog.unmountPopover();
this.isCreatingHighlight = false;
}

/**
Expand Down Expand Up @@ -532,11 +537,18 @@ class DocAnnotator extends Annotator {
this.selectionEndTimeout = null;
}
const isClickOutsideCreateDialog = this.isCreatingHighlight && util.isInDialog(event);
if (isClickOutsideCreateDialog) {
this.hideCreateDialog(event);
this.highlighter.removeAllHighlights();
return;
}
// Do nothing if in a text area or mobile dialog or mobile create dialog is already open
const pointController = this.modeControllers[TYPES.point];
const isCreatingPoint = !!(pointController && pointController.pendingThreadID);
const isPopoverActive = !!util.findClosestElWithClass(document.activeElement, CLASS_ANNOTATION_POPOVER);
if (isCreatingPoint || isPopoverActive) {
if (this.isCreatingHighlight || isCreatingPoint || isPopoverActive) {
return;
}
Expand All @@ -563,6 +575,7 @@ class DocAnnotator extends Annotator {
this.createHighlightDialog.show(this.lastSelection);
}
}, SELECTION_TIMEOUT);
this.isCreatingHighlight = true;

const { page } = util.getPageInfo(event.target);

Expand Down Expand Up @@ -785,6 +798,7 @@ class DocAnnotator extends Annotator {
return true;
}

this.highlighter.removeAllHighlights();
this.resetHighlightSelection(event);
return false;
}
Expand Down
21 changes: 9 additions & 12 deletions src/doc/DocHighlightThread.js
Expand Up @@ -115,22 +115,19 @@ class DocHighlightThread extends AnnotationThread {
return super.save(type, text);
}

/**
* Deletes an annotation.
*
* @param {Object} annotation annotation to delete
* @param {boolean} [useServer] Whether or not to delete on server, default true
* @return {void}
*/
delete(annotation: Object, useServer: boolean = true): Promise<any> {
const promise = super.delete(annotation, useServer);
/** @inheritdoc */
deleteSuccessHandler = () => {
// Broadcast annotation deletion event
this.emit(THREAD_EVENT.delete);

if (this.threadID) {
this.renderAnnotationPopover();
} else {
// $FlowFixMe
const { page } = this.location;
this.emit(THREAD_EVENT.render, { page });
}

return promise;
}
};

/**
* Mousedown handler for thread. Deletes this thread if it is still pending.
Expand Down
3 changes: 0 additions & 3 deletions src/doc/__tests__/CreateHighlightDialog-test.js
Expand Up @@ -158,9 +158,6 @@ describe('doc/CreateHighlightDialog', () => {

describe('onCommentClick()', () => {
it('should create a plain highlight and render the popover', () => {
document.getSelection = jest.fn().mockReturnValue({
removeAllRanges: jest.fn()
});
dialog.onCommentClick({ preventDefault: jest.fn(), stopPropagation: jest.fn() });
expect(dialog.emit).toBeCalledWith(CREATE_EVENT.comment);
expect(dialog.renderAnnotationPopover).toBeCalled();
Expand Down
16 changes: 16 additions & 0 deletions src/doc/__tests__/DocAnnotator-test.js
Expand Up @@ -411,6 +411,12 @@ describe('doc/DocAnnotator', () => {

describe('renderPage()', () => {
beforeEach(() => {
document.getSelection = jest.fn().mockReturnValue({
removeAllRanges: jest.fn()
});
annotator.highlighter = {
removeAllHighlights: jest.fn()
};
annotator.scaleAnnotationCanvases = jest.fn();
annotator.modeControllers = {
type: {
Expand Down Expand Up @@ -813,6 +819,12 @@ describe('doc/DocAnnotator', () => {
expect(window.getSelection).not.toBeCalled();
});

it('should do nothing the the user is currently creating a highlight annotation', () => {
annotator.isCreatingHighlight = true;
annotator.onSelectionChange(event);
expect(window.getSelection).not.toBeCalled();
});

it('should clear selection if the highlight has not changed', () => {
const selection = {
anchorNode: 'derp',
Expand Down Expand Up @@ -852,6 +864,7 @@ describe('doc/DocAnnotator', () => {

annotator.onSelectionChange(event);
expect(annotator.selectionEndTimeout).not.toBeNull();
expect(annotator.isCreatingHighlight).toBeTruthy();
});

it('should set all of the highlight annotations on the page to "inactive" state', () => {
Expand Down Expand Up @@ -947,6 +960,9 @@ describe('doc/DocAnnotator', () => {
annotator.modeControllers = {
highlight: controller
};
annotator.highlighter = {
removeAllHighlights: jest.fn()
};
annotator.activeThread = undefined;
annotator.plainHighlightEnabled = false;
annotator.commentHighlightEnabled = false;
Expand Down

0 comments on commit bfc839e

Please sign in to comment.