-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(annotations): Add region comment button in toolbar (#1183)
* feat(annotations): Add region comment button in toolbar * feat(annotations): Correct styles * feat(annotations): Address comments * feat(annotations): Rename variables * feat(annotations): Add tests, SUIT style, and typescript * feat(annotations): Fix typescript * feat(annotations): Address comments * feat(annotations): Address comments * feat(annotations): Address comments
- Loading branch information
Mingze
authored
Mar 27, 2020
1 parent
45f92a5
commit 04ea830
Showing
13 changed files
with
238 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
.bp-AnnotationControls-group { | ||
padding: 0 4px 0 8px; | ||
border-left: 1px solid $twos; | ||
|
||
.bp-AnnotationControls-regionBtn { | ||
width: $controls-button-width; | ||
height: $controls-button-width; | ||
border-radius: 4px; | ||
|
||
svg { | ||
fill: $white; | ||
} | ||
|
||
&.is-active { | ||
background-color: $white; | ||
|
||
svg { | ||
fill: $black; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import noop from 'lodash/noop'; | ||
import { ICON_REGION_COMMENT } from './icons/icons'; | ||
import Controls, { CLASS_BOX_CONTROLS_GROUP_BUTTON } from './Controls'; | ||
|
||
export const CLASS_ANNOTATIONS_GROUP = 'bp-AnnotationControls-group'; | ||
export const CLASS_REGION_BUTTON = 'bp-AnnotationControls-regionBtn'; | ||
export const CLASS_BUTTON_ACTIVE = 'is-active'; | ||
|
||
export type RegionHandler = ({ isRegionActive, event }: { isRegionActive: boolean; event: MouseEvent }) => void; | ||
export type Options = { | ||
onRegionClick?: RegionHandler; | ||
}; | ||
|
||
declare const __: (key: string) => string; | ||
|
||
export default class AnnotationControls { | ||
/** @property {Controls} - Controls object */ | ||
private controls: Controls; | ||
|
||
/** @property {boolean} - Region comment mode active state */ | ||
private isRegionActive = false; | ||
|
||
/** | ||
* [constructor] | ||
* | ||
* @param {Controls} controls - Viewer controls | ||
* @return {AnnotationControls} Instance of AnnotationControls | ||
*/ | ||
constructor(controls: Controls) { | ||
if (!controls || !(controls instanceof Controls)) { | ||
throw Error('controls must be an instance of Controls'); | ||
} | ||
|
||
this.controls = controls; | ||
} | ||
|
||
/** | ||
* Region comment button click handler | ||
* | ||
* @param {RegionHandler} onRegionClick - region click handler in options | ||
* @param {MouseEvent} event - mouse event | ||
* @return {void} | ||
*/ | ||
private handleRegionClick = (onRegionClick: RegionHandler) => (event: MouseEvent): void => { | ||
const regionButtonElement = event.target as HTMLButtonElement; | ||
|
||
this.isRegionActive = !this.isRegionActive; | ||
if (this.isRegionActive) { | ||
regionButtonElement.classList.add(CLASS_BUTTON_ACTIVE); | ||
} else { | ||
regionButtonElement.classList.remove(CLASS_BUTTON_ACTIVE); | ||
} | ||
|
||
onRegionClick({ isRegionActive: this.isRegionActive, event }); | ||
}; | ||
|
||
/** | ||
* Initialize the annotation controls with options. | ||
* | ||
* @param {RegionHandler} [options.onRegionClick] - Callback when region comment button is clicked | ||
* @return {void} | ||
*/ | ||
public init({ onRegionClick = noop }: Options = {}): void { | ||
const groupElement = this.controls.addGroup(CLASS_ANNOTATIONS_GROUP); | ||
this.controls.add( | ||
__('region_comment'), | ||
this.handleRegionClick(onRegionClick), | ||
`${CLASS_BOX_CONTROLS_GROUP_BUTTON} ${CLASS_REGION_BUTTON}`, | ||
ICON_REGION_COMMENT, | ||
'button', | ||
groupElement, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.bp-ZoomControls-currentScale { | ||
min-width: 48px; | ||
color: $white; | ||
font-size: 14px; | ||
text-align: center; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div id="test-annotation-controls-container"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
import AnnotationControls, { CLASS_REGION_BUTTON, CLASS_BUTTON_ACTIVE } from '../AnnotationControls'; | ||
import Controls, { CLASS_BOX_CONTROLS_GROUP_BUTTON } from '../Controls'; | ||
import { ICON_REGION_COMMENT } from '../icons/icons'; | ||
|
||
let annotationControls; | ||
let stubs = {}; | ||
|
||
const sandbox = sinon.sandbox.create(); | ||
|
||
describe('lib/AnnotationControls', () => { | ||
before(() => { | ||
fixture.setBase('src/lib'); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture.load('__tests__/AnnotationControls-test.html'); | ||
const controls = new Controls(document.getElementById('test-annotation-controls-container')); | ||
annotationControls = new AnnotationControls(controls); | ||
stubs.onRegionClick = sandbox.stub(); | ||
}); | ||
|
||
afterEach(() => { | ||
fixture.cleanup(); | ||
sandbox.verifyAndRestore(); | ||
|
||
annotationControls = null; | ||
stubs = {}; | ||
}); | ||
|
||
describe('constructor()', () => { | ||
it('should create the correct DOM structure', () => { | ||
expect(annotationControls.controls).not.to.be.undefined; | ||
}); | ||
|
||
it('should throw an exception if controls is not provided', () => { | ||
expect(() => new AnnotationControls()).to.throw(Error, 'controls must be an instance of Controls'); | ||
}); | ||
}); | ||
|
||
describe('init()', () => { | ||
beforeEach(() => { | ||
stubs.add = sandbox.stub(annotationControls.controls, 'add'); | ||
stubs.regionHandler = sandbox.stub(); | ||
sandbox.stub(annotationControls, 'handleRegionClick').returns(stubs.regionHandler); | ||
}); | ||
|
||
it('should add the controls', () => { | ||
annotationControls.init({ onRegionClick: stubs.onRegionClick }); | ||
|
||
expect(stubs.add).to.be.calledWith( | ||
__('region_comment'), | ||
stubs.regionHandler, | ||
`${CLASS_BOX_CONTROLS_GROUP_BUTTON} ${CLASS_REGION_BUTTON}`, | ||
ICON_REGION_COMMENT, | ||
'button', | ||
sinon.match.any, | ||
); | ||
}); | ||
}); | ||
|
||
describe('handleRegionClick()', () => { | ||
beforeEach(() => { | ||
stubs.classListAdd = sandbox.stub(); | ||
stubs.classListRemove = sandbox.stub(); | ||
stubs.event = sandbox.stub({ | ||
target: { | ||
classList: { | ||
add: stubs.classListAdd, | ||
remove: stubs.classListRemove, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should activate region button then deactivate', () => { | ||
expect(annotationControls.isRegionActive).to.be.false; | ||
|
||
annotationControls.handleRegionClick(stubs.onRegionClick)(stubs.event); | ||
expect(annotationControls.isRegionActive).to.be.true; | ||
expect(stubs.classListAdd).to.be.calledWith(CLASS_BUTTON_ACTIVE); | ||
|
||
annotationControls.handleRegionClick(stubs.onRegionClick)(stubs.event); | ||
expect(annotationControls.isRegionActive).to.be.false; | ||
expect(stubs.classListRemove).to.be.calledWith(CLASS_BUTTON_ACTIVE); | ||
}); | ||
|
||
it('should call onRegionClick', () => { | ||
annotationControls.handleRegionClick(stubs.onRegionClick)(stubs.event); | ||
|
||
expect(stubs.onRegionClick).to.be.calledWith({ | ||
isRegionActive: true, | ||
event: stubs.event, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters