From 498f8efa6aca0aed68f39c0a0f1a3c20cfb2344f Mon Sep 17 00:00:00 2001 From: jivansh77 Date: Mon, 6 Jan 2025 23:49:55 +0530 Subject: [PATCH] Moved touch event handling to block.js --- js/block.js | 32 +++++++++++++++++++++++++++++++- js/piemenus.js | 40 ---------------------------------------- 2 files changed, 31 insertions(+), 41 deletions(-) diff --git a/js/block.js b/js/block.js index 6420103b6b..b8943c9c48 100644 --- a/js/block.js +++ b/js/block.js @@ -91,7 +91,7 @@ const STRINGLEN = 9; * Length of a long touch in milliseconds. * @type {number} */ -const LONGPRESSTIME = 1500; +const LONGPRESSTIME = 500; const INLINECOLLAPSIBLES = ["newnote", "interval", "osctime", "definemode"]; /** @@ -3077,6 +3077,36 @@ class Block { moved = false; }); + + this.container.on("touchstart", (event) => { + event.preventDefault(); + event.stopPropagation(); + that.blocks.mouseDownTime = new Date().getTime(); + that.blocks.longPressTimeout = setTimeout(() => { + that.blocks.activeBlock = that.blocks.blockList.indexOf(that); + piemenuBlockContext(that); + }, LONGPRESSTIME); + }, { passive: false }); + + this.container.on("touchstart", (event) => { + if (event.touches.length > 1) { + if (that.blocks.longPressTimeout) { + clearTimeout(that.blocks.longPressTimeout); + } + } + }); + + this.container.on("touchmove", () => { + if (that.blocks.longPressTimeout) { + clearTimeout(that.blocks.longPressTimeout); + } + }); + + this.container.on("touchend touchcancel", () => { + if (that.blocks.longPressTimeout) { + clearTimeout(that.blocks.longPressTimeout); + } + }); } /** diff --git a/js/piemenus.js b/js/piemenus.js index f7510245ed..bd978c0ba4 100644 --- a/js/piemenus.js +++ b/js/piemenus.js @@ -3514,46 +3514,6 @@ const piemenuBlockContext = (block) => { setTimeout(() => { that.blocks.stageClick = false; }, 500); - - // Long-press on blocks - let longPressTimer = null; - const LONG_PRESS_DURATION = 500; - - block.container.on("touchstart", (event) => { - event.preventDefault(); - event.stopPropagation(); - - if (event.touches.length !== 1) return; - - longPressTimer = setTimeout(() => { - docById("contextWheelDiv").style.position = "absolute"; - docById("contextWheelDiv").style.display = ""; - - const x = block.container.x; - const y = block.container.y; - const canvasLeft = block.activity.canvas.offsetLeft + 28 * block.blocks.blockScale; - const canvasTop = block.activity.canvas.offsetTop + 6 * block.blocks.blockScale; - - docById("contextWheelDiv").style.left = Math.round((x + block.activity.blocksContainer.x) * - block.activity.getStageScale() + canvasLeft) + "px"; - docById("contextWheelDiv").style.top = Math.round((y + block.activity.blocksContainer.y) * - block.activity.getStageScale() + canvasTop) + "px"; - }, LONG_PRESS_DURATION); - }); - - const clearTimer = () => { - if (longPressTimer) { - clearTimeout(longPressTimer); - longPressTimer = null; - } - }; - - block.container.on("touchmove", (event) => { - event.preventDefault(); - clearTimer(); - }); - block.container.on("touchend", clearTimer); - block.container.on("touchcancel", clearTimer); }; /**