mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
82165855c7
- A list keeps its own order: drag a place by its grip (desktop and phone) or move it with the arrow keys. Reordering is off while a search or filter hides places and the list says why. New PUT /collections/:id/places/reorder and the MCP tool reorder_collection_places; new places go to the end. Client and server now order ties the same way (sort_order, oldest first, id). - Copying saved places into a trip no longer takes their notes and tags along unasked. The picker offers both; a tag taken along becomes a trip tag (a trip tag of the same name is reused), never someone's personal tag. - The source trip and place a saved place names are only kept when the user can open that trip and the place belongs to it. - A KML or KMZ map can be read into a list: the dialog lists every placemark to keep or leave out and says which entries were skipped and why (POST /collections/map/read writes nothing; the import does). - A saved place without coordinates says so and can be looked up on the map from its edit form, on the phone too, where a missing position could not be added at all. A typed address without a pick says it will not be on the map. - The trip map can show the place names beside the pins (desktop and phone, both renderers). Names that would cover another pin or a control give way, the selected place and the planned stops first.
106 lines
3.9 KiB
TypeScript
106 lines
3.9 KiB
TypeScript
import type React from 'react';
|
|
import { useState } from 'react';
|
|
|
|
/** Where a dragged place would land relative to the row under it. */
|
|
export type DropSide = 'before' | 'after';
|
|
|
|
/**
|
|
* Moves `id` next to `targetId` in `order`. Returns the order unchanged when either id
|
|
* is missing or the place would land where it already is.
|
|
*/
|
|
export function movePlace(order: readonly number[], id: number, targetId: number, side: DropSide): number[] {
|
|
if (id === targetId || !order.includes(id) || !order.includes(targetId)) return [...order];
|
|
const rest = order.filter((x) => x !== id);
|
|
const at = rest.indexOf(targetId) + (side === 'after' ? 1 : 0);
|
|
return [...rest.slice(0, at), id, ...rest.slice(at)];
|
|
}
|
|
|
|
/** One step up or down, for the keyboard and for the phone's move buttons. */
|
|
export function stepPlace(order: readonly number[], id: number, delta: -1 | 1): number[] {
|
|
const from = order.indexOf(id);
|
|
const to = from + delta;
|
|
if (from < 0 || to < 0 || to >= order.length) return [...order];
|
|
const next = [...order];
|
|
next.splice(from, 1);
|
|
next.splice(to, 0, id);
|
|
return next;
|
|
}
|
|
|
|
const sameOrder = (a: readonly number[], b: readonly number[]) =>
|
|
a.length === b.length && a.every((x, i) => x === b[i]);
|
|
|
|
/**
|
|
* The manual order of a list's places (#1457), behind the desktop list and the phone
|
|
* cards, which render their own handles over it. Drag works with the mouse and, through
|
|
* the shell's touch bridge, with a finger; the handle also answers the arrow keys.
|
|
*
|
|
* `enabled` is false whenever a reorder would be a lie: another sort, a search or a
|
|
* filter that hides places, a list the reader may not edit, or "All saved", which mixes
|
|
* lists. Every change hands the whole order of the list to `onCommit`.
|
|
*/
|
|
export function usePlaceReorder(order: readonly number[], enabled: boolean, onCommit: (orderedIds: number[]) => void) {
|
|
const [dragId, setDragId] = useState<number | null>(null);
|
|
const [over, setOver] = useState<{ id: number; side: DropSide } | null>(null);
|
|
|
|
const commit = (next: number[]) => {
|
|
if (!sameOrder(next, order)) onCommit(next);
|
|
};
|
|
const end = () => {
|
|
setDragId(null);
|
|
setOver(null);
|
|
};
|
|
|
|
/** For the drag handle of one row. */
|
|
const handleProps = (id: number) =>
|
|
enabled
|
|
? {
|
|
draggable: true,
|
|
onDragStart: (e: React.DragEvent) => {
|
|
e.stopPropagation();
|
|
e.dataTransfer.effectAllowed = 'move';
|
|
e.dataTransfer.setData('text/plain', String(id));
|
|
setDragId(id);
|
|
},
|
|
onDragEnd: end,
|
|
onKeyDown: (e: React.KeyboardEvent) => {
|
|
if (e.key !== 'ArrowUp' && e.key !== 'ArrowDown') return;
|
|
e.preventDefault();
|
|
commit(stepPlace(order, id, e.key === 'ArrowUp' ? -1 : 1));
|
|
},
|
|
}
|
|
: {};
|
|
|
|
/** For the row itself: where the drop would land, and the drop. */
|
|
const rowProps = (id: number) =>
|
|
enabled && dragId != null
|
|
? {
|
|
onDragOver: (e: React.DragEvent<HTMLElement>) => {
|
|
e.preventDefault();
|
|
e.dataTransfer.dropEffect = 'move';
|
|
const box = e.currentTarget.getBoundingClientRect();
|
|
const side: DropSide = e.clientY - box.top < box.height / 2 ? 'before' : 'after';
|
|
if (over?.id !== id || over.side !== side) setOver({ id, side });
|
|
},
|
|
onDrop: (e: React.DragEvent) => {
|
|
e.preventDefault();
|
|
if (dragId != null && over) commit(movePlace(order, dragId, over.id, over.side));
|
|
end();
|
|
},
|
|
}
|
|
: {};
|
|
|
|
/** The line a row shows while something is dragged over it. */
|
|
const dropSide = (id: number): DropSide | null =>
|
|
dragId != null && dragId !== id && over?.id === id ? over.side : null;
|
|
|
|
return {
|
|
enabled,
|
|
dragId,
|
|
handleProps,
|
|
rowProps,
|
|
dropSide,
|
|
moveUp: (id: number) => commit(stepPlace(order, id, -1)),
|
|
moveDown: (id: number) => commit(stepPlace(order, id, 1)),
|
|
};
|
|
}
|