Skip to content

Commit

Permalink
Moved touch event handling to block.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jivansh77 committed Jan 6, 2025
1 parent 04850db commit 498f8ef
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 41 deletions.
32 changes: 31 additions & 1 deletion js/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

/**
Expand Down Expand Up @@ -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);
}
});
}

/**
Expand Down
40 changes: 0 additions & 40 deletions js/piemenus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand Down

0 comments on commit 498f8ef

Please sign in to comment.