From fcd3c829c9c0f8b0ae5c0b8cf63cc9f67a2a66c1 Mon Sep 17 00:00:00 2001 From: Andrew Telnov Date: Thu, 28 Dec 2023 14:16:34 +0200 Subject: [PATCH 1/3] Check for mouse support #7584 --- src/utils/devices.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/utils/devices.ts b/src/utils/devices.ts index 7409204622..54e0434d86 100644 --- a/src/utils/devices.ts +++ b/src/utils/devices.ts @@ -40,7 +40,10 @@ export function getIsTouch() { return _isTouch; } -export let IsTouch = IsMobile && getIsTouch(); +const pointerMatches = (!!matchMedia && matchMedia("(pointer:fine)")) || undefined; +const hasMouse = !!pointerMatches && !!pointerMatches.matches; + +export let IsTouch = !hasMouse && getIsTouch(); //for tests export function _setIsTouch(val: boolean): void { From 83946518eb694709b52e2d04df5c6070e45b4cbb Mon Sep 17 00:00:00 2001 From: Andrew Telnov Date: Thu, 28 Dec 2023 14:42:44 +0200 Subject: [PATCH 2/3] Add unit test #7584 --- src/utils/devices.ts | 23 ++++++++++++----------- tests/utilstests.ts | 26 +++++++++++++++++++------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/utils/devices.ts b/src/utils/devices.ts index 54e0434d86..864fa9ef19 100644 --- a/src/utils/devices.ts +++ b/src/utils/devices.ts @@ -30,20 +30,21 @@ let _IPad = false; export const IsMobile = _isMobile || _IPad; -export function getIsTouch() { - let _isTouch = false; - - if (typeof window !== "undefined") { - _isTouch = "ontouchstart" in (window) || navigator.maxTouchPoints > 0; - } - - return _isTouch; -} +export var mouseInfo = { + get isTouch(): boolean { + if(this.hasMouse) return false; + return this.hasTouchEvent; + }, + get hasTouchEvent(): boolean { + return typeof window !== "undefined" && "ontouchstart" in (window) || navigator.maxTouchPoints > 0; + }, + hasMouse: true +}; const pointerMatches = (!!matchMedia && matchMedia("(pointer:fine)")) || undefined; -const hasMouse = !!pointerMatches && !!pointerMatches.matches; +mouseInfo.hasMouse = !!pointerMatches && !!pointerMatches.matches; -export let IsTouch = !hasMouse && getIsTouch(); +export let IsTouch = mouseInfo.isTouch; //for tests export function _setIsTouch(val: boolean): void { diff --git a/tests/utilstests.ts b/tests/utilstests.ts index 8b90cf1ce5..b14e70859c 100644 --- a/tests/utilstests.ts +++ b/tests/utilstests.ts @@ -1,8 +1,7 @@ import { IAction } from "../src/actions/action"; import { defaultListCss } from "../src/list"; -import { Question } from "../src/question"; import { createSvg, doKey2ClickDown, doKey2ClickUp, sanitizeEditableContent } from "../src/utils/utils"; -import { getIsTouch } from "../src/utils/devices"; +import { mouseInfo } from "../src/utils/devices"; export default QUnit.module("utils"); function checkSanitizer(element, text, selectionNodeIndex, selectionStart) { @@ -157,11 +156,24 @@ QUnit.test( } ); -QUnit.skip( - "utils: devices: getIsTouch", +QUnit.test( + "utils: devices: isTouch", function (assert) { - assert.equal(getIsTouch(), false, "getIsTouch() return false for 'mouse' screens"); - window["ontouchstart"] = ()=>{}; - assert.equal(getIsTouch(), true, "getIsTouch() return true for 'touch' screens"); + mouseInfo.hasMouse = true; + assert.equal(mouseInfo.isTouch, false, "isTouch, #1"); + mouseInfo.hasMouse = false; + const hasTouchEvent = mouseInfo.hasTouchEvent; + assert.equal(mouseInfo.isTouch, hasTouchEvent, "isTouch, #2. hasTouch in window: " + hasTouchEvent); + if(!hasTouchEvent) { + window["ontouchstart"] = ()=>{}; + } + mouseInfo.hasMouse = true; + assert.equal(mouseInfo.isTouch, false, "isTouch, #3"); + mouseInfo.hasMouse = false; + assert.equal(mouseInfo.isTouch, true, "isTouch, #4"); + mouseInfo.hasMouse = true; + if(!hasTouchEvent) { + window["ontouchstart"] = undefined; + } } ); \ No newline at end of file From 3f5bf0d7f9fbf0d7cb628e1a7fdaf7b84c4bfa8c Mon Sep 17 00:00:00 2001 From: Andrew Telnov Date: Thu, 28 Dec 2023 14:47:34 +0200 Subject: [PATCH 3/3] Refactor mouseInfo #7584 --- src/utils/devices.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/utils/devices.ts b/src/utils/devices.ts index 864fa9ef19..91ad43a7e8 100644 --- a/src/utils/devices.ts +++ b/src/utils/devices.ts @@ -32,11 +32,10 @@ export const IsMobile = _isMobile || _IPad; export var mouseInfo = { get isTouch(): boolean { - if(this.hasMouse) return false; - return this.hasTouchEvent; + return !this.hasMouse && this.hasTouchEvent; }, get hasTouchEvent(): boolean { - return typeof window !== "undefined" && "ontouchstart" in (window) || navigator.maxTouchPoints > 0; + return typeof window !== "undefined" && ("ontouchstart" in (window) || navigator.maxTouchPoints > 0); }, hasMouse: true };