diff --git a/frontend/src/pages/content.jsx b/frontend/src/pages/content.jsx index fa458b5..13e5552 100644 --- a/frontend/src/pages/content.jsx +++ b/frontend/src/pages/content.jsx @@ -1,4 +1,4 @@ -import { useEffect, useRef, useState } from "react"; +import { useEffect, useLayoutEffect, useRef, useState } from "react"; import { createPortal } from "react-dom"; import { Link } from "react-router-dom"; import { api, fetchFile, fetchPaperless } from "../api.js"; @@ -1092,11 +1092,14 @@ export function ContentBoard({ window.addEventListener("keydown", onKey); return () => { window.removeEventListener("keydown", onKey); - document.body.classList.remove("is-carrying", "is-assigning"); + document.body.classList.remove("is-carrying", "is-assigning", "is-list-intent", "is-list-carry"); + rootRef.current?.querySelectorAll(".preview-row.is-carry-source, .preview-row.is-assign-target").forEach((row) => { + row.classList.remove("is-carry-source", "is-assign-target"); + }); }; }, []); - useEffect(() => { + useLayoutEffect(() => { const node = rootRef.current; if (!node) return undefined; function place(event) { @@ -1173,7 +1176,9 @@ export function ContentBoard({ carryRef.current = null; setIndicator(null); setCarrying(null); - document.body.classList.remove("is-carrying", "is-assigning"); + document.body.classList.remove("is-carrying", "is-assigning", "is-list-intent", "is-list-carry"); + carried.sourceRow?.classList.remove("is-carry-source"); + rootRef.current?.querySelectorAll(".preview-row.is-assign-target").forEach((row) => row.classList.remove("is-assign-target")); if (assign) placeUseRef.current?.(useId, carried); else if ((carried.source === "attachment" || carried.source === "evidence") && (section || event.target?.closest?.("[data-release-drop]"))) { releaseRef.current?.({ @@ -1202,6 +1207,7 @@ export function ContentBoard({ function blockRowMouseDown(event) { if (event.button !== 0 || !rowTarget(event)) return; event.preventDefault(); + if (!carryRef.current) armListCarryRef.current?.(event); } node.addEventListener("pointerdown", onPointerDownCapture, true); node.addEventListener("mousedown", blockRowMouseDown, true); @@ -1213,7 +1219,7 @@ export function ContentBoard({ node.removeEventListener("dragover", place); node.removeEventListener("drop", drop); }; - }, []); + }, [canEdit, picking, source.length]); async function saveMode(mode, nextDirection, nextView = viewMode) { setError(""); @@ -1598,9 +1604,10 @@ export function ContentBoard({ event.preventDefault(); event.stopPropagation(); const opener = target.closest("a.preview-row__title, a.preview-row__text, .use-chip__title"); + const sourceRow = target.closest(".preview-row"); const carried = { ...payload, - pointerId: event.pointerId, + pointerId: event.pointerId ?? 1, anchorX: event.clientX, anchorY: event.clientY, lastX: event.clientX, @@ -1609,8 +1616,33 @@ export function ContentBoard({ settled: false, useBindingId: "", release: false, - sourceNode: target.closest(".preview-row, .use-chip") || target, + sourceNode: sourceRow || target.closest(".use-chip") || target, + sourceRow, }; + function paintAssignTarget(bindingId) { + const root = rootRef.current; + if (!root) return; + root.querySelectorAll(".preview-row.is-assign-target").forEach((row) => row.classList.remove("is-assign-target")); + if (!bindingId) return; + root.querySelector(`.preview-row[data-binding="${bindingId}"]`)?.classList.add("is-assign-target"); + } + function beginCarryMotion() { + document.body.classList.add("is-carrying"); + if (carried.source === "list") { + document.body.classList.add("is-list-carry"); + carried.sourceRow?.classList.add("is-carry-source"); + } + setCarrying({ + source: carried.source, + bindingId: carried.bindingId || "", + hostBindingId: carried.hostBindingId || "", + }); + } + function clearCarryMotion() { + paintAssignTarget(""); + document.body.classList.remove("is-carrying", "is-assigning", "is-list-intent", "is-list-carry"); + carried.sourceRow?.classList.remove("is-carry-source"); + } function blockBrowserGesture(native) { native.preventDefault(); } @@ -1664,6 +1696,7 @@ export function ContentBoard({ carried.release = false; carried.place = false; carried.chapterId = ""; + paintAssignTarget(found.useId); setIndicator({ kind: "use", bindingId: found.useId }); return; } @@ -1672,11 +1705,13 @@ export function ContentBoard({ carried.release = true; carried.place = Boolean(found.place); carried.chapterId = found.chapterId || ""; + paintAssignTarget(""); setIndicator({ kind: "release", place: carried.place, chapterId: carried.chapterId, title: found.title || "" }); return; } if (sticky && carried.useBindingId && !(x || y)) { carried.release = false; + paintAssignTarget(carried.useBindingId); setIndicator({ kind: "use", bindingId: carried.useBindingId }); return; } @@ -1684,41 +1719,56 @@ export function ContentBoard({ carried.release = false; carried.place = false; carried.chapterId = ""; + paintAssignTarget(""); setIndicator(null); } function cleanup() { window.removeEventListener("pointermove", track, true); window.removeEventListener("pointerup", finish, true); + window.removeEventListener("mousemove", onMouseMove, true); + window.removeEventListener("mouseup", onMouseUp, true); window.removeEventListener("mousedown", blockBrowserGesture, true); - window.removeEventListener("dragstart", blockBrowserGesture, true); + window.removeEventListener("dragstart", onDragStart, true); window.removeEventListener("dragend", onDragEnd, true); window.removeEventListener("selectstart", blockBrowserGesture, true); - try { carried.captureNode?.releasePointerCapture?.(carried.pointerId); } catch { /* already released */ } - try { carried.sourceNode?.releasePointerCapture?.(carried.pointerId); } catch { /* already released */ } - document.body.classList.remove("is-carrying", "is-assigning"); + clearCarryMotion(); if (carryRef.current === carried) carryRef.current = null; setCarrying(null); setIndicator(null); } - function track(pointer) { - if (carryRef.current !== carried || (pointer.pointerId !== carried.pointerId && pointer.pointerId !== 0)) return; - if (pointer.clientX || pointer.clientY) { - carried.lastX = pointer.clientX; - carried.lastY = pointer.clientY; + function noteMove(x, y) { + if (x || y) { + carried.lastX = x; + carried.lastY = y; } - if (!carried.moved && Math.hypot(carried.lastX - carried.anchorX, carried.lastY - carried.anchorY) > 4) { + if (!carried.moved && Math.hypot(carried.lastX - carried.anchorX, carried.lastY - carried.anchorY) > 3) { carried.moved = true; if (carried.sourceNode) carried.sourceNode.dataset.dragged = "1"; - document.body.classList.add("is-carrying"); - setCarrying({ - source: carried.source, - bindingId: carried.bindingId || "", - hostBindingId: carried.hostBindingId || "", - }); + beginCarryMotion(); } - if (!carried.moved) return; - pointer.preventDefault(); + if (!carried.moved) return false; remember(carried.lastX, carried.lastY, false); + return true; + } + function track(pointer) { + if (carryRef.current !== carried || (pointer.pointerId !== carried.pointerId && pointer.pointerId !== 0)) return; + if (noteMove(pointer.clientX, pointer.clientY)) pointer.preventDefault(); + } + function onMouseMove(native) { + if (carryRef.current !== carried) return; + if (noteMove(native.clientX, native.clientY)) native.preventDefault(); + } + function onMouseUp(native) { + if (carryRef.current !== carried) return; + finish({ pointerId: carried.pointerId, clientX: native.clientX, clientY: native.clientY }); + } + function onDragStart(native) { + native.preventDefault(); + if (!carried.moved) { + carried.moved = true; + if (carried.sourceNode) carried.sourceNode.dataset.dragged = "1"; + beginCarryMotion(); + } } function finish(pointer) { if (pointer && pointer.pointerId !== carried.pointerId && pointer.pointerId !== 0) return; @@ -1766,16 +1816,16 @@ export function ContentBoard({ cleanup(); }; carryRef.current = carried; - const captureNode = target; - carried.captureNode = captureNode || null; - try { captureNode?.setPointerCapture?.(event.pointerId); } catch { /* the window listeners still follow the pointer */ } document.body.classList.add("is-assigning"); + if (carried.source === "list") document.body.classList.add("is-list-intent"); window.addEventListener("mousedown", blockBrowserGesture, true); - window.addEventListener("dragstart", blockBrowserGesture, true); + window.addEventListener("dragstart", onDragStart, true); window.addEventListener("dragend", onDragEnd, true); window.addEventListener("selectstart", blockBrowserGesture, true); window.addEventListener("pointermove", track, true); window.addEventListener("pointerup", finish, true); + window.addEventListener("mousemove", onMouseMove, true); + window.addEventListener("mouseup", onMouseUp, true); } armListCarryRef.current = (event) => { @@ -1995,7 +2045,7 @@ export function ContentBoard({ } return ( -