Enhancing file browser with drag-to-select.

This commit is contained in:
Roland 2025-12-26 11:28:10 -07:00
parent 150ad6ee78
commit bdaccdeca1
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
5 changed files with 525 additions and 21 deletions

View file

@ -22,7 +22,7 @@ import {
Toggle,
useIsIgnoredDirectory
} from "./Common";
import React, {useEffect, useState} from "react";
import React, {useContext, useEffect, useState} from "react";
import {useDropzone} from "react-dropzone";
import {deleteFile, ignoreDirectory, makeDirectory, movePaths, renamePath, unignoreDirectory} from "../api";
import _ from 'lodash';
@ -30,7 +30,7 @@ import {SortableTable} from "./SortableTable";
import {useBrowseFiles, useMediaDirectory, useUploadFile, useWROLMode} from "../hooks/customHooks";
import {FileRowTagIcon, FilesRefreshButton} from "./Files";
import {FilePreviewContext} from "./FilePreview";
import {SettingsContext, ThemeContext} from "../contexts/contexts";
import {DragSelectionContext, DragSelectionProvider, SettingsContext, ThemeContext} from "../contexts/contexts";
import {BulkTagModal} from "./BulkTagModal";
import {InlineErrorBoundary} from "./ErrorBoundary";
@ -67,14 +67,27 @@ export function splitPathParentAndName(path) {
}
function Folder({folder, onFolderClick, sortData, selectedPaths, onFileClick, onSelect, disabled}) {
const {handleDragStart, handleDragMove, getDragState} = useContext(DragSelectionContext);
// Creates a single table row for a folder, or a row for itself and indented rows for its children.
let {path, children, is_empty} = folder;
const ignored = useIsIgnoredDirectory(path);
const pathWithNoTrailingSlash = path.substring(0, path.length - 1);
const name = path.substring(pathWithNoTrailingSlash.lastIndexOf('/') + 1);
const f = <TableRow key={path} disabled={disabled}>
const isInSelectedPaths = selectedPaths.indexOf(folder['path']) >= 0;
const {isDragSelecting, isDragDeselecting} = getDragState(path, isInSelectedPaths);
// Preview selection: show checked if selecting, unchecked if deselecting
const showAsSelected = (isInSelectedPaths && !isDragDeselecting) || isDragSelecting;
// Visual highlight class
const dragClass = isDragSelecting ? 'drag-selecting' : (isDragDeselecting ? 'drag-deselecting' : '');
const f = <TableRow
key={path}
disabled={disabled}
className={dragClass}
onMouseDown={(e) => handleDragStart(path, e)}
onMouseEnter={() => handleDragMove(path)}
>
<TableCell collapsing>
<Checkbox checked={selectedPaths.indexOf(folder['path']) >= 0} onChange={() => onSelect(folder['path'])}/>
<Checkbox checked={showAsSelected} onChange={() => onSelect(folder['path'])}/>
</TableCell>
<TableCell onClick={() => onFolderClick(path)} className='file-path' colSpan={2} disabled={is_empty}>
{depthIndentation(pathWithNoTrailingSlash)}
@ -105,10 +118,23 @@ function Folder({folder, onFolderClick, sortData, selectedPaths, onFileClick, on
}
function File({file, onFileClick, selectedPaths, onSelect, disabled}) {
const {handleDragStart, handleDragMove, getDragState} = useContext(DragSelectionContext);
const {path, size} = file;
return <TableRow key={path} disabled={disabled}>
const isInSelectedPaths = selectedPaths.indexOf(path) >= 0;
const {isDragSelecting, isDragDeselecting} = getDragState(path, isInSelectedPaths);
// Preview selection: show checked if selecting, unchecked if deselecting
const showAsSelected = (isInSelectedPaths && !isDragDeselecting) || isDragSelecting;
// Visual highlight class
const dragClass = isDragSelecting ? 'drag-selecting' : (isDragDeselecting ? 'drag-deselecting' : '');
return <TableRow
key={path}
disabled={disabled}
className={dragClass}
onMouseDown={(e) => handleDragStart(path, e)}
onMouseEnter={() => handleDragMove(path)}
>
<TableCell collapsing>
<Checkbox checked={selectedPaths.indexOf(path) >= 0} onChange={() => onSelect(path)}/>
<Checkbox checked={showAsSelected} onChange={() => onSelect(path)}/>
</TableCell>
<TableCell onClick={() => onFileClick(file)} className='file-path'>
{depthIndentation(path)}
@ -398,28 +424,52 @@ export function FileBrowser() {
return <ErrorMessage>Could not fetch files</ErrorMessage>
}
return <>
<div style={{marginBottom: '4em'}}>
return <DragSelectionProvider
selectedPaths={selectedPaths}
setSelectedPaths={setSelectedPaths}
browseFiles={browseFiles}
openFolders={openFolders}
>
<FileBrowserContent
browseFiles={browseFiles}
headers={headers}
onFolderClick={onFolderClick}
setPreviewFile={setPreviewFile}
selectedPaths={selectedPaths}
onSelect={onSelect}
pending={pending}
/>
{footer}
</DragSelectionProvider>
}
function FileBrowserContent({browseFiles, headers, onFolderClick, setPreviewFile, selectedPaths, onSelect, pending}) {
const {isDragging} = useContext(DragSelectionContext);
const containerClassName = isDragging ? 'file-browser-dragging' : '';
return (
<div style={{marginBottom: '4em'}} className={containerClassName}>
<SortableTable
tableProps={{unstackable: true}}
data={browseFiles}
tableHeaders={headers}
defaultSortColumn='path'
rowKey='path'
rowFunc={(i, sortData) => <Path
key={i['key']}
path={i}
onFolderClick={onFolderClick}
onFileClick={(i) => setPreviewFile(i)}
sortData={sortData}
selectedPaths={selectedPaths}
onSelect={onSelect}
disabled={pending}
/>}
rowFunc={(i, sortData) => (
<Path
key={i['key']}
path={i}
onFolderClick={onFolderClick}
onFileClick={(file) => setPreviewFile(file)}
sortData={sortData}
selectedPaths={selectedPaths}
onSelect={onSelect}
disabled={pending}
/>
)}
/>
</div>
{footer}
</>
);
}
export function RenameModal({open, onClose, path, onSubmit, onPending}) {

View file

@ -474,4 +474,229 @@ describe('FileBrowser', () => {
expect(mockSetOpenFolders).not.toHaveBeenCalled();
});
});
describe('Drag Selection', () => {
const mockBrowseFilesMultiple = [
{path: 'file1.txt', size: 100},
{path: 'file2.txt', size: 200},
{path: 'file3.txt', size: 300},
];
beforeEach(() => {
useBrowseFiles.mockReturnValue({
browseFiles: mockBrowseFilesMultiple,
openFolders: [],
setOpenFolders: jest.fn(),
fetchFiles: jest.fn(),
});
});
it('does not enable drag selection on touch devices', async () => {
// Mock matchMedia to simulate touch device
window.matchMedia = jest.fn().mockImplementation(query => ({
matches: query === '(pointer: coarse)', // true for touch devices
media: query,
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
}));
renderFileBrowser(<FileBrowser/>);
const rows = screen.getAllByRole('row');
// Skip header row (index 0)
const firstRow = rows[1];
// Simulate mousedown - on touch device, this should not start drag
await act(async () => {
fireEvent.mouseDown(firstRow, {button: 0, clientX: 100, clientY: 100});
});
// Move mouse and mouseup - items should NOT be selected via drag
await act(async () => {
fireEvent.mouseMove(document, {clientX: 100, clientY: 200});
fireEvent.mouseUp(document);
});
// Verify no items are selected (checkboxes unchecked)
const checkboxes = screen.getAllByRole('checkbox');
checkboxes.forEach(cb => expect(cb).not.toBeChecked());
});
it('selects rows when dragging across them', async () => {
renderFileBrowser(<FileBrowser/>);
const rows = screen.getAllByRole('row');
// Skip header row
const firstDataRow = rows[1];
const secondDataRow = rows[2];
const thirdDataRow = rows[3];
// Start drag on first row
await act(async () => {
fireEvent.mouseDown(firstDataRow, {button: 0, clientX: 100, clientY: 100});
});
// Move mouse enough to trigger drag threshold (5px)
await act(async () => {
fireEvent.mouseMove(document, {clientX: 100, clientY: 110});
});
// Enter subsequent rows
await act(async () => {
fireEvent.mouseEnter(secondDataRow);
});
await act(async () => {
fireEvent.mouseEnter(thirdDataRow);
});
// End drag
await act(async () => {
fireEvent.mouseUp(document);
});
// All three files should be selected
const checkboxes = screen.getAllByRole('checkbox');
expect(checkboxes.length).toBe(3);
checkboxes.forEach(cb => expect(cb).toBeChecked());
});
it('adds to existing selection when dragging', async () => {
renderFileBrowser(<FileBrowser/>);
// First, select the first file via checkbox
const checkboxes = screen.getAllByRole('checkbox');
await act(async () => {
fireEvent.click(checkboxes[0]);
});
// Verify first file is selected
expect(checkboxes[0]).toBeChecked();
expect(checkboxes[1]).not.toBeChecked();
expect(checkboxes[2]).not.toBeChecked();
const rows = screen.getAllByRole('row');
const secondDataRow = rows[2];
const thirdDataRow = rows[3];
// Start drag on second row
await act(async () => {
fireEvent.mouseDown(secondDataRow, {button: 0, clientX: 100, clientY: 100});
});
// Move enough to trigger drag
await act(async () => {
fireEvent.mouseMove(document, {clientX: 100, clientY: 110});
});
await act(async () => {
fireEvent.mouseEnter(thirdDataRow);
});
await act(async () => {
fireEvent.mouseUp(document);
});
// All three should now be selected (first via checkbox, second and third via drag)
const updatedCheckboxes = screen.getAllByRole('checkbox');
updatedCheckboxes.forEach(cb => expect(cb).toBeChecked());
});
it('does not start drag when clicking on checkbox', async () => {
renderFileBrowser(<FileBrowser/>);
const checkboxes = screen.getAllByRole('checkbox');
// Click checkbox - should toggle selection, not start drag
await act(async () => {
fireEvent.click(checkboxes[0]);
});
expect(checkboxes[0]).toBeChecked();
expect(checkboxes[1]).not.toBeChecked();
expect(checkboxes[2]).not.toBeChecked();
});
it('does not start drag with right mouse button', async () => {
renderFileBrowser(<FileBrowser/>);
const rows = screen.getAllByRole('row');
const firstDataRow = rows[1];
const secondDataRow = rows[2];
// Try to start drag with right button
await act(async () => {
fireEvent.mouseDown(firstDataRow, {button: 2, clientX: 100, clientY: 100});
});
await act(async () => {
fireEvent.mouseMove(document, {clientX: 100, clientY: 110});
});
await act(async () => {
fireEvent.mouseEnter(secondDataRow);
});
await act(async () => {
fireEvent.mouseUp(document);
});
// No items should be selected
const checkboxes = screen.getAllByRole('checkbox');
checkboxes.forEach(cb => expect(cb).not.toBeChecked());
});
it('deselects rows when dragging from a selected item', async () => {
renderFileBrowser(<FileBrowser/>);
// First, select all three files via checkboxes
const checkboxes = screen.getAllByRole('checkbox');
await act(async () => {
fireEvent.click(checkboxes[0]);
});
await act(async () => {
fireEvent.click(checkboxes[1]);
});
await act(async () => {
fireEvent.click(checkboxes[2]);
});
// Verify all are selected
expect(checkboxes[0]).toBeChecked();
expect(checkboxes[1]).toBeChecked();
expect(checkboxes[2]).toBeChecked();
const rows = screen.getAllByRole('row');
const firstDataRow = rows[1];
const secondDataRow = rows[2];
// Start drag from first row (which is selected) - should trigger deselect mode
await act(async () => {
fireEvent.mouseDown(firstDataRow, {button: 0, clientX: 100, clientY: 100});
});
// Move enough to trigger drag
await act(async () => {
fireEvent.mouseMove(document, {clientX: 100, clientY: 110});
});
await act(async () => {
fireEvent.mouseEnter(secondDataRow);
});
await act(async () => {
fireEvent.mouseUp(document);
});
// First two should now be deselected, third should remain selected
const updatedCheckboxes = screen.getAllByRole('checkbox');
expect(updatedCheckboxes[0]).not.toBeChecked();
expect(updatedCheckboxes[1]).not.toBeChecked();
expect(updatedCheckboxes[2]).toBeChecked();
});
});
});