Files
wekan/models/lib/boardSortReorder.js
T
Lauri Ojansivu c0e71705f6 All Boards: a live drop gap that follows the drag, in place, not just at the end.
Replaces the fixed "drop at the end" placeholder with the standard sortable feel:
while a board is dragged, an empty dashed gap opens up before or after the tile
under the cursor, showing exactly where the board will land - anywhere in the
current view (Starred / Templates / Remaining / a workspace), not only at the
end.

How it works:
  * `dragover .js-board` computes which side of the tile the cursor is on
    (inline-centre, RTL-aware) and inserts a placeholder `<li>` there. The
    placeholder is a plain DOM node, inserted next to the Blaze-managed tiles and
    removed on dragend, so it never fights Blaze's reactive list.
  * `drop .js-board` reads that side and reorders with the pure
    computeReorderedIds(..., after), which now inserts on the target's leading OR
    trailing side and returns null for a no-op (including a drop back into the
    board's own slot). Dropping past the last tile reaches the end, so the
    separate end target is gone.

The placeholder is a real grid cell, so the surrounding tiles flow around the gap
as it moves. Unit tests cover the new before/after side and the own-slot no-ops.

Still mouse-only: HTML5 drag-and-drop does not fire from touch (the jQuery-UI-
sortable + touch-punch move noted before is what would add finger dragging).

Thanks to xet7 !
2026-07-24 08:06:14 +03:00

73 lines
3.3 KiB
JavaScript

'use strict';
// Pure helpers for reordering boards on the All Boards page by drag-and-drop.
// Extracted so the decision logic is unit-testable in plain Node without Meteor
// (mirrors models/lib/cardSearch.js).
//
// #6439 (Custom (drag order) sort — drag shows 'not-allowed' cursor, drop does
// not reorder): the old jQuery-ui sortable that reordered boards in the "custom"
// mode was removed when the All Boards page moved to HTML5 drag-and-drop for
// workspace/space management, and no HTML5 reorder handler replaced it. Boards
// carry draggable="true", but with no drop target that accepts them the browser
// shows the forbidden cursor and nothing persists to profile.boardSortIndex.
//
// The wiring lives in client/components/boards/boardsList.js; the pieces below
// are the two decisions worth testing in isolation:
// 1. whether drag-reordering should be active for the current sort mode, and
// 2. the new per-user boardSortIndex mapping produced by a drop.
// Drag-reordering only makes sense in the manual "custom" order. In the sorted
// modes ('title-asc' / 'title-desc') the order is derived from the title, so a
// manual drop must be ignored (and the board grid must not offer a drop target).
function isDragReorderEnabled(sortBy) {
return sortBy === 'custom';
}
// Given the currently displayed board ids (in display order), move `draggedId`
// so it sits immediately before `targetId` (the board it was dropped on). Returns
// the new ordered array, or null when the drop is a no-op or the ids are unknown.
function computeReorderedIds(orderedIds, draggedId, targetId, after = false) {
if (!Array.isArray(orderedIds)) return null;
if (!draggedId || !targetId || draggedId === targetId) return null;
const from = orderedIds.indexOf(draggedId);
const to = orderedIds.indexOf(targetId);
if (from === -1 || to === -1) return null;
const result = orderedIds.slice();
result.splice(from, 1);
// Insert relative to the target's current position (after the dragged id has
// been removed). `after` inserts on the target's trailing side, so a board can
// be dropped either before or after the tile the cursor is over - including
// after the LAST tile, which reaches the end of the order.
const at = result.indexOf(targetId);
result.splice(after ? at + 1 : at, 0, draggedId);
// A drop that lands the board back in its own slot is a no-op.
if (result.every((id, i) => id === orderedIds[i])) return null;
return result;
}
// Turn an ordered list of board ids into a { boardId: index } mapping suitable
// for profile.boardSortIndex. Sequential integers keep the order stable and
// avoid fractional drift on repeated reorders.
function computeSortIndexMapping(orderedIds) {
const mapping = {};
(orderedIds || []).forEach((id, i) => {
mapping[id] = i;
});
return mapping;
}
// Convenience: compute the persisted boardSortIndex mapping for a drop, or null
// when the drop changes nothing (so callers can skip the write).
function computeReorderedSortIndex(orderedIds, draggedId, targetId, after = false) {
const reordered = computeReorderedIds(orderedIds, draggedId, targetId, after);
if (!reordered) return null;
return computeSortIndexMapping(reordered);
}
export {
isDragReorderEnabled,
computeReorderedIds,
computeSortIndexMapping,
computeReorderedSortIndex,
};