Skip to content

Commit d7718a4

Browse files
WiFuchsnateirwin25
andauthored
Add doodlin controls (#4)
* add basic doodlability * tweak css * remove logs and bad comments * small format change * test prettier * test prettier * test prettier * test prettier * pre commit hooks are running * small change to format file * pass linter * so thats how type checking works * Doodlintoolbar (#3) * added drawing toolbar * Eraser and back button now turn off doodlin * link up erasing * remove dead code * update gitignore * prettify Co-authored-by: Nut <[email protected]>
1 parent 8d99a69 commit d7718a4

File tree

16 files changed

+156
-66
lines changed

16 files changed

+156
-66
lines changed

.eslintcache

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
[{"/Users/willfuchs/Documents/Cal Poly 20-21/csc 402/client/src/annotator/components/toolbar.js":"1","/Users/willfuchs/Documents/Cal Poly 20-21/csc 402/client/src/annotator/guest.js":"2","/Users/willfuchs/Documents/Cal Poly 20-21/csc 402/client/src/annotator/toolbar.js":"3","/Users/willfuchs/Documents/Cal Poly 20-21/csc 402/client/src/doodle/doodleCanvas.js":"4"},{"size":5021,"mtime":1614880460577,"results":"5","hashOfConfig":"6"},{"size":22213,"mtime":1614880828948,"results":"7","hashOfConfig":"6"},{"size":3684,"mtime":1614881067711,"results":"8","hashOfConfig":"6"},{"size":2861,"mtime":1614880967602,"results":"9","hashOfConfig":"10"},{"filePath":"11","messages":"12","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"zv3vgn",{"filePath":"13","messages":"14","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"15","messages":"16","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"17","messages":"18","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"itfxzx","/Users/willfuchs/Documents/Cal Poly 20-21/csc 402/client/src/annotator/components/toolbar.js",[],"/Users/willfuchs/Documents/Cal Poly 20-21/csc 402/client/src/annotator/guest.js",[],"/Users/willfuchs/Documents/Cal Poly 20-21/csc 402/client/src/annotator/toolbar.js",[],"/Users/willfuchs/Documents/Cal Poly 20-21/csc 402/client/src/doodle/doodleCanvas.js",[]]

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ package-lock.json
1111
.tlskey.pem
1212

1313
.tox
14+
15+
.eslintcache

src/annotator/components/toolbar.js

Lines changed: 95 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,18 @@ ToolbarButton.propTypes = {
7272
* Callback to toggle visibility of highlights in the document.
7373
* @prop {() => any} toggleSidebar -
7474
* Callback to toggle the visibility of the sidebar.
75-
* @prop {() => any} toggleDoodleability -
76-
* Callback to toggle visibility of highlights in the document.
75+
* @prop {(object) => any} setDoodleOptions
76+
* Callback to set the options of the doodle canvas
7777
* @prop {import("preact").Ref<HTMLButtonElement>} [toggleSidebarRef] -
7878
* Ref that gets set to the toolbar button for toggling the sidebar.
7979
* This is exposed to enable the drag-to-resize functionality of this
8080
* button.
8181
* @prop {boolean} [useMinimalControls] -
8282
* If true, all controls are hidden except for the "Close sidebar" button
8383
* when the sidebar is open.
84+
* @prop {boolean} [drawingToolbarActivated]
85+
*
86+
* @prop {() => any} drawingToolbarToggle
8487
*/
8588

8689
/**
@@ -97,54 +100,102 @@ export default function Toolbar({
97100
showHighlights,
98101
toggleHighlights,
99102
toggleSidebar,
100-
toggleDoodleability,
103+
setDoodleOptions,
101104
toggleSidebarRef,
102105
useMinimalControls = false,
106+
drawingToolbarActivated,
107+
drawingToolbarToggle,
103108
}) {
104-
return (
105-
<div>
106-
{useMinimalControls && isSidebarOpen && (
107-
<ToolbarButton
108-
className="annotator-toolbar__sidebar-close"
109-
label="Close annotation sidebar"
110-
icon="cancel"
111-
onClick={closeSidebar}
112-
/>
113-
)}
114-
{!useMinimalControls && (
115-
<ToolbarButton
116-
className="annotator-toolbar__sidebar-toggle"
117-
buttonRef={toggleSidebarRef}
118-
label="Annotation sidebar"
119-
icon={isSidebarOpen ? 'caret-right' : 'caret-left'}
120-
expanded={isSidebarOpen}
121-
onClick={toggleSidebar}
122-
/>
123-
)}
124-
{!useMinimalControls && (
125-
<div className="annotator-toolbar-buttonbar">
109+
if (!drawingToolbarActivated) {
110+
return (
111+
<div>
112+
{useMinimalControls && isSidebarOpen && (
126113
<ToolbarButton
127-
label="Show highlights"
128-
icon={showHighlights ? 'show' : 'hide'}
129-
selected={showHighlights}
130-
onClick={toggleHighlights}
114+
className="annotator-toolbar__sidebar-close"
115+
label="Close annotation sidebar"
116+
icon="cancel"
117+
onClick={closeSidebar}
131118
/>
119+
)}
120+
{!useMinimalControls && (
132121
<ToolbarButton
133-
label={
134-
newAnnotationType === 'note' ? 'New page note' : 'New annotation'
135-
}
136-
icon={newAnnotationType === 'note' ? 'note' : 'annotate'}
137-
onClick={createAnnotation}
122+
className="annotator-toolbar__sidebar-toggle"
123+
buttonRef={toggleSidebarRef}
124+
label="Annotation sidebar"
125+
icon={isSidebarOpen ? 'caret-right' : 'caret-left'}
126+
expanded={isSidebarOpen}
127+
onClick={toggleSidebar}
138128
/>
129+
)}
130+
{!useMinimalControls && (
131+
<div className="annotator-toolbar-buttonbar">
132+
<ToolbarButton
133+
label="Show highlights"
134+
icon={showHighlights ? 'show' : 'hide'}
135+
selected={showHighlights}
136+
onClick={toggleHighlights}
137+
/>
138+
<ToolbarButton
139+
label={
140+
newAnnotationType === 'note'
141+
? 'New page note'
142+
: 'New annotation'
143+
}
144+
icon={newAnnotationType === 'note' ? 'note' : 'annotate'}
145+
onClick={createAnnotation}
146+
/>
147+
<ToolbarButton
148+
label="New Doodle"
149+
icon="doodle"
150+
onClick={drawingToolbarToggle}
151+
/>
152+
</div>
153+
)}
154+
</div>
155+
);
156+
} else {
157+
return (
158+
<div>
159+
{useMinimalControls && isSidebarOpen && (
139160
<ToolbarButton
140-
label={'New Doodle'}
141-
icon={'doodle'}
142-
onClick={toggleDoodleability}
161+
className="annotator-toolbar__sidebar-close"
162+
label="Close annotation sidebar"
163+
icon="cancel"
164+
onClick={closeSidebar}
143165
/>
144-
</div>
145-
)}
146-
</div>
147-
);
166+
)}
167+
{!useMinimalControls && (
168+
<ToolbarButton
169+
className="annotator-toolbar__sidebar-toggle"
170+
buttonRef={toggleSidebarRef}
171+
label="Annotation sidebar"
172+
icon={isSidebarOpen ? 'caret-right' : 'caret-left'}
173+
expanded={isSidebarOpen}
174+
onClick={toggleSidebar}
175+
/>
176+
)}
177+
{!useMinimalControls && (
178+
<div className="annotator-toolbar-buttonbar">
179+
<ToolbarButton
180+
label="Stop doodle"
181+
icon="close"
182+
onClick={drawingToolbarToggle}
183+
/>
184+
<ToolbarButton
185+
label="Pen"
186+
icon="pen"
187+
onClick={() => setDoodleOptions({ tool: 'pen', size: 7 })}
188+
/>
189+
<ToolbarButton
190+
label="Eraser"
191+
icon="erase"
192+
onClick={() => setDoodleOptions({ tool: 'eraser', size: 25 })}
193+
/>
194+
</div>
195+
)}
196+
</div>
197+
);
198+
}
148199
}
149200

150201
Toolbar.propTypes = {
@@ -158,4 +209,7 @@ Toolbar.propTypes = {
158209
toggleDoodleability: propTypes.func.isRequired,
159210
toggleSidebarRef: propTypes.any,
160211
useMinimalControls: propTypes.bool,
212+
drawingToolbarActivated: propTypes.bool,
213+
drawingToolbarToggle: propTypes.func.isRequired,
214+
setDoodleOptions: propTypes.func.isRequired,
161215
};

src/annotator/guest.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ export default class Guest extends Delegator {
356356
crossframe.on('setDoodleability', state => {
357357
this.setDoodleability(state);
358358
});
359+
360+
crossframe.on('setDoodleOptions', state => {
361+
this.setDoodleOptions(state);
362+
});
359363
}
360364

361365
destroy() {
@@ -729,4 +733,20 @@ export default class Guest extends Delegator {
729733
this.doodleCanvasController.doodleable = shouldBeDoodleable;
730734
}
731735
}
736+
737+
/**
738+
* Set the brush options for doodlin
739+
*
740+
* @param {object} options
741+
*/
742+
setDoodleOptions(options) {
743+
if (this.doodleCanvasController) {
744+
if (options.hasOwnProperty('tool')) {
745+
this.doodleCanvasController.tool = options.tool;
746+
}
747+
if (options.hasOwnProperty('size')) {
748+
this.doodleCanvasController.size = options.size;
749+
}
750+
}
751+
}
732752
}

src/annotator/icons.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ export default {
1616
pointer: require('../images/icons/pointer.svg'),
1717
show: require('../images/icons/show.svg'),
1818
doodle: require('../images/icons/doodle.svg'),
19+
close: require('../images/icons/close.svg'),
20+
pen: require('../images/icons/pen.svg'),
21+
erase: require('../images/icons/erase.svg'),
1922
};

src/annotator/sidebar.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export default class Sidebar extends Guest {
136136
setSidebarOpen: open => (open ? this.show() : this.hide()),
137137
setHighlightsVisible: show => this.setAllVisibleHighlights(show),
138138
setUserCanDoodle: show => this.setAllDoodleability(show),
139+
setDoodleOptions: options => this.setAllDoodleOptions(options),
139140
});
140141
this.toolbar.useMinimalControls = config.theme === 'clean';
141142

@@ -429,4 +430,13 @@ export default class Sidebar extends Guest {
429430
setAllDoodleability(shouldBeDoodleable) {
430431
this.crossframe.call('setDoodleability', shouldBeDoodleable);
431432
}
433+
434+
/**
435+
* (CreativeNTR) Set the doodle options
436+
*
437+
* @param {object} options
438+
*/
439+
setAllDoodleOptions(options) {
440+
this.crossframe.call('setDoodleOptions', options);
441+
}
432442
}

src/annotator/toolbar.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Toolbar from './components/toolbar';
88
* @prop {(open: boolean) => any} setSidebarOpen
99
* @prop {(visible: boolean) => any} setHighlightsVisible
1010
* @prop {(doodleable: boolean) => any} setUserCanDoodle
11+
* @prop {(doodleable: boolean) => any} setDoodleOptions
1112
*/
1213

1314
/**
@@ -27,13 +28,16 @@ export class ToolbarController {
2728
setSidebarOpen,
2829
setHighlightsVisible,
2930
setUserCanDoodle,
31+
setDoodleOptions,
3032
} = options;
3133

3234
this._container = container;
3335
this._container.className = 'annotator-toolbar';
3436

3537
this._useMinimalControls = false;
3638

39+
this._drawingToolbar = false;
40+
3741
/** @type {'annotation'|'note'} */
3842
this._newAnnotationType = 'note';
3943

@@ -45,9 +49,14 @@ export class ToolbarController {
4549
this._toggleSidebar = () => setSidebarOpen(!this._sidebarOpen);
4650
this._toggleHighlights = () =>
4751
setHighlightsVisible(!this._highlightsVisible);
48-
this._toggleDoodleability = () => {
49-
this._doodleable = !this._doodleable;
52+
this._toggleDoodleToolbar = () => {
53+
this._drawingToolbar = !this._drawingToolbar;
54+
this._doodleable = this._drawingToolbar;
5055
setUserCanDoodle(this._doodleable);
56+
this.render();
57+
};
58+
this._setDoodleOptions = options => {
59+
setDoodleOptions(options);
5160
};
5261
this._createAnnotation = () => {
5362
createAnnotation();
@@ -136,7 +145,9 @@ export class ToolbarController {
136145
toggleSidebar={this._toggleSidebar}
137146
toggleSidebarRef={this._sidebarToggleButton}
138147
useMinimalControls={this.useMinimalControls}
139-
toggleDoodleability={this._toggleDoodleability}
148+
setDoodleOptions={this._setDoodleOptions}
149+
drawingToolbarActivated={this._drawingToolbar}
150+
drawingToolbarToggle={this._toggleDoodleToolbar}
140151
/>,
141152
this._container
142153
);

src/doodle/canvas.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@ import { createElement } from 'preact';
22
import { useRef, useEffect } from 'preact/hooks';
33
import propTypes from 'prop-types';
44

5-
//TODO not sure how to enforce this type...
6-
/**
7-
* @typedef Line
8-
* @property {string} tool - The tool used to draw this line.
9-
* @property {any} points
10-
*/
11-
125
const Canvas = ({
136
width,
147
height,
@@ -28,14 +21,13 @@ const Canvas = ({
2821
// Move to the first point, begin the line
2922
ctx.beginPath();
3023
ctx.moveTo(startX, startY);
24+
ctx.lineWidth = line.size;
3125

3226
if (line.tool === 'pen') {
3327
ctx.globalCompositeOperation = 'source-over';
34-
ctx.lineWidth = line.size;
3528
ctx.strokeStyle = line.color;
3629
} else {
3730
ctx.globalCompositeOperation = 'destination-out';
38-
ctx.lineWidth = 25;
3931
}
4032

4133
// Draw the rest of the lines
@@ -48,7 +40,6 @@ const Canvas = ({
4840
};
4941

5042
useEffect(() => {
51-
// for testing
5243
const canvas = canvasRef.current;
5344
const ctx = canvas.getContext('2d');
5445

src/doodle/doodleCanvas.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const DoodleCanvas = ({ tool, size, active, attachedElement }) => {
5959
const newLine = {
6060
tool: curLine.tool,
6161
color: curLine.color,
62+
size: curLine.size,
6263
points: [[xPos, yPos], ...curLine.points],
6364
};
6465

src/doodle/doodleController.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class DoodleController {
2323
*/
2424
set tool(toolType) {
2525
this._tool = toolType;
26+
this.render();
2627
}
2728

2829
get tool() {
@@ -36,6 +37,7 @@ export class DoodleController {
3637
*/
3738
set size(newSize) {
3839
this._size = newSize;
40+
this.render();
3941
}
4042

4143
get size() {

0 commit comments

Comments
 (0)