User:Stumblean/common.js: Difference between revisions
No edit summary Tags: Mobile edit Mobile web edit Advanced mobile edit |
No edit summary Tags: Mobile edit Mobile web edit Advanced mobile edit |
||
Line 1: | Line 1: | ||
(function () { | (function () { | ||
// Utility function for constraining position within viewport | |||
function constrainPosition(x, y, width, height) { | |||
const winWidth = window.innerWidth; | |||
const winHeight = window.innerHeight; | |||
if (x < 0) x = 0; | |||
if (y < 0) y = 0; | |||
if (x + width > winWidth) x = winWidth - width; | |||
if (y + height > winHeight) y = winHeight - height; | |||
return [x, y]; | |||
} | |||
// Create toggle button with image icon | // Create toggle button with image icon | ||
const toggle = document.createElement("button"); | const toggle = document.createElement("button"); | ||
Line 8: | Line 19: | ||
right: "20px", | right: "20px", | ||
padding: "0", | padding: "0", | ||
background: "#2b2b2b", | background: "#2b2b2b", | ||
border: "none", | border: "none", | ||
borderRadius: "12px", | borderRadius: "12px", | ||
zIndex: 9998, | zIndex: 9998, | ||
cursor: " | cursor: "move", // show move cursor to indicate draggable | ||
userSelect: "none", | |||
touchAction: "none", | |||
}); | }); | ||
Line 22: | Line 35: | ||
height: "48px", | height: "48px", | ||
borderRadius: "12px", | borderRadius: "12px", | ||
display: "block", // | display: "block", | ||
pointerEvents: "none", // so dragging the button works smoothly | |||
}); | }); | ||
toggle.appendChild(icon); | toggle.appendChild(icon); | ||
Line 44: | Line 58: | ||
cursor: "move", | cursor: "move", | ||
userSelect: "none", | userSelect: "none", | ||
touchAction: "none", | touchAction: "none", | ||
}); | }); | ||
Line 53: | Line 67: | ||
fontSize: "18px", | fontSize: "18px", | ||
marginBottom: "10px", | marginBottom: "10px", | ||
userSelect: "text", | |||
cursor: "default", | |||
}); | }); | ||
Line 92: | Line 108: | ||
}; | }; | ||
// Toggle menu visibility when clicking toggle button | |||
toggle.onclick = () => { | toggle.onclick = () => { | ||
menu.style.display = menu.style.display === "none" ? "block" : "none"; | menu.style.display = menu.style.display === "none" ? "block" : "none"; | ||
}; | }; | ||
// | // Generic drag handlers for mouse and touch | ||
let isDragging = false; | function makeDraggable(element, excludeElements = []) { | ||
let isDragging = false; | |||
let dragStartX = 0; | |||
let dragStartY = 0; | |||
let elemStartLeft = 0; | |||
let elemStartTop = 0; | |||
function onDragStart(x, y, target) { | |||
// Ignore drag if started on excluded elements inside container | |||
if (excludeElements.includes(target)) return false; | |||
isDragging = true; | |||
dragStartX = x; | |||
dragStartY = y; | |||
const rect = element.getBoundingClientRect(); | |||
elemStartLeft = rect.left; | |||
elemStartTop = rect.top; | |||
return true; | |||
} | |||
function onDragMove(x, y) { | |||
if (!isDragging) return; | |||
let newLeft = elemStartLeft + (x - dragStartX); | |||
let newTop = elemStartTop + (y - dragStartY); | |||
[newLeft, newTop] = constrainPosition( | |||
newLeft, | |||
newTop, | |||
element.offsetWidth, | |||
element.offsetHeight | |||
); | |||
element.style.left = newLeft + "px"; | |||
element.style.top = newTop + "px"; | |||
element.style.bottom = "auto"; | |||
element.style.right = "auto"; | |||
} | |||
function onDragEnd() { | |||
isDragging = false; | |||
} | |||
if ( | // Mouse events | ||
element.addEventListener("mousedown", (e) => { | |||
if (!onDragStart(e.clientX, e.clientY, e.target)) return; | |||
e.preventDefault(); | |||
}); | |||
document.addEventListener("mousemove", (e) => onDragMove(e.clientX, e.clientY)); | |||
document.addEventListener("mouseup", onDragEnd); | |||
// Touch events | |||
element.addEventListener("touchstart", (e) => { | |||
if (!e.touches || !e.touches[0]) return; | |||
if (!onDragStart(e.touches[0].clientX, e.touches[0].clientY, e.target)) return; | |||
e.preventDefault(); | |||
}); | |||
document.addEventListener( | |||
"touchmove", | |||
(e) => { | |||
if (!e.touches || !e.touches[0]) return; | |||
onDragMove(e.touches[0].clientX, e.touches[0].clientY); | |||
}, | |||
{ passive: false } | |||
); | |||
document.addEventListener("touchend", onDragEnd); | |||
document.addEventListener("touchcancel", onDragEnd); | |||
} | } | ||
// Apply draggable to both toggle button and menu | |||
// For menu, exclude textarea and runBtn from drag start | |||
makeDraggable(toggle); | |||
makeDraggable(menu, [textarea, runBtn]); | |||
// | |||
// Assemble menu content | // Assemble menu content |