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 |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
(function () { | (function () { | ||
function constrainPosition(x, y, width, height) { | function constrainPosition(x, y, width, height) { | ||
const winWidth = window.innerWidth; | const winWidth = window.innerWidth; | ||
Line 11: | Line 10: | ||
} | } | ||
const toggle = document.createElement("button"); | const toggle = document.createElement("button"); | ||
toggle.id = "delta-toggle"; | toggle.id = "delta-toggle"; | ||
Line 23: | Line 21: | ||
borderRadius: "12px", | borderRadius: "12px", | ||
zIndex: 9998, | zIndex: 9998, | ||
cursor: "move", | cursor: "move", | ||
userSelect: "none", | userSelect: "none", | ||
touchAction: "none", | touchAction: "none", | ||
Line 36: | Line 34: | ||
borderRadius: "12px", | borderRadius: "12px", | ||
display: "block", | display: "block", | ||
pointerEvents: "none", | pointerEvents: "none", | ||
}); | }); | ||
toggle.appendChild(icon); | toggle.appendChild(icon); | ||
const menu = document.createElement("div"); | const menu = document.createElement("div"); | ||
menu.id = "delta-menu"; | menu.id = "delta-menu"; | ||
Line 108: | Line 105: | ||
}; | }; | ||
function makeDraggable(element, excludeElements = [], onClick) { | |||
function makeDraggable(element, excludeElements = []) { | |||
let isDragging = false; | let isDragging = false; | ||
let dragStartX = 0; | let dragStartX = 0; | ||
Line 120: | Line 111: | ||
let elemStartLeft = 0; | let elemStartLeft = 0; | ||
let elemStartTop = 0; | let elemStartTop = 0; | ||
let moved = false; | |||
function onDragStart(x, y, target) { | function onDragStart(x, y, target) { | ||
if (excludeElements.includes(target)) return false; | if (excludeElements.includes(target)) return false; | ||
isDragging = true; | isDragging = true; | ||
moved = false; | |||
dragStartX = x; | dragStartX = x; | ||
dragStartY = y; | dragStartY = y; | ||
Line 135: | Line 127: | ||
function onDragMove(x, y) { | function onDragMove(x, y) { | ||
if (!isDragging) return; | if (!isDragging) return; | ||
let newLeft = elemStartLeft + | const dx = x - dragStartX; | ||
let newTop = elemStartTop + | const dy = y - dragStartY; | ||
if (Math.abs(dx) > 3 || Math.abs(dy) > 3) moved = true; | |||
let newLeft = elemStartLeft + dx; | |||
let newTop = elemStartTop + dy; | |||
[newLeft, newTop] = constrainPosition( | [newLeft, newTop] = constrainPosition( | ||
newLeft, | newLeft, | ||
Line 150: | Line 146: | ||
function onDragEnd() { | function onDragEnd() { | ||
if (!isDragging) return; | |||
isDragging = false; | isDragging = false; | ||
if (!moved && typeof onClick === "function") { | |||
onClick(); | |||
} | |||
} | } | ||
element.addEventListener("mousedown", (e) => { | element.addEventListener("mousedown", (e) => { | ||
if (!onDragStart(e.clientX, e.clientY, e.target)) return; | if (!onDragStart(e.clientX, e.clientY, e.target)) return; | ||
Line 161: | Line 160: | ||
document.addEventListener("mouseup", onDragEnd); | document.addEventListener("mouseup", onDragEnd); | ||
element.addEventListener("touchstart", (e) => { | element.addEventListener("touchstart", (e) => { | ||
if (!e.touches || !e.touches[0]) return; | if (!e.touches || !e.touches[0]) return; | ||
Line 179: | Line 177: | ||
} | } | ||
function toggleMenu() { | |||
menu.style.display = menu.style.display === "none" ? "block" : "none"; | |||
makeDraggable(toggle); | } | ||
makeDraggable(toggle, [], toggleMenu); | |||
makeDraggable(menu, [textarea, runBtn]); | makeDraggable(menu, [textarea, runBtn]); | ||
menu.appendChild(title); | menu.appendChild(title); | ||
menu.appendChild(textarea); | menu.appendChild(textarea); |