User:Stumblean/common.js: Difference between revisions

Stumblean (talk | contribs)
No edit summary
Tags: Mobile edit Mobile web edit Advanced mobile edit
Stumblean (talk | contribs)
No edit summary
Tags: Mobile edit Mobile web edit Advanced mobile edit
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
(function () {
(function () {
   // Create toggle button with image icon
   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];
  }
 
   const toggle = document.createElement("button");
   const toggle = document.createElement("button");
   toggle.id = "delta-toggle";
   toggle.id = "delta-toggle";
Line 8: Line 17:
     right: "20px",
     right: "20px",
     padding: "0",
     padding: "0",
     background: "#2b2b2b", // Matching menu background color
     background: "#2b2b2b",
     border: "none",
     border: "none",
     borderRadius: "12px", // Rounded square
     borderRadius: "12px",
     zIndex: 9998,
     zIndex: 9998,
     cursor: "pointer"
     cursor: "move",
    userSelect: "none",
    touchAction: "none",
   });
   });


Line 22: Line 33:
     height: "48px",
     height: "48px",
     borderRadius: "12px",
     borderRadius: "12px",
     display: "block" // Removes small spacing around img inside button
     display: "block",
    pointerEvents: "none",
   });
   });
   toggle.appendChild(icon);
   toggle.appendChild(icon);


  // Create Delta menu
   const menu = document.createElement("div");
   const menu = document.createElement("div");
   menu.id = "delta-menu";
   menu.id = "delta-menu";
Line 43: Line 54:
     fontFamily: "monospace",
     fontFamily: "monospace",
     cursor: "move",
     cursor: "move",
     userSelect: "none"
     userSelect: "none",
    touchAction: "none",
   });
   });


Line 51: Line 63:
     textAlign: "center",
     textAlign: "center",
     fontSize: "18px",
     fontSize: "18px",
     marginBottom: "10px"
     marginBottom: "10px",
    userSelect: "text",
    cursor: "default",
   });
   });


Line 67: Line 81:
     boxSizing: "border-box",
     boxSizing: "border-box",
     cursor: "auto",
     cursor: "auto",
     userSelect: "text"
     userSelect: "text",
   });
   });


Line 80: Line 94:
     border: "none",
     border: "none",
     borderRadius: "6px",
     borderRadius: "6px",
     cursor: "pointer"
     cursor: "pointer",
   });
   });


Line 91: Line 105:
   };
   };


   toggle.onclick = () => {
   function makeDraggable(element, excludeElements = [], onClick) {
     menu.style.display = menu.style.display === "none" ? "block" : "none";
    let isDragging = false;
  };
     let dragStartX = 0;
    let dragStartY = 0;
    let elemStartLeft = 0;
    let elemStartTop = 0;
    let moved = false;


  // Drag functionality
    function onDragStart(x, y, target) {
  let isDragging = false;
      if (excludeElements.includes(target)) return false;
  let dragStartX = 0;
      isDragging = true;
  let dragStartY = 0;
      moved = false;
  let menuStartLeft = 0;
      dragStartX = x;
  let menuStartTop = 0;
      dragStartY = y;
      const rect = element.getBoundingClientRect();
      elemStartLeft = rect.left;
      elemStartTop = rect.top;
      return true;
    }


  menu.addEventListener("mousedown", (e) => {
    function onDragMove(x, y) {
    // Only drag if clicked outside textarea and buttons
      if (!isDragging) return;
    if (e.target === textarea || e.target === runBtn) return;
      const dx = x - dragStartX;
      const dy = y - dragStartY;
      if (Math.abs(dx) > 3 || Math.abs(dy) > 3) moved = true;


    isDragging = true;
      let newLeft = elemStartLeft + dx;
    dragStartX = e.clientX;
      let newTop = elemStartTop + dy;
    dragStartY = e.clientY;
      [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";
    }


     // Compute current left/top relative to viewport
     function onDragEnd() {
    const rect = menu.getBoundingClientRect();
      if (!isDragging) return;
    menuStartLeft = rect.left;
      isDragging = false;
    menuStartTop = rect.top;
      if (!moved && typeof onClick === "function") {
        onClick();
      }
    }


     // Prevent text selection while dragging
     element.addEventListener("mousedown", (e) => {
    e.preventDefault();
      if (!onDragStart(e.clientX, e.clientY, e.target)) return;
  });
      e.preventDefault();
    });
    document.addEventListener("mousemove", (e) => onDragMove(e.clientX, e.clientY));
    document.addEventListener("mouseup", onDragEnd);


  document.addEventListener("mousemove", (e) => {
    element.addEventListener("touchstart", (e) => {
    if (!isDragging) return;
      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);
  }


    const dx = e.clientX - dragStartX;
  function toggleMenu() {
    const dy = e.clientY - dragStartY;
     menu.style.display = menu.style.display === "none" ? "block" : "none";
 
  }
    let newLeft = menuStartLeft + dx;
    let newTop = menuStartTop + dy;
 
    // Constrain within viewport (optional)
     const winWidth = window.innerWidth;
    const winHeight = window.innerHeight;
    const menuRect = menu.getBoundingClientRect();


    if (newLeft < 0) newLeft = 0;
  makeDraggable(toggle, [], toggleMenu);
    if (newTop < 0) newTop = 0;
  makeDraggable(menu, [textarea, runBtn]);
    if (newLeft + menuRect.width > winWidth) newLeft = winWidth - menuRect.width;
    if (newTop + menuRect.height > winHeight) newTop = winHeight - menuRect.height;
 
    menu.style.left = newLeft + "px";
    menu.style.top = newTop + "px";
    menu.style.bottom = "auto"; // Reset bottom so top/left positioning works
    menu.style.right = "auto";  // Reset right for same reason
  });
 
  document.addEventListener("mouseup", () => {
    isDragging = false;
  });


  // Assemble menu content
   menu.appendChild(title);
   menu.appendChild(title);
   menu.appendChild(textarea);
   menu.appendChild(textarea);