Commit 26658d13 authored by Phil Hughes's avatar Phil Hughes

Merge branch '60006-add-touch-for-image-diff' into 'master'

Add touch events for diff viewers

Closes #60006

See merge request gitlab-org/gitlab-ce!26971
parents 0c1fffe2 513e4f99
...@@ -71,29 +71,39 @@ export default class ImageFile { ...@@ -71,29 +71,39 @@ export default class ImageFile {
// eslint-disable-next-line class-methods-use-this // eslint-disable-next-line class-methods-use-this
initDraggable($el, padding, callback) { initDraggable($el, padding, callback) {
var dragging = false; var dragging = false;
var $body = $('body'); const $body = $('body');
var $offsetEl = $el.parent(); const $offsetEl = $el.parent();
const dragStart = function() {
$el.off('mousedown').on('mousedown', function() {
dragging = true; dragging = true;
$body.css('user-select', 'none'); $body.css('user-select', 'none');
}); };
const dragStop = function() {
dragging = false;
$body.css('user-select', '');
};
const dragMove = function(e) {
const moveX = e.pageX || e.touches[0].pageX;
const left = moveX - ($offsetEl.offset().left + padding);
if (!dragging) return;
callback(e, left);
};
$el
.off('mousedown')
.off('touchstart')
.on('mousedown', dragStart)
.on('touchstart', dragStart);
$body $body
.off('mouseup') .off('mouseup')
.off('mousemove') .off('mousemove')
.on('mouseup', function() { .off('touchend')
dragging = false; .off('touchmove')
$body.css('user-select', ''); .on('mouseup', dragStop)
}) .on('touchend', dragStop)
.on('mousemove', function(e) { .on('mousemove', dragMove)
var left; .on('touchmove', dragMove);
if (!dragging) return;
left = e.pageX - ($offsetEl.offset().left + padding);
callback(e, left);
});
} }
prepareFrames(view) { prepareFrames(view) {
......
...@@ -40,12 +40,15 @@ export default { ...@@ -40,12 +40,15 @@ export default {
}, },
beforeDestroy() { beforeDestroy() {
document.body.removeEventListener('mouseup', this.stopDrag); document.body.removeEventListener('mouseup', this.stopDrag);
this.$refs.dragger.removeEventListener('mousedown', this.startDrag); document.body.removeEventListener('touchend', this.stopDrag);
document.body.removeEventListener('mousemove', this.dragMove);
document.body.removeEventListener('touchmove', this.dragMove);
}, },
methods: { methods: {
dragMove(e) { dragMove(e) {
if (!this.dragging) return; if (!this.dragging) return;
const left = e.pageX - this.$refs.dragTrack.getBoundingClientRect().left; const moveX = e.pageX || e.touches[0].pageX;
const left = moveX - this.$refs.dragTrack.getBoundingClientRect().left;
const dragTrackWidth = const dragTrackWidth =
this.$refs.dragTrack.clientWidth - this.$refs.dragger.clientWidth || 100; this.$refs.dragTrack.clientWidth - this.$refs.dragger.clientWidth || 100;
...@@ -60,11 +63,13 @@ export default { ...@@ -60,11 +63,13 @@ export default {
this.dragging = true; this.dragging = true;
document.body.style.userSelect = 'none'; document.body.style.userSelect = 'none';
document.body.addEventListener('mousemove', this.dragMove); document.body.addEventListener('mousemove', this.dragMove);
document.body.addEventListener('touchmove', this.dragMove);
}, },
stopDrag() { stopDrag() {
this.dragging = false; this.dragging = false;
document.body.style.userSelect = ''; document.body.style.userSelect = '';
document.body.removeEventListener('mousemove', this.dragMove); document.body.removeEventListener('mousemove', this.dragMove);
document.body.removeEventListener('touchmove', this.dragMove);
}, },
prepareOnionSkin() { prepareOnionSkin() {
if (this.onionOldImgInfo && this.onionNewImgInfo) { if (this.onionOldImgInfo && this.onionNewImgInfo) {
...@@ -82,6 +87,7 @@ export default { ...@@ -82,6 +87,7 @@ export default {
this.$refs.dragTrack.clientWidth - this.$refs.dragger.clientWidth || 100; this.$refs.dragTrack.clientWidth - this.$refs.dragger.clientWidth || 100;
document.body.addEventListener('mouseup', this.stopDrag); document.body.addEventListener('mouseup', this.stopDrag);
document.body.addEventListener('touchend', this.stopDrag);
} }
}, },
onionNewImgLoaded(imgInfo) { onionNewImgLoaded(imgInfo) {
...@@ -140,7 +146,14 @@ export default { ...@@ -140,7 +146,14 @@ export default {
</div> </div>
<div class="controls"> <div class="controls">
<div class="transparent"></div> <div class="transparent"></div>
<div ref="dragTrack" class="drag-track" @mousedown="startDrag" @mouseup="stopDrag"> <div
ref="dragTrack"
class="drag-track"
@mousedown="startDrag"
@mouseup="stopDrag"
@touchstart="startDrag"
@touchend="stopDrag"
>
<div ref="dragger" :style="{ left: onionDraggerPixelPos }" class="dragger"></div> <div ref="dragger" :style="{ left: onionDraggerPixelPos }" class="dragger"></div>
</div> </div>
<div class="opaque"></div> <div class="opaque"></div>
......
...@@ -46,6 +46,8 @@ export default { ...@@ -46,6 +46,8 @@ export default {
window.removeEventListener('resize', this.resizeThrottled, false); window.removeEventListener('resize', this.resizeThrottled, false);
document.body.removeEventListener('mouseup', this.stopDrag); document.body.removeEventListener('mouseup', this.stopDrag);
document.body.removeEventListener('mousemove', this.dragMove); document.body.removeEventListener('mousemove', this.dragMove);
document.body.removeEventListener('touchend', this.stopDrag);
document.body.removeEventListener('touchmove', this.dragMove);
}, },
mounted() { mounted() {
window.addEventListener('resize', this.resize, false); window.addEventListener('resize', this.resize, false);
...@@ -54,7 +56,8 @@ export default { ...@@ -54,7 +56,8 @@ export default {
dragMove(e) { dragMove(e) {
if (!this.dragging) return; if (!this.dragging) return;
let leftValue = e.pageX - this.$refs.swipeFrame.getBoundingClientRect().left; const moveX = e.pageX || e.touches[0].pageX;
let leftValue = moveX - this.$refs.swipeFrame.getBoundingClientRect().left;
const spaceLeft = 20; const spaceLeft = 20;
const { clientWidth } = this.$refs.swipeFrame; const { clientWidth } = this.$refs.swipeFrame;
if (leftValue <= 0) { if (leftValue <= 0) {
...@@ -69,10 +72,12 @@ export default { ...@@ -69,10 +72,12 @@ export default {
startDrag() { startDrag() {
this.dragging = true; this.dragging = true;
document.body.addEventListener('mousemove', this.dragMove); document.body.addEventListener('mousemove', this.dragMove);
document.body.addEventListener('touchmove', this.dragMove);
}, },
stopDrag() { stopDrag() {
this.dragging = false; this.dragging = false;
document.body.removeEventListener('mousemove', this.dragMove); document.body.removeEventListener('mousemove', this.dragMove);
document.body.removeEventListener('touchmove', this.dragMove);
}, },
prepareSwipe() { prepareSwipe() {
if (this.swipeOldImgInfo && this.swipeNewImgInfo) { if (this.swipeOldImgInfo && this.swipeNewImgInfo) {
...@@ -83,6 +88,7 @@ export default { ...@@ -83,6 +88,7 @@ export default {
Math.max(this.swipeOldImgInfo.renderedHeight, this.swipeNewImgInfo.renderedHeight) + 2; Math.max(this.swipeOldImgInfo.renderedHeight, this.swipeNewImgInfo.renderedHeight) + 2;
document.body.addEventListener('mouseup', this.stopDrag); document.body.addEventListener('mouseup', this.stopDrag);
document.body.addEventListener('touchend', this.stopDrag);
} }
}, },
swipeNewImgLoaded(imgInfo) { swipeNewImgLoaded(imgInfo) {
...@@ -143,6 +149,8 @@ export default { ...@@ -143,6 +149,8 @@ export default {
class="swipe-bar" class="swipe-bar"
@mousedown="startDrag" @mousedown="startDrag"
@mouseup="stopDrag" @mouseup="stopDrag"
@touchstart="startDrag"
@touchend="stopDrag"
> >
<span class="top-handle"></span> <span class="bottom-handle"></span> <span class="top-handle"></span> <span class="bottom-handle"></span>
</span> </span>
......
---
title: "Make touch events work on image diff swipe view and onion skin"
merge_request: 26971
author: ftab
type: added
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment