From 5d7b31b486903e4c5a77a6e146f97738b04c8012 Mon Sep 17 00:00:00 2001 From: jivansh77 Date: Thu, 2 Jan 2025 13:41:22 +0530 Subject: [PATCH] Long-press for block menu --- js/activity.js | 28 ++++++++++++++++++++++++++-- js/blocks.js | 3 --- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/js/activity.js b/js/activity.js index 3c9d1d942e..3c77b3448d 100644 --- a/js/activity.js +++ b/js/activity.js @@ -508,13 +508,34 @@ class Activity { let longPressTimer = null; const LONG_PRESS_DURATION = 500; + // Function to check if coordinates are within any block + const isCoordinateOnBlock = (x, y) => { + if (!this.blocks || !this.blocks.blockList) return false; + + return this.blocks.blockList.some(block => { + if (!block || !block.container || block.trash) return false; + + const blockX = block.container.x + this.blocksContainer.x; + const blockY = block.container.y + this.blocksContainer.y; + + return (x >= blockX && + x <= blockX + block.width && + y >= blockY && + y <= blockY + block.height); + }); + }; + document.addEventListener("contextmenu", (event) => { event.preventDefault(); event.stopPropagation(); + const canvasRect = this.canvas.getBoundingClientRect(); + const x = event.clientX - canvasRect.left; + const y = event.clientY - canvasRect.top; + if (!this.beginnerMode && event.target.id === "myCanvas" && - !event.target.closest('.block')) { + !isCoordinateOnBlock(x, y)) { this._displayHelpfulWheel(event); } }, false); @@ -523,10 +544,13 @@ class Activity { if (event.touches.length !== 1) return; const touch = event.touches[0]; + const canvasRect = this.canvas.getBoundingClientRect(); + const x = touch.clientX - canvasRect.left; + const y = touch.clientY - canvasRect.top; if (!this.beginnerMode && touch.target.id === "myCanvas" && - !touch.target.closest('.block')) { + !isCoordinateOnBlock(x, y)) { longPressTimer = setTimeout(() => { const touchEvent = { diff --git a/js/blocks.js b/js/blocks.js index 5160d951d2..d1b12f60b6 100644 --- a/js/blocks.js +++ b/js/blocks.js @@ -5039,9 +5039,6 @@ class Blocks { } this.inLongPress = true; - - const helpfulWheel = docById("helpfulWheelDiv"); - helpfulWheel.style.display = "none"; piemenuBlockContext(this.blockList[this.activeBlock]); };