Skip to content

Commit d013d50

Browse files
authored
Merge pull request #169 from NDLANO/fix-navbutton-click
Fixes various nav button click issues
2 parents f9c2750 + 03d7874 commit d013d50

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

scripts/components/Interactions/NavigationButton.js

+22-15
Original file line numberDiff line numberDiff line change
@@ -282,18 +282,34 @@ export default class NavigationButton extends React.Component {
282282
return style;
283283
}
284284

285+
/**
286+
* Handle key down.
287+
* @param {KeyboardEvent} event Keyboard event.
288+
*/
289+
handleKeyDown(event) {
290+
if (!this.context.extras.isEditor) {
291+
return;
292+
}
293+
294+
if (event.code === 'Enter' || event.code === 'Space') {
295+
this.onDoubleClick();
296+
}
297+
}
298+
285299
/**
286300
* Handle click.
287301
*/
288302
onClick() {
303+
if (this.props.sceneIsDragging) {
304+
return;
305+
}
306+
289307
if (this.props.forceClickHandler || !this.context.extras.isEditor) {
290308
this.props.clickHandler();
291309

292310
// Reset button focus state when changing scenes or opening content
293311
this.setState({ innerButtonFocused: false });
294312
}
295-
296-
this.setFocus(true);
297313
}
298314

299315
/**
@@ -339,18 +355,10 @@ export default class NavigationButton extends React.Component {
339355
}
340356

341357
/**
342-
* @param {FocusEvent} event Event.
358+
* Handle focus.
343359
*/
344-
handleFocus(event) {
345-
if (this.context.extras.isEditor) {
346-
if (this.navButtonWrapper?.current === event.target) {
347-
this.setFocus();
348-
}
349-
350-
return;
351-
}
352-
353-
if (!this.context.extras.isEditor && this.props.onFocus) {
360+
handleFocus() {
361+
if (this.props.onFocus) {
354362
if (this.skipFocus) {
355363
this.skipFocus = false;
356364
}
@@ -489,6 +497,7 @@ export default class NavigationButton extends React.Component {
489497
tabIndex={isWrapperTabbable ? 0 : undefined}
490498
onFocus={this.handleFocus.bind(this)}
491499
onClick={this.onClick.bind(this)}
500+
onKeyDown={this.handleKeyDown.bind(this)}
492501
onBlur={this.onBlur.bind(this)}
493502
>
494503
{
@@ -498,7 +507,6 @@ export default class NavigationButton extends React.Component {
498507
style={{ height:'100%', width:'100%' }}
499508
ariaLabel={getLabelText(label) || this.context.l10n.untitled}
500509
tabIndexValue={isInnerButtonTabbable ? undefined : -1}
501-
onClickEvent={this.onClick.bind(this)}
502510
onDoubleClickEvent={this.onDoubleClick.bind(this)}
503511
onMouseDownEvent={this.onMouseDown.bind(this)}
504512
onFocusEvent={() => this.setState({ innerButtonFocused: true })}
@@ -517,7 +525,6 @@ export default class NavigationButton extends React.Component {
517525
aria-label={ getLabelText(label) || this.props.title }
518526
className='nav-button'
519527
tabIndex={isInnerButtonTabbable ? undefined : -1}
520-
onClick={this.onClick.bind(this)}
521528
onDoubleClick={this.onDoubleClick.bind(this)}
522529
onMouseDown={this.onMouseDown.bind(this)}
523530
onFocus={() => this.setState({ innerButtonFocused: true })}

scripts/components/Scene/SceneTypes/ThreeSixtyScene.js

+27-11
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export default class ThreeSixtyScene extends React.Component {
4444

4545
this.handleFirstRender = this.handleFirstRender.bind(this);
4646
this.handleSceneMoveStart = this.handleSceneMoveStart.bind(this);
47+
this.handleSceneMove = this.handleSceneMove.bind(this);
4748
this.handleSceneMoveStop = this.handleSceneMoveStop.bind(this);
4849

4950
this.terminateAffordance = this.terminateAffordance.bind(this);
@@ -242,6 +243,23 @@ export default class ThreeSixtyScene extends React.Component {
242243
}
243244
}
244245

246+
/**
247+
* Handle dragging scene ongoing.
248+
*/
249+
handleSceneMove() {
250+
if (this.context.extras.isEditor) {
251+
return;
252+
}
253+
254+
// Consider to be dragging after moving beyond maximum slack.
255+
const endPosition = this.props.threeSixty.getCurrentPosition();
256+
const sceneIsDragging =
257+
Math.abs(endPosition.yaw - this.startPosition?.yaw ?? 0) > ThreeSixtyScene.MAX_YAW_DELTA ||
258+
Math.abs(endPosition.pitch - this.startPosition?.pitch ?? 0) > ThreeSixtyScene.MAX_PITCH_DELTA;
259+
260+
this.setState({ sceneIsDragging: sceneIsDragging });
261+
}
262+
245263
/**
246264
* Handle dragging scene ended.
247265
* @param {H5P.Event} event Event.
@@ -250,6 +268,11 @@ export default class ThreeSixtyScene extends React.Component {
250268
if (this.context.extras.isEditor) {
251269
this.cancelPointerLock();
252270
}
271+
272+
window.requestAnimationFrame(() => {
273+
this.setState({ sceneIsDragging: false });
274+
});
275+
253276
this.context.trigger('movestop', event.data);
254277
}
255278

@@ -264,6 +287,7 @@ export default class ThreeSixtyScene extends React.Component {
264287
// Remove handlers.
265288
this.props.threeSixty.stopRendering();
266289
this.props.threeSixty.off('movestart', this.handleSceneMoveStart);
290+
this.props.threeSixty.off('move', this.handleSceneMove);
267291
this.props.threeSixty.off('movestop', this.handleSceneMoveStop);
268292
this.props.threeSixty.off('firstrender', this.handleFirstRender);
269293
}
@@ -315,6 +339,7 @@ export default class ThreeSixtyScene extends React.Component {
315339
threeSixty.update();
316340

317341
threeSixty.on('movestart', this.handleSceneMoveStart);
342+
threeSixty.on('move', this.handleSceneMove);
318343
threeSixty.on('movestop', this.handleSceneMoveStop);
319344

320345
// Add buttons to scene
@@ -518,6 +543,7 @@ export default class ThreeSixtyScene extends React.Component {
518543
:
519544
<NavigationButton
520545
key={key}
546+
sceneIsDragging={this.state.sceneIsDragging}
521547
staticScene={false}
522548
leftPosition={null}
523549
topPosition={null}
@@ -590,17 +616,6 @@ export default class ThreeSixtyScene extends React.Component {
590616
* @param {number} index Index of interaction linked to navigation button.
591617
*/
592618
handleNavButtonClick(index) {
593-
// Prevent click if user also dragged button beyond maximum slack.
594-
const endPosition = this.props.threeSixty.getCurrentPosition();
595-
if (
596-
Math.abs(endPosition.yaw - this.startPosition?.yaw) >
597-
ThreeSixtyScene.MAX_YAW_DELTA ||
598-
Math.abs(endPosition.pitch - this.startPosition?.pitch) >
599-
ThreeSixtyScene.MAX_PITCH_DELTA
600-
) {
601-
return; // Dragged button too much for click
602-
}
603-
604619
this.props.showInteraction.bind(this)(index);
605620
}
606621

@@ -657,6 +672,7 @@ export default class ThreeSixtyScene extends React.Component {
657672
// No longer active, indicate that scene must be updated
658673
this.props.threeSixty.stopRendering();
659674
this.props.threeSixty.off('movestart', this.handleSceneMoveStart);
675+
this.props.threeSixty.off('move', this.handleSceneMove);
660676
this.props.threeSixty.off('movestop', this.handleSceneMoveStop);
661677
this.props.threeSixty.off('firstrender', this.handleFirstRender);
662678
this.setState({

0 commit comments

Comments
 (0)