TagSpaces is an offline, open source, document manager with tagging support
add duplicate Location (#1877)
Stanimir3907886a5649
diff --git a/app/components/menus/LocationContextMenu.tsx b/app/components/menus/LocationContextMenu.tsxindex b67e17003..11724e33f 100644--- a/app/components/menus/LocationContextMenu.tsx+++ b/app/components/menus/LocationContextMenu.tsx@@ -17,8 +17,10 @@*/import React from 'react';+import { getUuid } from '@tagspaces/tagspaces-common/utils-io';import ListItemText from '@mui/material/ListItemText';import ListItemIcon from '@mui/material/ListItemIcon';+import ContentCopyIcon from '@mui/icons-material/ContentCopy';import Menu from '@mui/material/Menu';import MenuItem from '@mui/material/MenuItem';import EditIcon from '@mui/icons-material/Edit';@@ -32,7 +34,10 @@ import { bindActionCreators } from 'redux';import { connect } from 'react-redux';import { locationType } from '@tagspaces/tagspaces-common/misc';import AppConfig from '-/AppConfig';-import { actions as LocationActions } from '-/reducers/locations';+import {+ actions as LocationActions,+ getLocationPosition+} from '-/reducers/locations';import { actions as LocationIndexActions } from '-/reducers/location-index';import i18n from '-/services/i18n';import { actions as AppActions } from '-/reducers/app';@@ -51,6 +56,12 @@ interface Props {closeLocationTree: () => void;locationDirectoryContextMenuAnchorEl: HTMLElement;setLocationDirectoryContextMenuAnchorEl: (el: HTMLElement) => void;+ addLocation: (+ location: TS.Location,+ openAfterCreate?: boolean,+ locationPosition?: number+ ) => void;+ locationPosition: number;}function LocationContextMenu(props: Props) {@@ -66,6 +77,20 @@ function LocationContextMenu(props: Props) {props.setEditLocationDialogOpened(true);};+ const duplicateLocation = () => {+ props.addLocation(+ {+ ...props.selectedLocation,+ uuid: getUuid(),+ name: props.selectedLocation.name + ' (copy)',+ isDefault: false+ },+ false,+ props.locationPosition + 1+ );+ props.setLocationDirectoryContextMenuAnchorEl(null);+ };+const moveLocationUp = () => {props.setLocationDirectoryContextMenuAnchorEl(null);if (selectedLocation && selectedLocation.uuid) {@@ -114,6 +139,18 @@ function LocationContextMenu(props: Props) {</MenuItem>);}+ menuItems.push(+ <MenuItem+ key="duplicateLocation"+ data-tid="duplicateLocationTID"+ onClick={duplicateLocation}+ >+ <ListItemIcon>+ <ContentCopyIcon />+ </ListItemIcon>+ <ListItemText primary={i18n.t('core:duplicateLocationTitle')} />+ </MenuItem>+ );menuItems.push(<MenuItemkey="indexLocation"@@ -209,10 +246,20 @@ function mapDispatchToProps(dispatch) {moveLocationUp: LocationActions.moveLocationUp,moveLocationDown: LocationActions.moveLocationDown,showInFileManager: AppActions.showInFileManager,- closeLocation: AppActions.closeLocation+ closeLocation: AppActions.closeLocation,+ addLocation: LocationActions.addLocation},dispatch);}-export default connect(undefined, mapDispatchToProps)(LocationContextMenu);+function mapStateToProps(state, ownProps) {+ return {+ locationPosition: getLocationPosition(state, ownProps.selectedLocation.uuid)+ };+}++export default connect(+ mapStateToProps,+ mapDispatchToProps+)(LocationContextMenu);diff --git a/app/locales/en/core.json b/app/locales/en/core.jsonindex d013428c5..4d5598155 100644--- a/app/locales/en/core.json+++ b/app/locales/en/core.json@@ -734,5 +734,6 @@"autosave": "Auto save","restore": "Restore","createdOn": "Created on",+ "duplicateLocationTitle": "Duplicate Location","-": "-"}diff --git a/app/reducers/locations.ts b/app/reducers/locations.tsindex d7d07e50d..d930e4ec3 100644--- a/app/reducers/locations.ts+++ b/app/reducers/locations.ts@@ -40,20 +40,22 @@ export const initialState = [];export default (state: Array<TS.Location> = initialState, action: any) => {switch (action.type) {case types.ADD_LOCATION: {- if (action.location.isDefault) {- state.forEach(location => {- // eslint-disable-next-line no-param-reassign- location.isDefault = false;- });+ const locations = action.location.isDefault+ ? state.map(location => ({ ...location, isDefault: false }))+ : [...state];++ const newLocation = {+ ...action.location,+ uuid: action.location.uuid || getUuid(),+ creationDate: new Date().toJSON()+ };+ if (action.locationPosition) {+ locations.splice(action.locationPosition, 0, newLocation);+ return locations;+ } else {+ locations.push(newLocation);+ return locations;}- return [- ...state,- {- ...action.location,- uuid: action.location.uuid || getUuid(),- creationDate: new Date().toJSON()- }- ];}case types.EDIT_LOCATION: {let indexForEditing = -1;@@ -172,10 +174,12 @@ export const actions = {}).catch(ex => console.error(ex));},- addLocation: (location: TS.Location, openAfterCreate = true) => (- dispatch: (actions: Object) => void- ) => {- dispatch(actions.createLocation(location));+ addLocation: (+ location: TS.Location,+ openAfterCreate = true,+ locationPosition: number = undefined+ ) => (dispatch: (actions: Object) => void) => {+ dispatch(actions.createLocation(location, locationPosition));if (openAfterCreate) {dispatch(AppActions.openLocation(location));}@@ -201,9 +205,13 @@ export const actions = {}});},- createLocation: (location: TS.Location) => ({+ createLocation: (+ location: TS.Location,+ locationPosition: number = undefined+ ) => ({type: types.ADD_LOCATION,- location+ location,+ locationPosition}),moveLocation: (uuid: string, position: number) => ({type: types.MOVE_LOCATION,@@ -280,6 +288,8 @@ export const getLocation = (locationId: string): TS.Location | null =>state.locations.find(location => location.uuid === locationId);+export const getLocationPosition = (state: any, locationId: string): number =>+ state.locations.findIndex(location => location.uuid === locationId);export const getLocationByPath = (state: any,path: string