Skip to content

Commit

Permalink
feat(annotations): Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingze Xiao committed Jun 16, 2020
1 parent 599f3d3 commit bff442d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
27 changes: 25 additions & 2 deletions src/lib/AnnotationControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ interface ControlsMap {
[key: string]: () => void;
}

const buttonClassMap = {
region: CLASS_REGION_BUTTON,
};

export default class AnnotationControls {
private controls: Controls;

Expand Down Expand Up @@ -60,7 +64,11 @@ export default class AnnotationControls {
}

public focus(): void {
this.controlsElement.querySelector(`.${CLASS_REGION_BUTTON}`).focus();
const buttonClass = buttonClassMap[this.currentActiveControl];
if (!buttonClass) {
return;
}
this.controlsElement.querySelector(`.${buttonClass}`).focus();
}

/**
Expand Down Expand Up @@ -152,7 +160,7 @@ export default class AnnotationControls {
/**
* Initialize the annotation controls with options.
*/
public init({ onRegionClick = noop }: Options = {}): void {
public init({ onRegionClick = noop, annotator }: Options = {}): void {
const groupElement = this.controls.addGroup(CLASS_ANNOTATIONS_GROUP);
const regionButton = this.controls.add(
__('region_comment'),
Expand All @@ -164,5 +172,20 @@ export default class AnnotationControls {
);

regionButton.setAttribute('data-testid', 'bp-AnnotationsControls-regionBtn');
regionButton.onkeydown = (event: KeyboardEvent): void => {
if (!annotator) {
return;
}

if (event.key !== 'Escape') {
return;
}

this.resetControls();
annotator.toggleAnnotationMode(AnnotationMode.NONE);

event.preventDefault();
event.stopPropagation();
};
}
}
16 changes: 2 additions & 14 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1178,8 +1178,8 @@ class BaseViewer extends EventEmitter {
}
}

handleAnnotationStagedEvent(staged) {
if (!staged && this.annotationControls) {
handleAnnotationStagedEvent({ staged }) {
if (this.annotationControls && !staged) {
this.annotationControls.focus();
}
}
Expand Down Expand Up @@ -1264,18 +1264,6 @@ class BaseViewer extends EventEmitter {

return addedElement;
}

/**
* Method to exit annotation mode
*
* @return {void}
*/
exitAnnotationMode = () => {
if (this.areAnnotationsEnabled() && this.annotator && this.annotationControls) {
this.annotator.toggleAnnotationMode(AnnotationMode.NONE);
this.annotationControls.resetControls();
}
};
}

export default BaseViewer;
4 changes: 1 addition & 3 deletions src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,9 +593,6 @@ class DocBaseViewer extends BaseViewer {
case ']':
this.nextPage();
break;
case 'Escape':
this.exitAnnotationMode();
break;
default:
if (this.findBar) {
return this.findBar.onKeydown(event);
Expand Down Expand Up @@ -1105,6 +1102,7 @@ class DocBaseViewer extends BaseViewer {

if (this.areNewAnnotationsEnabled() && this.hasAnnotationCreatePermission()) {
this.annotationControls.init({
annotator: this.annotator,
onRegionClick: this.regionClickHandler,
});
}
Expand Down

0 comments on commit bff442d

Please sign in to comment.