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

Add configuration option for available annotation tools #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions demo/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const config = {
annotation: {
adapter: (canvasId) => new LocalStorageAdapter(`localStorage://?canvasId=${canvasId}`),
// adapter: (canvasId) => new AnnototAdapter(canvasId, endpointUrl),
// availableTools: ['cursor', 'edit', 'rectangle', 'ellipse', 'polygon', 'freehand'],
},
id: 'demo',
window: {
Expand Down
63 changes: 42 additions & 21 deletions src/AnnotationCreation.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import TextEditor from './TextEditor';
import WebAnnotation from './WebAnnotation';
import CursorIcon from './icons/Cursor';

const targetTools = ['cursor', 'edit', 'rectangle', 'ellipse', 'polygon', 'freehand'];

/** */
class AnnotationCreation extends Component {
/** */
Expand Down Expand Up @@ -62,8 +64,10 @@ class AnnotationCreation extends Component {
annoState.svg = props.annotation.target.selector.value;
}
}
const availableTools = (props.config.annotation && props.config.annotation.availableTools)
|| targetTools;
this.state = {
activeTool: 'cursor',
activeTool: availableTools[0],
annoBody: '',
closedMode: 'closed',
colorPopoverOpen: false,
Expand Down Expand Up @@ -208,13 +212,17 @@ class AnnotationCreation extends Component {
/** */
render() {
const {
annotation, classes, closeCompanionWindow, id, windowId,
annotation, classes, closeCompanionWindow, config, id, windowId,
} = this.props;

const {
activeTool, colorPopoverOpen, currentColorType, fillColor, popoverAnchorEl, strokeColor,
popoverLineWeightAnchorEl, lineWeightPopoverOpen, strokeWidth, closedMode, annoBody, svg,
} = this.state;
const availableTools = (config.annotation && config.annotation.availableTools) || targetTools;
const displayDivider = (availableTools.indexOf('cursor') !== -1 || availableTools.indexOf('edit') !== -1)
&& (availableTools.indexOf('rectangle') !== -1 || availableTools.indexOf('ellipse') !== -1
|| availableTools.indexOf('polygon') !== -1 || availableTools.indexOf('freehand') !== -1);
return (
<CompanionWindow
title={annotation ? 'Edit annotation' : 'New annotation'}
Expand Down Expand Up @@ -248,14 +256,18 @@ class AnnotationCreation extends Component {
aria-label="tool selection"
size="small"
>
<ToggleButton value="cursor" aria-label="select cursor">
<CursorIcon />
</ToggleButton>
<ToggleButton value="edit" aria-label="select cursor">
<FormatShapesIcon />
</ToggleButton>
{ availableTools.indexOf('cursor') !== -1 && (
<ToggleButton value="cursor" aria-label="select cursor">
<CursorIcon />
</ToggleButton>
)}
{ availableTools.indexOf('edit') !== -1 && (
<ToggleButton value="edit" aria-label="select cursor">
<FormatShapesIcon />
</ToggleButton>
)}
</ToggleButtonGroup>
<Divider flexItem orientation="vertical" className={classes.divider} />
{ displayDivider && <Divider flexItem orientation="vertical" className={classes.divider} /> }
<ToggleButtonGroup
className={classes.grouped}
value={activeTool}
Expand All @@ -264,18 +276,26 @@ class AnnotationCreation extends Component {
aria-label="tool selection"
size="small"
>
<ToggleButton value="rectangle" aria-label="add a rectangle">
<RectangleIcon />
</ToggleButton>
<ToggleButton value="ellipse" aria-label="add a circle">
<CircleIcon />
</ToggleButton>
<ToggleButton value="polygon" aria-label="add a polygon">
<PolygonIcon />
</ToggleButton>
<ToggleButton value="freehand" aria-label="free hand polygon">
<GestureIcon />
</ToggleButton>
{ availableTools.indexOf('rectangle') !== -1 && (
<ToggleButton value="rectangle" aria-label="add a rectangle">
<RectangleIcon />
</ToggleButton>
)}
{ availableTools.indexOf('ellipse') !== -1 && (
<ToggleButton value="ellipse" aria-label="add a circle">
<CircleIcon />
</ToggleButton>
)}
{ availableTools.indexOf('polygon') !== -1 && (
<ToggleButton value="polygon" aria-label="add a polygon">
<PolygonIcon />
</ToggleButton>
)}
{ availableTools.indexOf('freehand') !== -1 && (
<ToggleButton value="freehand" aria-label="free hand polygon">
<GestureIcon />
</ToggleButton>
)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are giving an ordered list of tools, it could also be rendered in the order of the availableTools list maybe, if you loop over availableTools and render based on the value.

</ToggleButtonGroup>
</Paper>
</Grid>
Expand Down Expand Up @@ -426,6 +446,7 @@ AnnotationCreation.propTypes = {
config: PropTypes.shape({
annotation: PropTypes.shape({
adapter: PropTypes.func,
availableTools: PropTypes.arrayOf(PropTypes.oneOf(targetTools)),
}),
}).isRequired,
id: PropTypes.string.isRequired,
Expand Down