Skip to content

Commit 3eba7d5

Browse files
authored
Hide doodle button (#17)
* add button to hide doodles * cant forget tests * performance boost
1 parent c3239a3 commit 3eba7d5

File tree

9 files changed

+99
-1
lines changed

9 files changed

+99
-1
lines changed

src/annotator/components/toolbar.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ ToolbarButton.propTypes = {
6969
* Icon to show on the "Create annotation" button indicating what kind of annotation
7070
* will be created.
7171
* @prop {boolean} showHighlights - Are highlights currently visible in the document?
72+
* @prop {boolean} showDoodles - Are doodles currently visible in the document?
7273
* @prop {() => any} toggleHighlights -
7374
* Callback to toggle visibility of highlights in the document.
75+
* @prop {() => any} toggleDoodles -
76+
* Callback to toggle visibility of doodles in the document.
7477
* @prop {() => any} toggleSidebar -
7578
* Callback to toggle the visibility of the sidebar.
7679
* @prop {(object) => any} setDoodleOptions
@@ -101,7 +104,9 @@ export default function Toolbar({
101104
isSidebarOpen,
102105
newAnnotationType,
103106
showHighlights,
107+
showDoodles,
104108
toggleHighlights,
109+
toggleDoodles,
105110
toggleSidebar,
106111
setDoodleOptions,
107112
saveDoodle,
@@ -153,6 +158,12 @@ export default function Toolbar({
153158
selected={showHighlights}
154159
onClick={toggleHighlights}
155160
/>
161+
<ToolbarButton
162+
label="Show Doodles"
163+
icon={showDoodles ? 'show' : 'hide'}
164+
selected={showDoodles}
165+
onClick={toggleDoodles}
166+
/>
156167
<ToolbarButton
157168
label={
158169
newAnnotationType === 'note'

src/annotator/guest.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export default class Guest extends Delegator {
119119
super(element, { ...defaultConfig, ...config });
120120

121121
this.visibleHighlights = false;
122+
this.visibleDoodles = false;
122123

123124
/** @type {ToolbarController|null} */
124125
this.toolbar = null;
@@ -298,6 +299,7 @@ export default class Guest extends Delegator {
298299
_setupInitialState(config) {
299300
this.publish('panelReady');
300301
this.setVisibleHighlights(config.showHighlights === 'always');
302+
this.setVisibleDoodles(config.showHighlights === 'always');
301303
}
302304

303305
_connectAnnotationSync() {
@@ -353,6 +355,10 @@ export default class Guest extends Delegator {
353355
this.setVisibleHighlights(state);
354356
});
355357

358+
crossframe.on('setVisibleDoodles', state => {
359+
this.setVisibleDoodles(state);
360+
});
361+
356362
crossframe.on('setDoodleability', state => {
357363
this.setDoodleability(state);
358364
});
@@ -754,6 +760,22 @@ export default class Guest extends Delegator {
754760
}
755761
}
756762

763+
/**
764+
* Set whether doodles are visible in the document or not.
765+
*
766+
* @param {boolean} shouldShowDoodles
767+
*/
768+
setVisibleDoodles(shouldShowDoodles) {
769+
if (this.doodleCanvasController) {
770+
this.doodleCanvasController.showDoodles = shouldShowDoodles;
771+
}
772+
773+
this.visibleDoodles = shouldShowDoodles;
774+
if (this.toolbar) {
775+
this.toolbar.doodlesVisible = shouldShowDoodles;
776+
}
777+
}
778+
757779
/**
758780
* Set whether the document can be doodled on
759781
*

src/annotator/sidebar.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export default class Sidebar extends Guest {
135135
createAnnotation: () => this.createAnnotation(),
136136
setSidebarOpen: open => (open ? this.show() : this.hide()),
137137
setHighlightsVisible: show => this.setAllVisibleHighlights(show),
138+
setDoodlesVisible: show => this.setAllVisibleDoodles(show),
138139
setUserCanDoodle: show => this.setAllDoodleability(show),
139140
setDoodleOptions: options => this.setAllDoodleOptions(options),
140141
saveDoodle: () => this.saveDoodle(),
@@ -429,6 +430,15 @@ export default class Sidebar extends Guest {
429430
this.crossframe.call('setVisibleHighlights', shouldShowHighlights);
430431
}
431432

433+
/**
434+
* Hide or show doodles associated with annotations in the document.
435+
*
436+
* @param {boolean} shouldShowDoodles
437+
*/
438+
setAllVisibleDoodles(shouldShowDoodles) {
439+
this.crossframe.call('setVisibleDoodles', shouldShowDoodles);
440+
}
441+
432442
/**
433443
* (CreativeNTR) Toggle doodle ability on and off
434444
*

src/annotator/test/sidebar-test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,13 @@ describe('Sidebar', () => {
553553
assert.calledWith(fakeCrossFrame.call, 'setVisibleHighlights', true);
554554
}));
555555

556+
describe('#setAllVisibleDoodles', () =>
557+
it('sets the doodle state through crossframe and emits', () => {
558+
const sidebar = createSidebar({});
559+
sidebar.setAllVisibleDoodles(true);
560+
assert.calledWith(fakeCrossFrame.call, 'setVisibleDoodles', true);
561+
}));
562+
556563
it('hides toolbar controls when using the "clean" theme', () => {
557564
createSidebar({ theme: 'clean' });
558565
assert.equal(fakeToolbar.useMinimalControls, true);

src/annotator/toolbar.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Toolbar from './components/toolbar';
77
* @prop {() => any} createAnnotation
88
* @prop {(open: boolean) => any} setSidebarOpen
99
* @prop {(visible: boolean) => any} setHighlightsVisible
10+
* @prop {(visible: boolean) => any} setDoodlesVisible
1011
* @prop {(doodleable: boolean) => any} setUserCanDoodle
1112
* @prop {(doodleable: boolean) => any} setDoodleOptions
1213
* @prop {() => any} saveDoodle
@@ -28,6 +29,7 @@ export class ToolbarController {
2829
createAnnotation,
2930
setSidebarOpen,
3031
setHighlightsVisible,
32+
setDoodlesVisible,
3133
setUserCanDoodle,
3234
setDoodleOptions,
3335
saveDoodle,
@@ -46,11 +48,13 @@ export class ToolbarController {
4648
this._highlightsVisible = false;
4749
this._sidebarOpen = false;
4850
this._doodleable = false;
51+
this._doodlesVisible = false;
4952

5053
this._closeSidebar = () => setSidebarOpen(false);
5154
this._toggleSidebar = () => setSidebarOpen(!this._sidebarOpen);
5255
this._toggleHighlights = () =>
5356
setHighlightsVisible(!this._highlightsVisible);
57+
this._toggleDoodles = () => setDoodlesVisible(!this._doodlesVisible);
5458
this._toggleDoodleToolbar = () => {
5559
this._drawingToolbar = !this._drawingToolbar;
5660
this._doodleable = this._drawingToolbar;
@@ -129,6 +133,18 @@ export class ToolbarController {
129133
return this._highlightsVisible;
130134
}
131135

136+
/**
137+
* Update the toolbar to reflect whether doodles are currently visible
138+
*/
139+
set doodlesVisible(visible) {
140+
this._doodlesVisible = visible;
141+
this.render();
142+
}
143+
144+
get doodlesVisible() {
145+
return this._doodlesVisible;
146+
}
147+
132148
/**
133149
* Return the DOM element that toggles the sidebar's visibility.
134150
*
@@ -146,7 +162,9 @@ export class ToolbarController {
146162
newAnnotationType={this._newAnnotationType}
147163
isSidebarOpen={this._sidebarOpen}
148164
showHighlights={this._highlightsVisible}
165+
showDoodles={this._doodlesVisible}
149166
toggleHighlights={this._toggleHighlights}
167+
toggleDoodles={this._toggleDoodles}
150168
toggleSidebar={this._toggleSidebar}
151169
toggleSidebarRef={this._sidebarToggleButton}
152170
useMinimalControls={this.useMinimalControls}

src/doodle/displayCanvas.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import propTypes from 'prop-types';
55
* @typedef DisplayCanvasProps
66
* @prop {HTMLElement} container - Which element the DisplayCanvas should cover.
77
* @prop {Array<import('../types/api').Doodle>} doodles - An array of Doodles to render
8+
* @prop {Boolean} showDoodles - An array of Doodles to render
89
* @prop {(String) => any} handleDoodleClick - What to do when a doodle is clicked? Accepts the Doodle's `tag` as a prop
910
*/
1011

@@ -13,7 +14,12 @@ import propTypes from 'prop-types';
1314
*
1415
* @param {DisplayCanvasProps} props
1516
*/
16-
const DisplayCanvas = ({ container, doodles, handleDoodleClick }) => {
17+
const DisplayCanvas = ({
18+
container,
19+
doodles,
20+
handleDoodleClick,
21+
showDoodles,
22+
}) => {
1723
const boundingRect = container.getBoundingClientRect();
1824
return (
1925
<div
@@ -23,6 +29,7 @@ const DisplayCanvas = ({ container, doodles, handleDoodleClick }) => {
2329
left: boundingRect.left + window.scrollX,
2430
zIndex: 9998,
2531
pointerEvents: 'none',
32+
display: showDoodles ? undefined : 'none',
2633
}}
2734
>
2835
<Canvas
@@ -48,6 +55,7 @@ DisplayCanvas.propTypes = {
4855
doodles: propTypes.array.isRequired,
4956
container: propTypes.object.isRequired,
5057
handleDoodleClick: propTypes.func.isRequired,
58+
showDoodles: propTypes.bool.isRequired,
5159
};
5260

5361
export { DisplayCanvas };

src/doodle/doodleController.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,25 @@ export class DoodleController {
2020

2121
this._doodleable = false;
2222
this._handleDoodleClick = handleDoodleClick;
23+
this._showDoodles = false;
2324

2425
// create a new element to render into, to avoid overwriting the main page content.
2526
this.target = document.body.appendChild(document.createElement('div'));
2627

2728
this.render();
2829
}
2930

31+
/**
32+
* Re-render whenver doodle visibility changes
33+
*/
34+
set showDoodles(shouldShowDoodles) {
35+
this._showDoodles = shouldShowDoodles;
36+
this.render();
37+
}
38+
get showDoodles() {
39+
return this._showDoodles;
40+
}
41+
3042
/**
3143
* Update the toolbar to reflect whether the sidebar is open or not.
3244
*/
@@ -110,6 +122,7 @@ export class DoodleController {
110122
handleDoodleClick={this._handleDoodleClick}
111123
doodles={this.savedDoodles}
112124
container={this._container}
125+
showDoodles={this._showDoodles}
113126
/>
114127
</Fragment>,
115128
this.target

src/sidebar/services/frame-sync.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ export default function FrameSync(annotationsService, bridge, store) {
191191
bridge.on('setVisibleHighlights', function (state) {
192192
bridge.call('setVisibleHighlights', state);
193193
});
194+
bridge.on('setVisibleDoodles', function (state) {
195+
bridge.call('setVisibleDoodles', state);
196+
});
194197
bridge.on('setDoodleability', function (state) {
195198
bridge.call('setDoodleability', state);
196199
});

src/sidebar/services/test/frame-sync-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,12 @@ describe('sidebar/services/frame-sync', function () {
407407

408408
assert.calledWith(fakeBridge.call, 'setVisibleHighlights');
409409
});
410+
411+
it('calls "setVisibleDoodles"', function () {
412+
fakeBridge.emit('setVisibleDoodles');
413+
414+
assert.calledWith(fakeBridge.call, 'setVisibleDoodles');
415+
});
410416
});
411417

412418
describe('when annotations are focused in the sidebar', () => {

0 commit comments

Comments
 (0)