Skip to content

Commit 6152b8c

Browse files
authored
Added color and size toolbars (#12)
1 parent b4907f0 commit 6152b8c

File tree

21 files changed

+255
-5
lines changed

21 files changed

+255
-5
lines changed

src/annotator/components/test/toolbar-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('Toolbar', () => {
1919
showHighlights={false}
2020
newAnnotationType="note"
2121
useMinimalControls={false}
22+
setDoodleOptions={noop}
2223
{...props}
2324
/>
2425
);

src/annotator/components/toolbar.js

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { SvgIcon } from '@hypothesis/frontend-shared';
22
import propTypes from 'prop-types';
33
import { createElement } from 'preact';
4+
import { useState, useEffect } from 'preact/hooks';
45

56
/**
67
* @param {Object} props
@@ -109,6 +110,20 @@ export default function Toolbar({
109110
drawingToolbarActivated,
110111
drawingToolbarToggle,
111112
}) {
113+
const small = 1;
114+
const medium = 5;
115+
const large = 10;
116+
117+
const [toolSize, setToolSize] = useState(medium);
118+
119+
const [tool, setTool] = useState('pen');
120+
121+
const [color, setColor] = useState('red');
122+
123+
useEffect(() => {
124+
setDoodleOptions({ tool: tool, size: toolSize, color: color });
125+
}, [toolSize, tool, color, setDoodleOptions]);
126+
112127
if (!drawingToolbarActivated) {
113128
return (
114129
<div>
@@ -187,13 +202,94 @@ export default function Toolbar({
187202
<ToolbarButton
188203
label="Pen"
189204
icon="pen"
190-
onClick={() => setDoodleOptions({ tool: 'pen', size: 7 })}
205+
onClick={() => {
206+
setTool('pen');
207+
}}
191208
/>
192209
<ToolbarButton
193210
label="Eraser"
194211
icon="erase"
195-
onClick={() => setDoodleOptions({ tool: 'eraser', size: 25 })}
212+
onClick={() => {
213+
setTool('eraser');
214+
}}
196215
/>
216+
<button
217+
className="popup annotator-toolbar-button size selected"
218+
onClick={e => {
219+
//@ts-ignore
220+
e.target?.querySelector('#myPopup').classList.toggle('show');
221+
//@ts-ignore
222+
e.target?.parentElement
223+
.querySelector('.color')
224+
.querySelector('#myPopup')
225+
.classList.remove('show');
226+
}}
227+
aria-label="Brush Size"
228+
>
229+
<SvgIcon name="sizes-icon" className="svgicon" />
230+
<span className="popuptext" id="myPopup">
231+
<ToolbarButton
232+
label="Large"
233+
icon="circle-large"
234+
onClick={() => {
235+
setToolSize(large);
236+
}}
237+
/>
238+
<ToolbarButton
239+
label="Medium"
240+
icon="circle-medium"
241+
onClick={() => {
242+
setToolSize(medium);
243+
}}
244+
/>
245+
<ToolbarButton
246+
label="Small"
247+
icon="circle-small"
248+
className="annotator-toolbar-button smallSelector"
249+
onClick={() => {
250+
setToolSize(small);
251+
}}
252+
/>
253+
</span>
254+
</button>
255+
<button
256+
className="popup annotator-toolbar-button color"
257+
onClick={e => {
258+
//@ts-ignore
259+
e.target?.querySelector('#myPopup').classList.toggle('show');
260+
//@ts-ignore
261+
e.target?.parentElement
262+
.querySelector('.size')
263+
.querySelector('#myPopup')
264+
.classList.remove('show');
265+
}}
266+
aria-label="Brush Color"
267+
>
268+
<SvgIcon name="color-icon" className="svgicon" />
269+
<span className="popuptext" id="myPopup">
270+
<ToolbarButton
271+
label="Red"
272+
icon="red-icon"
273+
onClick={() => {
274+
setColor('red');
275+
}}
276+
/>
277+
<ToolbarButton
278+
label="Green"
279+
icon="green-icon"
280+
onClick={() => {
281+
setColor('green');
282+
}}
283+
/>
284+
<ToolbarButton
285+
label="Blue"
286+
icon="blue-icon"
287+
onClick={() => {
288+
setColor('blue');
289+
}}
290+
/>
291+
</span>
292+
</button>
197293
<ToolbarButton
198294
label="Save"
199295
icon="save"

src/annotator/guest.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,9 @@ export default class Guest extends Delegator {
768768
if (options.hasOwnProperty('size')) {
769769
this.doodleCanvasController.size = options.size;
770770
}
771+
if (options.hasOwnProperty('color')) {
772+
this.doodleCanvasController.color = options.color;
773+
}
771774
}
772775
}
773776

src/annotator/icons.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,12 @@ export default {
2020
pen: require('../images/icons/pen.svg'),
2121
erase: require('../images/icons/erase.svg'),
2222
save: require('../images/icons/save.svg'),
23+
'circle-small': require('../images/icons/circleSmall.svg'),
24+
'circle-medium': require('../images/icons/circleMedium.svg'),
25+
'circle-large': require('../images/icons/circleLarge.svg'),
26+
'red-icon': require('../images/icons/red.svg'),
27+
'blue-icon': require('../images/icons/blue.svg'),
28+
'green-icon': require('../images/icons/green.svg'),
29+
'color-icon': require('../images/icons/palette.svg'),
30+
'sizes-icon': require('../images/icons/sizes.svg'),
2331
};

src/annotator/sidebar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export default class Sidebar extends Guest {
147147
{
148148
tool: 'pen',
149149
size: 5,
150+
color: 'red',
150151
}
151152
);
152153

src/doodle/doodleCanvas.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import propTypes from 'prop-types';
88
* @prop {string} tool - The name of the tool that is being used. One of {'pen'|'eraser'}.
99
* @prop {number} size - The size of the brush.
1010
* @prop {boolean} active - Whether the canvas can be doodled on at this time
11+
* @prop {string} color - The color of the brush
1112
* @prop {HTMLElement} attachedElement - Which element the DoodleCanvas should cover.
1213
* @prop {Array<import('../types/api').DoodleLine>} lines - An array of lines that compose this doodle.
1314
* @prop {Function} setLines - A function to set the lines
@@ -26,6 +27,7 @@ const DoodleCanvas = ({
2627
tool,
2728
size,
2829
active,
30+
color,
2931
attachedElement,
3032
lines,
3133
setLines,
@@ -54,7 +56,7 @@ const DoodleCanvas = ({
5456
setLines([
5557
{
5658
tool: tool,
57-
color: 'red',
59+
color: color,
5860
size: size,
5961
points: [[e.offsetX, e.offsetY]],
6062
},
@@ -126,6 +128,7 @@ DoodleCanvas.propTypes = {
126128
active: propTypes.bool.isRequired,
127129
lines: propTypes.array.isRequired,
128130
setLines: propTypes.func.isRequired,
131+
color: propTypes.string.isRequired,
129132
attachedElement: propTypes.any.isRequired,
130133
};
131134

src/doodle/doodleController.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ export class DoodleController {
88
* @param {any} options
99
*/
1010
constructor(container, options) {
11-
const { tool, size } = options;
11+
const { tool, size, color } = options;
1212
this._lines = [];
1313
this._savedLines = [];
1414
this._newLines = [];
1515

1616
this._container = container === null ? document.body : container;
1717
this._tool = tool;
1818
this._size = size;
19+
this._color = color;
1920

2021
this._doodleable = false;
2122

@@ -63,6 +64,15 @@ export class DoodleController {
6364
this.render();
6465
}
6566

67+
set color(newColor) {
68+
this._color = newColor;
69+
this.render();
70+
}
71+
72+
get color() {
73+
return this._color;
74+
}
75+
6676
get size() {
6777
return this._size;
6878
}
@@ -99,6 +109,7 @@ export class DoodleController {
99109
active={this._doodleable}
100110
lines={this.newLines}
101111
setLines={setLines}
112+
color={this._color}
102113
/>
103114
<DisplayCanvas lines={this.savedLines} container={this._container} />
104115
</Fragment>,

src/images/icons/blue.svg

Lines changed: 9 additions & 0 deletions
Loading

src/images/icons/circle.svg

Lines changed: 1 addition & 0 deletions
Loading

src/images/icons/circleLarge.svg

Lines changed: 6 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)