Skip to content
This repository was archived by the owner on Jan 19, 2023. It is now read-only.

Commit 210e934

Browse files
committed
Code style refactor
1 parent 6958d43 commit 210e934

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

src/react-image-lightbox.js

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class ReactImageLightbox extends Component {
170170
this.listenersAttached = true;
171171
}
172172
}
173-
173+
174174
// Change zoom level
175175
changeZoom(zoomLevel, clientX, clientY) {
176176
const windowWidth = getWindowWidth();
@@ -554,31 +554,48 @@ class ReactImageLightbox extends Component {
554554
}
555555
}
556556

557+
// Handle move start over the lightbox container
558+
// This happens:
559+
// - On a mouseDown event
560+
// - On a touchstart event
561+
handleMoveStart(clientX, clientY) {
562+
// Only allow dragging when zoomed
563+
if (this.state.zoomLevel <= MIN_ZOOM_LEVEL) {
564+
return;
565+
}
566+
567+
this.isDragging = true;
568+
this.dragStartX = clientX;
569+
this.dragStartY = clientY;
570+
this.dragStartOffsetX = this.state.offsetX;
571+
this.dragStartOffsetY = this.state.offsetY;
572+
}
573+
557574
// Handle the mouse clicking down in the lightbox container
558575
handleOuterMouseDown(event) {
559576
event.preventDefault();
577+
this.handleMoveStart(event.clientX, event.clientY);
578+
}
560579

561-
// Allow dragging when zoomed
562-
if (this.state.zoomLevel > MIN_ZOOM_LEVEL) {
563-
this.isDragging = true;
564-
this.dragStartX = event.clientX;
565-
this.dragStartY = event.clientY;
566-
this.dragStartOffsetX = this.state.offsetX;
567-
this.dragStartOffsetY = this.state.offsetY;
568-
}
580+
// Touch screen version of handleOuterMouseDown()
581+
handleOuterTouchStart(event) {
582+
const touchObj = event.changedTouches[0];
583+
this.handleMoveStart(parseInt(touchObj.clientX, 10), parseInt(touchObj.clientY, 10));
569584
}
570585

571-
// Handle the mouse dragging over the lightbox container
572-
// (after a mouseDown and before a mouseUp event)
573-
handleOuterMouseMove(event) {
586+
// Handle dragging over the lightbox container
587+
// This happens:
588+
// - After a mouseDown and before a mouseUp event
589+
// - After a touchstart and before a touchend event
590+
handleMove(clientX, clientY) {
574591
if (!this.isDragging) {
575592
return;
576593
}
577594

578595
const zoomMultiplier = this.getZoomMultiplier();
579596

580-
const newOffsetX = (this.dragStartX - event.clientX) / zoomMultiplier + this.dragStartOffsetX;
581-
const newOffsetY = (this.dragStartY - event.clientY) / zoomMultiplier + this.dragStartOffsetY;
597+
const newOffsetX = (this.dragStartX - clientX) / zoomMultiplier + this.dragStartOffsetX;
598+
const newOffsetY = (this.dragStartY - clientY) / zoomMultiplier + this.dragStartOffsetY;
582599
if (this.state.offsetX !== newOffsetX || this.state.offsetY !== newOffsetY) {
583600
this.setState({
584601
offsetX: newOffsetX,
@@ -587,37 +604,23 @@ class ReactImageLightbox extends Component {
587604
}
588605
}
589606

590-
// Touch screen version of handleOuterMouseDown()
591-
handleOuterTouchStart(event) {
592-
// Allow dragging when zoomed
593-
if (this.state.zoomLevel > MIN_ZOOM_LEVEL) {
594-
this.isDragging = true;
595-
let touchObj = event.changedTouches[0];
596-
this.dragStartX = parseInt(touchObj.clientX);
597-
this.dragStartY = parseInt(touchObj.clientY);
598-
this.dragStartOffsetX = this.state.offsetX;
599-
this.dragStartOffsetY = this.state.offsetY;
600-
}
607+
// Handle the mouse dragging over the lightbox container
608+
// (after a mouseDown and before a mouseUp event)
609+
handleOuterMouseMove(event) {
610+
this.handleMove(event.clientX, event.clientY);
601611
}
602612

603613
// Touch screen version of handleOuterMouseMove()
604614
handleOuterTouchMove(event) {
605615
event.preventDefault();
606616

607-
if(this.state.zoomLevel <= MIN_ZOOM_LEVEL) // We shouldn't go any further if we're not zoomed
617+
// We shouldn't go any further if we're not zoomed
618+
if (this.state.zoomLevel <= MIN_ZOOM_LEVEL) {
608619
return;
609-
610-
const zoomMultiplier = this.getZoomMultiplier();
611-
612-
const touchObj = event.changedTouches[0];
613-
const newOffsetX = (this.dragStartX - parseInt(touchObj.clientX)) / zoomMultiplier + this.dragStartOffsetX;
614-
const newOffsetY = (this.dragStartY - parseInt(touchObj.clientY)) / zoomMultiplier + this.dragStartOffsetY;
615-
if (this.state.offsetX !== newOffsetX || this.state.offsetY !== newOffsetY) {
616-
this.setState({
617-
offsetX: newOffsetX,
618-
offsetY: newOffsetY,
619-
});
620620
}
621+
622+
const touchObj = event.changedTouches[0];
623+
this.handleMove(parseInt(touchObj.clientX, 10), parseInt(touchObj.clientY, 10));
621624
}
622625

623626
// Handle the window resize event

0 commit comments

Comments
 (0)