Skip to content

TagSpaces is an offline, open source, document manager with tagging support

TSN657---Allow-changing-the-order-of-locations-with-drag-and-drop (#1430)

Stanimir3afc9f32e60e
diff --git a/app/components/LocationManager.tsx b/app/components/LocationManager.tsx
index 1b6ea6c22..cf013005a 100644
--- a/app/components/LocationManager.tsx
+++ b/app/components/LocationManager.tsx
@@ -20,8 +20,9 @@ import React, { useEffect, useRef, useState } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { withStyles } from '@material-ui/core/styles';
-import List from '@material-ui/core/List';
import Button from '@material-ui/core/Button';
+import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
+import { List } from '@material-ui/core';
import styles from './SidePanels.css';
import LocationManagerMenu from './menus/LocationManagerMenu';
import ConfirmDialog from './dialogs/ConfirmDialog';
@@ -63,6 +64,7 @@ interface Props {
addLocations: (locations: Array<TS.Location>) => void;
editLocation: () => void;
removeLocation: (location: TS.Location) => void;
+ moveLocation: (uuid: string, position: number) => void;
isDesktop: boolean;
}
@@ -113,6 +115,29 @@ const LocationManager = (props: Props) => {
target.value = null;
}
+ /* const getItemStyle = (isDragging, draggableStyle) => ({
+ // some basic styles to make the items look a bit nicer
+ userSelect: 'none',
+ // change background colour if dragging
+ background: isDragging ? 'lightgreen' : 'transparent',
+
+ // styles we need to apply on draggables
+ ...draggableStyle
+ });
+
+ const getListStyle = isDraggingOver => ({
+ background: isDraggingOver ? 'lightblue' : 'transparent',
+ }); */
+
+ const onDragEnd = result => {
+ // dropped outside the list
+ if (!result.destination) {
+ return;
+ }
+
+ props.moveLocation(result.draggableId, result.destination.index);
+ };
+
const { classes, isDesktop } = props;
return (
<div className={classes.panel} style={props.style}>
@@ -198,18 +223,53 @@ const LocationManager = (props: Props) => {
overflowY: AppConfig.isFirefox ? 'auto' : 'overlay'
}}
>
- {props.locations.map(location => (
- <LocationView
- key={location.uuid}
- classes={props.classes}
- location={location}
- hideDrawer={props.hideDrawer}
- setEditLocationDialogOpened={setEditLocationDialogOpened}
- setDeleteLocationDialogOpened={setDeleteLocationDialogOpened}
- selectedLocation={selectedLocation}
- setSelectedLocation={setSelectedLocation}
- />
- ))}
+ <DragDropContext onDragEnd={onDragEnd}>
+ <Droppable droppableId="droppable">
+ {(provided, snapshot) => (
+ <div
+ {...provided.droppableProps}
+ ref={provided.innerRef}
+ /* style={getListStyle(snapshot.isDraggingOver)} */
+ >
+ {props.locations.map((location, index) => (
+ <Draggable
+ key={location.uuid}
+ draggableId={location.uuid}
+ index={index}
+ >
+ {(prov, snap) => (
+ <div
+ ref={prov.innerRef}
+ {...prov.draggableProps}
+ {...prov.dragHandleProps}
+ /* style={getItemStyle(
+ snap.isDragging,
+ prov.draggableProps.style
+ )} */
+ >
+ <LocationView
+ key={location.uuid}
+ classes={props.classes}
+ location={location}
+ hideDrawer={props.hideDrawer}
+ setEditLocationDialogOpened={
+ setEditLocationDialogOpened
+ }
+ setDeleteLocationDialogOpened={
+ setDeleteLocationDialogOpened
+ }
+ selectedLocation={selectedLocation}
+ setSelectedLocation={setSelectedLocation}
+ />
+ </div>
+ )}
+ </Draggable>
+ ))}
+ {provided.placeholder}
+ </div>
+ )}
+ </Droppable>
+ </DragDropContext>
</List>
</div>
<input
@@ -257,6 +317,7 @@ function mapDispatchToProps(dispatch) {
addLocations: LocationActions.addLocations,
editLocation: LocationActions.editLocation,
removeLocation: LocationActions.removeLocation,
+ moveLocation: LocationActions.moveLocation,
toggleOpenLinkDialog: AppActions.toggleOpenLinkDialog,
openURLExternally: AppActions.openURLExternally
},
diff --git a/app/reducers/locations.ts b/app/reducers/locations.ts
index 3382bd878..64bbb18cb 100644
--- a/app/reducers/locations.ts
+++ b/app/reducers/locations.ts
@@ -26,6 +26,7 @@ import { TS } from '-/tagspaces.namespace';
export const types = {
ADD_LOCATION: 'APP/ADD_LOCATION',
+ MOVE_LOCATION: 'APP/MOVE_LOCATION',
MOVE_UP_LOCATION: 'APP/MOVE_UP_LOCATION',
MOVE_DOWN_LOCATION: 'APP/MOVE_DOWN_LOCATION',
EDIT_LOCATION: 'APP/EDIT_LOCATION',
@@ -79,6 +80,21 @@ export default (state: Array<TS.Location> = initialState, action: any) => {
}
return state;
}
+ case types.MOVE_LOCATION: {
+ let indexForUpdating = -1;
+ state.forEach((location, index) => {
+ if (location.uuid === action.uuid) {
+ indexForUpdating = index;
+ }
+ });
+ if (indexForUpdating > -1) {
+ const locations = Array.from(state);
+ const [removed] = locations.splice(indexForUpdating, 1);
+ locations.splice(action.position, 0, removed);
+ return locations;
+ }
+ return state;
+ }
case types.MOVE_UP_LOCATION: {
let indexForUpdating = -1;
state.forEach((location, index) => {
@@ -181,6 +197,11 @@ export const actions = {
type: types.ADD_LOCATION,
location
}),
+ moveLocation: (uuid: string, position: number) => ({
+ type: types.MOVE_LOCATION,
+ uuid,
+ position
+ }),
moveLocationUp: (uuid: string) => ({ type: types.MOVE_UP_LOCATION, uuid }),
moveLocationDown: (uuid: string) => ({
type: types.MOVE_DOWN_LOCATION,