CI / go (Server) (push) Failing after 4m16s
Docs / build (push) Canceled after 0s
Docs / deploy (push) Canceled after 0s
CI / go (Web-App) (push) Successful in 10m49s
CI / frontend (push) Failing after 7m26s
CI / desktop (ubuntu-latest) (push) Successful in 1h43m36s
CI / mobile (push) Failing after 3m46s
CI / docker (push) Successful in 58m32s
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
- Touch dragging (database/touchdrag.ts): rows drag by their grip, board cards by a grip shown on touch screens, calendar entries by themselves. The handle takes the touch from its start, since Android's long press cancels a held touch; a tap still clicks and holding still is the long press. The phone's editor WebView claims touches at once. - Editor bridge: toolbar commands and state follow focus into an editor inside a panel (a record's notes) and back to the page. - Phone-App widget tests: fixtures gain the fields the API added since (journal, tags, properties, journalDate, createdBy, lastEditedBy). - Guide: how to drag records on a phone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
181 lines
6.3 KiB
TypeScript
181 lines
6.3 KiB
TypeScript
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// Dragging records with a finger. HTML drag and drop starts only from a
|
|
// mouse, and a press held on a phone becomes the browser's long press, which
|
|
// cancels the touch. So a record drags from a handle that takes the touch
|
|
// from its start (no scrolling from it): the row's grip, the card's grip, a
|
|
// calendar entry. A tap on the handle still clicks it, and holding it still
|
|
// is the long press (the record's menu).
|
|
|
|
/** How long a still press counts as a long press, in ms. */
|
|
export const HOLD_MS = 500;
|
|
/** How far a finger may wander and still count as still, in px. */
|
|
const SLOP = 6;
|
|
/** Distance from a scroll area's edge at which dragging scrolls it, in px. */
|
|
const EDGE = 48;
|
|
|
|
export interface TouchDrag {
|
|
/** The element that moves: its size places the copy that follows the finger. */
|
|
source: HTMLElement;
|
|
/** What follows the finger while dragging (positioned by touchDraggable). */
|
|
ghost(): HTMLElement;
|
|
/** The finger is over target: show where the record would land. */
|
|
over(target: Element | null, x: number, y: number): void;
|
|
/** The record was let go over target. */
|
|
drop(target: Element | null, x: number, y: number): void;
|
|
/** Dragging stopped, dropped or not: clear what over() showed. */
|
|
end(): void;
|
|
/** Held still and let go: the long press. */
|
|
hold?(): void;
|
|
}
|
|
|
|
/** Lets a finger drag d.source by handle. Mouse input is left to HTML drag and drop. */
|
|
export function touchDraggable(handle: HTMLElement, d: TouchDrag): void {
|
|
handle.classList.add("dw-db-touch-handle");
|
|
let active = false;
|
|
let touchId = -1;
|
|
let sx = 0;
|
|
let sy = 0;
|
|
let x = 0;
|
|
let y = 0;
|
|
let start = 0;
|
|
let pressed: Element | null = null;
|
|
let ghost: HTMLElement | null = null;
|
|
let frame = 0;
|
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
// The last touch ended a moment ago: the browser's late events are ours.
|
|
let ended = 0;
|
|
|
|
const target = () => document.elementFromPoint(x, y);
|
|
const reset = () => {
|
|
clearTimeout(timer);
|
|
cancelAnimationFrame(frame);
|
|
if (ghost) {
|
|
ghost.remove();
|
|
ghost = null;
|
|
d.end();
|
|
}
|
|
d.source.classList.remove("is-held", "is-dragging");
|
|
active = false;
|
|
touchId = -1;
|
|
ended = Date.now();
|
|
};
|
|
// Near the edge of a scroll area (the board, the page), scroll it.
|
|
const scroll = () => {
|
|
if (!ghost) return;
|
|
const dx = x < EDGE ? -1 : x > innerWidth - EDGE ? 1 : 0;
|
|
const dy = y < EDGE ? -1 : y > innerHeight - EDGE ? 1 : 0;
|
|
let moved = false;
|
|
for (const [axis, dir] of [["x", dx], ["y", dy]] as const) {
|
|
if (!dir) continue;
|
|
const s = scroller(d.source, axis) ?? (axis === "y" ? document.scrollingElement : null);
|
|
if (!s) continue;
|
|
const key = axis === "x" ? "scrollLeft" : "scrollTop";
|
|
const before = s[key];
|
|
s[key] += dir * 12;
|
|
moved ||= s[key] !== before;
|
|
}
|
|
if (moved) d.over(target(), x, y);
|
|
frame = requestAnimationFrame(scroll);
|
|
};
|
|
const touchOf = (e: TouchEvent) => [...e.changedTouches].find((t) => t.identifier === touchId);
|
|
|
|
handle.addEventListener(
|
|
"touchstart",
|
|
(e) => {
|
|
if (active || e.touches.length !== 1) return;
|
|
const t = e.changedTouches[0]!;
|
|
// Fields inside the handle keep their touch (not the editor around the database).
|
|
const field = (e.target as Element).closest("input, textarea, select, [contenteditable='true']");
|
|
if (field && handle.contains(field)) return;
|
|
// Ours from the start: no scrolling, no long-press gesture, no mouse events.
|
|
e.preventDefault();
|
|
active = true;
|
|
touchId = t.identifier;
|
|
sx = x = t.clientX;
|
|
sy = y = t.clientY;
|
|
start = Date.now();
|
|
pressed = e.target as Element;
|
|
timer = setTimeout(() => {
|
|
d.source.classList.add("is-held");
|
|
navigator.vibrate?.(10);
|
|
}, HOLD_MS);
|
|
},
|
|
{ passive: false },
|
|
);
|
|
handle.addEventListener(
|
|
"touchmove",
|
|
(e) => {
|
|
const t = active && touchOf(e);
|
|
if (!t) return;
|
|
e.preventDefault();
|
|
x = t.clientX;
|
|
y = t.clientY;
|
|
if (!ghost) {
|
|
if (Math.hypot(x - sx, y - sy) <= SLOP) return;
|
|
clearTimeout(timer);
|
|
d.source.classList.remove("is-held");
|
|
ghost = d.ghost();
|
|
const box = d.source.getBoundingClientRect();
|
|
Object.assign(ghost.style, {
|
|
position: "fixed",
|
|
left: `${box.left}px`,
|
|
top: `${box.top}px`,
|
|
// A table row can be wider than the screen.
|
|
width: `${Math.max(80, Math.min(box.width, innerWidth - Math.max(box.left, 0) - 8))}px`,
|
|
margin: "0",
|
|
pointerEvents: "none",
|
|
zIndex: "1000",
|
|
});
|
|
ghost.classList.add("dw-db-ghost");
|
|
document.body.append(ghost);
|
|
d.source.classList.add("is-dragging");
|
|
frame = requestAnimationFrame(scroll);
|
|
}
|
|
ghost.style.transform = `translate(${x - sx}px, ${y - sy}px)`;
|
|
d.over(target(), x, y);
|
|
},
|
|
{ passive: false },
|
|
);
|
|
handle.addEventListener("touchend", (e) => {
|
|
if (!active || !touchOf(e)) return;
|
|
e.preventDefault();
|
|
const dragged = ghost !== null;
|
|
const long = Date.now() - start >= HOLD_MS;
|
|
const at = pressed;
|
|
if (dragged) d.drop(target(), x, y);
|
|
reset();
|
|
if (dragged) return;
|
|
if (long && d.hold) d.hold();
|
|
// The touch was taken from the browser, so its tap is clicked here.
|
|
else if (at instanceof HTMLElement) at.click();
|
|
});
|
|
handle.addEventListener("touchcancel", (e) => {
|
|
if (active && touchOf(e)) reset();
|
|
});
|
|
// The browser's long-press menu and native drag would cover ours.
|
|
for (const type of ["contextmenu", "dragstart"]) {
|
|
handle.addEventListener(
|
|
type,
|
|
(e) => {
|
|
if (active || Date.now() - ended < 800) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
}
|
|
},
|
|
true,
|
|
);
|
|
}
|
|
}
|
|
|
|
/** The nearest ancestor of el that scrolls along axis. */
|
|
function scroller(el: HTMLElement, axis: "x" | "y"): HTMLElement | null {
|
|
for (let p = el.parentElement; p; p = p.parentElement) {
|
|
const style = getComputedStyle(p);
|
|
const overflow = axis === "x" ? style.overflowX : style.overflowY;
|
|
const room = axis === "x" ? p.scrollWidth > p.clientWidth : p.scrollHeight > p.clientHeight;
|
|
if (room && (overflow === "auto" || overflow === "scroll")) return p;
|
|
}
|
|
return null;
|
|
}
|