From a5779095f8f964ec3b66da026da8c7d9575726d6 Mon Sep 17 00:00:00 2001 From: kharhamel Date: Mon, 7 Jun 2021 17:48:39 +0200 Subject: [PATCH] FIX: cowebsite resize now works on touchscreens --- front/src/WebRtc/CoWebsiteManager.ts | 47 +++++++++++++++++++++++----- front/style/cowebsite.scss | 2 ++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/front/src/WebRtc/CoWebsiteManager.ts b/front/src/WebRtc/CoWebsiteManager.ts index 9793cd25..1fb28487 100644 --- a/front/src/WebRtc/CoWebsiteManager.ts +++ b/front/src/WebRtc/CoWebsiteManager.ts @@ -1,6 +1,7 @@ import {HtmlUtils} from "./HtmlUtils"; import {Subject} from "rxjs"; import {iframeListener} from "../Api/IframeListener"; +import {touchScreenManager} from "../Touch/TouchScreenManager"; enum iframeStates { closed = 1, @@ -17,6 +18,11 @@ const cowebsiteOpenFullScreenImageId = 'cowebsite-fullscreen-open'; const cowebsiteCloseFullScreenImageId = 'cowebsite-fullscreen-close'; const animationTime = 500; //time used by the css transitions, in ms. +interface TouchMoveCoordinates { + x: number; + y: number; +} + class CoWebsiteManager { private opened: iframeStates = iframeStates.closed; @@ -32,6 +38,7 @@ class CoWebsiteManager { private resizing: boolean = false; private cowebsiteMainDom: HTMLDivElement; private cowebsiteAsideDom: HTMLDivElement; + private previousTouchMoveCoordinates: TouchMoveCoordinates|null = null; //only use on touchscreens to track touch movement get width(): number { return this.cowebsiteDiv.clientWidth; @@ -62,7 +69,10 @@ class CoWebsiteManager { this.cowebsiteMainDom = HtmlUtils.getElementByIdOrFail(cowebsiteMainDomId); this.cowebsiteAsideDom = HtmlUtils.getElementByIdOrFail(cowebsiteAsideDomId); - this.initResizeListeners(); + if (touchScreenManager.supportTouchScreen) { + this.initResizeListeners(true); + } + this.initResizeListeners(false); const buttonCloseFrame = HtmlUtils.getElementByIdOrFail(cowebsiteCloseButtonId); buttonCloseFrame.addEventListener('click', () => { @@ -77,22 +87,43 @@ class CoWebsiteManager { }); } - private initResizeListeners() { - const movecallback = (event:MouseEvent) => { - this.verticalMode ? this.height += event.movementY / this.getDevicePixelRatio() : this.width -= event.movementX / this.getDevicePixelRatio(); + private initResizeListeners(touchMode:boolean) { + const movecallback = (event:MouseEvent|TouchEvent) => { + let x, y; + if (event.type === 'mousemove') { + x = (event as MouseEvent).movementX / this.getDevicePixelRatio(); + y = (event as MouseEvent).movementY / this.getDevicePixelRatio(); + } else { + const touchEvent = (event as TouchEvent).touches[0]; + const last = {x: touchEvent.pageX, y: touchEvent.pageY}; + const previous = this.previousTouchMoveCoordinates as TouchMoveCoordinates; + this.previousTouchMoveCoordinates = last; + x = last.x - previous.x; + y = last.y - previous.y; + } + + + this.verticalMode ? this.height += y : this.width -= x; this.fire(); } - this.cowebsiteAsideDom.addEventListener('mousedown', (event) => { + this.cowebsiteAsideDom.addEventListener( touchMode ? 'touchstart' : 'mousedown', (event) => { this.resizing = true; this.getIframeDom().style.display = 'none'; + if (touchMode) { + const touchEvent = (event as TouchEvent).touches[0]; + this.previousTouchMoveCoordinates = {x: touchEvent.pageX, y: touchEvent.pageY}; + } - document.addEventListener('mousemove', movecallback); + document.addEventListener(touchMode ? 'touchmove' : 'mousemove', movecallback); }); - document.addEventListener('mouseup', (event) => { + document.addEventListener(touchMode ? 'touchend' : 'mouseup', (event) => { if (!this.resizing) return; - document.removeEventListener('mousemove', movecallback); + if (touchMode) { + this.previousTouchMoveCoordinates = null; + } + document.removeEventListener(touchMode ? 'touchmove' : 'mousemove', movecallback); this.getIframeDom().style.display = 'block'; this.resizing = false; }); diff --git a/front/style/cowebsite.scss b/front/style/cowebsite.scss index 2ed1ef69..e508b58d 100644 --- a/front/style/cowebsite.scss +++ b/front/style/cowebsite.scss @@ -13,6 +13,8 @@ iframe { width: 100%; height: 100%; + max-width: 100vw; + max-height: 100vh; } }