mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
On a board large enough to have scrollbars in both directions, dragging a card sometimes moved the card, sometimes scrolled the list, sometimes scrolled the board, and often several of those at once, so the card could not be dropped where it was aimed. A board runs three drag-scroll implementations over the same pointer: the dragscroll library, bound separately to `.board-canvas`, to every swimlane and to every lane; the `mousedown .board-canvas` lane pan in swimlanes.js; and jQuery UI sortable, which is the one that is supposed to move the card. Nothing stood any of them down while a drag was in progress. swimlanes.js did try, for list drags, but called `dragscroll.reset()` FIRST and removed the class afterwards - reset() binds to whatever carries the class at that moment, so the listeners stayed - and it left the canvas tagged anyway. The lane pan ignored the `nodragscroll` marker the library honours, so pressing a minicard, which carries that marker exactly because it is the card's drag handle, started a horizontal pan that ran alongside the card drag. Now client/lib/boardDragscroll.js suspends drag-scrolling for the whole drag - class off, THEN reset - and restores exactly the elements it took it from, so a board route does not gain panning it deliberately turned off. Every sortable on the board (cards, lists, swimlanes) suspends in start and resumes in stop, a window mouseup/touchend/dragend restores in case a drag never reaches its stop handler, and a swimlane re-rendered mid-drag does not re-arm panning under the pointer. The lane pan now refuses to start on `.nodragscroll` or on a drag handle, and gives way if a drag begins after the press. The drag handles themselves are marked `nodragscroll`: a press on a handle moves the item, it never pans the board. tests/boardDragScrollDuringDrag.test.cjs pins the order of the two steps, the restore-what-was-suspended rule, the safety net, the suspend/resume in every sortable and the two markers; specs/37-card-drag-sort.e2e.js drags a card sideways on a 12-list board and pins that neither the lane nor the canvas scrolls while it moves, and that panning is back afterwards. Thanks to mueschel and xet7 ! Fixes #6558,
107 lines
4.3 KiB
JavaScript
107 lines
4.3 KiB
JavaScript
import dragscroll from '@wekanteam/dragscroll';
|
|
|
|
// ============================================================================
|
|
// Drag-to-scroll stands down while something is being DRAGGED (#6558).
|
|
//
|
|
// "Moving cards behaves weirdly": on a board large enough to have scrollbars in
|
|
// both directions, dragging a card sometimes moved the card, sometimes scrolled
|
|
// the list, sometimes scrolled the whole board - and often several of those at
|
|
// the same time, which makes dropping a card where it was aimed nearly
|
|
// impossible.
|
|
//
|
|
// Why: a board has SEVERAL nested elements carrying the `dragscroll` class -
|
|
// `.board-canvas`, every `.swimlane` and every `.js-lists` lane. The library
|
|
// binds its own mousedown/mousemove to each of them independently, so ONE press
|
|
// can start a pan on the lane AND on the canvas, while jQuery UI sortable is
|
|
// dragging the card with the same pointer. Three things then move at once.
|
|
//
|
|
// swimlanes.js already tried to disable dragscroll while a LIST is dragged, but
|
|
// in the wrong order: it called `dragscroll.reset()` first and removed the class
|
|
// afterwards. reset() unbinds what it bound before and re-binds whatever carries
|
|
// the class AT THAT MOMENT, so removing the class after it changed nothing - the
|
|
// listeners stayed. The class has to go first, and the canvas has to be included,
|
|
// which is what this module does. Cards had no such attempt at all, which is why
|
|
// the report is about cards.
|
|
//
|
|
// suspend() remembers exactly which elements were tagged and restores that same
|
|
// set, so nothing becomes drag-scrollable that was not before (the board routes
|
|
// deliberately leave <body>/#content untagged - see client/lib/pageDragscroll.js).
|
|
// It is a no-op when already suspended, so nested drags cannot lose the original
|
|
// set. A window-level mouseup/touchend/dragend restores as well, so a drag that
|
|
// ends without its sortable `stop` - a re-render or sortable('destroy') mid-drag
|
|
// - can never leave the board unable to pan.
|
|
// ============================================================================
|
|
|
|
// The elements whose `dragscroll` class we took away, or null when not suspended.
|
|
let suspendedEls = null;
|
|
|
|
function restore() {
|
|
if (!suspendedEls) return;
|
|
const els = suspendedEls;
|
|
suspendedEls = null;
|
|
els.forEach(el => el.classList.add('dragscroll'));
|
|
try {
|
|
dragscroll.reset();
|
|
} catch (e) {
|
|
/* the library is not loaded in tests / SSR */
|
|
}
|
|
}
|
|
|
|
function onPointerEnd() {
|
|
restore();
|
|
}
|
|
|
|
export function suspendBoardDragscroll() {
|
|
if (suspendedEls || typeof document === 'undefined') return;
|
|
|
|
suspendedEls = Array.prototype.slice.call(
|
|
document.getElementsByClassName('dragscroll'),
|
|
);
|
|
suspendedEls.forEach(el => el.classList.remove('dragscroll'));
|
|
// AFTER the class is gone: reset() re-binds to what is tagged now, i.e. to
|
|
// nothing on the board, which also cancels a pan that is already under way.
|
|
try {
|
|
dragscroll.reset();
|
|
} catch (e) {
|
|
/* the library is not loaded in tests / SSR */
|
|
}
|
|
|
|
// Safety net for a drag that never reaches its `stop` handler.
|
|
window.addEventListener('mouseup', onPointerEnd, true);
|
|
window.addEventListener('touchend', onPointerEnd, true);
|
|
window.addEventListener('dragend', onPointerEnd, true);
|
|
}
|
|
|
|
export function resumeBoardDragscroll() {
|
|
window.removeEventListener('mouseup', onPointerEnd, true);
|
|
window.removeEventListener('touchend', onPointerEnd, true);
|
|
window.removeEventListener('dragend', onPointerEnd, true);
|
|
restore();
|
|
}
|
|
|
|
export function isBoardDragscrollSuspended() {
|
|
return suspendedEls !== null;
|
|
}
|
|
|
|
// dragscroll.reset() for callers that re-scan the board when its DOM changes
|
|
// (boardBody.js). While a drag is suspended, a swimlane or lane re-rendered
|
|
// mid-drag arrives with `dragscroll` in its markup again; binding it would put
|
|
// a pan back under the pointer that is dragging a card. Take the class off the
|
|
// newcomers too - and remember them, so they are restored with the rest.
|
|
export function resetBoardDragscroll() {
|
|
if (typeof document !== 'undefined' && suspendedEls) {
|
|
const tagged = Array.prototype.slice.call(
|
|
document.getElementsByClassName('dragscroll'),
|
|
);
|
|
tagged.forEach(el => {
|
|
el.classList.remove('dragscroll');
|
|
if (!suspendedEls.includes(el)) suspendedEls.push(el);
|
|
});
|
|
}
|
|
try {
|
|
dragscroll.reset();
|
|
} catch (e) {
|
|
/* the library is not loaded in tests / SSR */
|
|
}
|
|
}
|