|
| 1 | +/** |
| 2 | + * TagSpaces - universal file and folder organizer |
| 3 | + * Copyright (C) 2025-present TagSpaces GmbH |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License (version 3) as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +// ********************************************************************** |
| 20 | +// Selection Area Component |
| 21 | +// ********************************************************************** |
| 22 | +// |
| 23 | +// This component covers the full area where selection is enabled. |
| 24 | +// It uses React DnD’s useDrag so that a drag gesture anywhere in the |
| 25 | +// container will produce the selection rectangle. When the drag ends, |
| 26 | +// it computes the rectangle (in viewport coordinates) and passes it |
| 27 | +// to an onSelect callback. |
| 28 | +// |
| 29 | +import DragItemTypes from '-/components/DragItemTypes'; |
| 30 | +import React, { useEffect } from 'react'; |
| 31 | +import { useDrag } from 'react-dnd'; |
| 32 | +import { getEmptyImage } from 'react-dnd-html5-backend'; |
| 33 | + |
| 34 | +interface SelectionAreaProps { |
| 35 | + onSelect: (rect: DOMRect) => void; |
| 36 | + children: React.ReactNode; |
| 37 | +} |
| 38 | + |
| 39 | +export const SelectionArea: React.FC<SelectionAreaProps> = ({ |
| 40 | + onSelect, |
| 41 | + children, |
| 42 | +}) => { |
| 43 | + // useDrag returns a ref that you attach to the element that should capture |
| 44 | + // drag events. Here we use it on the container. |
| 45 | + const [, drag, preview] = useDrag({ |
| 46 | + type: DragItemTypes.SELECTION, |
| 47 | + // No payload is needed here |
| 48 | + item: {}, |
| 49 | + end: (_, monitor) => { |
| 50 | + const initialOffset = monitor.getInitialClientOffset(); |
| 51 | + const dropOffset = monitor.getClientOffset(); |
| 52 | + if (initialOffset && dropOffset) { |
| 53 | + // Compute rectangle coordinates based on the drag's start and end points |
| 54 | + const x = Math.min(initialOffset.x, dropOffset.x); |
| 55 | + const y = Math.min(initialOffset.y, dropOffset.y); |
| 56 | + const width = Math.abs(initialOffset.x - dropOffset.x); |
| 57 | + const height = Math.abs(initialOffset.y - dropOffset.y); |
| 58 | + // Create a DOMRect-like object (DOMRect constructor is available in modern browsers) |
| 59 | + const rect = new DOMRect(x, y, width, height); |
| 60 | + onSelect(rect); |
| 61 | + } |
| 62 | + }, |
| 63 | + }); |
| 64 | + |
| 65 | + // Disable the default drag preview by using an empty image. |
| 66 | + useEffect(() => { |
| 67 | + preview(getEmptyImage(), { captureDraggingState: true }); |
| 68 | + }, [preview]); |
| 69 | + |
| 70 | + return ( |
| 71 | + <div ref={drag} style={{ height: '100%' }}> |
| 72 | + {children} |
| 73 | + </div> |
| 74 | + ); |
| 75 | +}; |
0 commit comments