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

Fix: Ensure onSelectionChange isn't triggered while creating highlights #281

Merged
merged 4 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Annotator.scss
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@

// Quad point positioning - the helper divs are positioned relative to the Rangy-created element
.bp-doc .rangy-highlight {
background-color: lighten($highlight-yellow, 10%);
opacity: .25;
background-color: $highlight-yellow;
position: relative;
}

Expand Down
3 changes: 3 additions & 0 deletions src/components/ActionControls/ActionControls.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
}

.ba-annotation-input-container {
bottom: 0;
padding: 15px;
position: absolute;
width: 100%;
}

Expand Down
5 changes: 1 addition & 4 deletions src/doc/CreateHighlightDialog.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
pramodsum marked this conversation as resolved.
Show resolved Hide resolved
if (isClickOutsideCreateDialog) {
this.hideCreateDialog(event);
pramodsum marked this conversation as resolved.
Show resolved Hide resolved
this.highlighter.removeAllHighlights();
pramodsum marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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