Skip to content

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

Support native drag and drop from tag spaces to desktop v2 (#2161)

Stanimir12933348cad8
diff --git a/package.json b/package.json
index 026186011..99d3fd149 100644
--- a/package.json
+++ b/package.json
@@ -354,8 +354,7 @@
"electronmon": {
"patterns": [
"!**/**",
- "src/main/**",
- ".erb/dll/**"
+ "src/main/**"
],
"logLevel": "quiet"
}
diff --git a/src/main/mainEvents.ts b/src/main/mainEvents.ts
index 5241c66b8..c32f24c73 100644
--- a/src/main/mainEvents.ts
+++ b/src/main/mainEvents.ts
@@ -4,6 +4,7 @@ import {
globalShortcut,
ipcMain,
shell,
+ nativeImage,
BrowserWindow,
} from 'electron';
import {
@@ -125,6 +126,17 @@ export default function loadMainEvents() {
const results = await readMacOSTags(filename);
return results;
});
+ ipcMain.on('ondragstart', (event, filePath) => {
+ // https://www.electronjs.org/docs/latest/api/web-contents#contentsstartdragitem
+
+ const dragIconBase64 =
+ 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEYAAABGCAYAAABxLuKEAAAACXBIWXMAACsPAAArDwFlCXzZAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAA3hJREFUeJztm8+LVlUYxz+PjdaULkQkQWRIGBrcDNoipIJCxYVBu9noX6DSRglTpJWCQa4qajEkGLlwL4gWRCOFOJIgpFBiObVIKVr4C3zn2+JOMF3Pw3vuzD33Pdn5wNmc+9xzvu+Xc8597nnPNUkUHmfJoAXkSjHGoRjjUIxxGOqiEzNbAzzfUX8PgBlJfy2qFUlJCrAU2AfcANRxmQW+A95csP5EpjwLfDUAQ0LlaE7GTGZgyPyys+lvsLYTPDMbBa6R18L+M/CCGvzYFOJ3JGp3MYwA401uSPGUGHHqLwB/JOivzigwFqgfAb6PbSSFMUud+lNUQzo12wkbs6xJI7kN+WwoxjgUYxyKMQ6NF18zWwtsBX6TdK59SXkQPWLMbNjMPgZuAieAtxJpyoKoEWNmy4AzwOtJ1WRE7Ih5h/+RKRBhjJk9BbzdgZasiBkxLwKrUwvJjZg15ndgIlD/Y8tasqKvMZLuAKc70JIVJcFzKMY49J1KZrYSeClwaUbStfYl5UHM4jsOhFL/j4C97crJhzKVHIoxDsUYh2KMQ8zi+wtwLFD/bctasiIm870BHOhAS1aUqeRQjHGIyXyHgBWBSw8l3WtfUh7EjJhXqf5arZf3E+oaOGUqORRjHIoxDsUYh5jM90/gfKD+estasiIm870CbOtAS1aUqeSQ4kSVdwBwDFieoL863lG32SaNpDDmtlM/6G1QT1eQmFeCl4EvApc+l/ReoP6bJgI64h5wqckNMSNmGFgfqF/lxH8NXAY2NRGSmE+avte1vvhKmgV2UT3mc2AaONz0piRPJUk/AJsZ7C7fLNUBpzcWsguQ7DMZSdfN7BWqKbUZWJOyv3k8oNqO/VLSgs8Vxwh9RHha3O9349zZ/em58p+i9Y8snhRK5utQjHEoxjjEZL6jwJHApbOSJufFPQ2cDMT9JOndWpsfAOvqgZImanG7CZ8W3SfpVj/tiyLiM761hD+n+7AW95wTdzHQ5tVQbCDus0BcD1iZ4pPF+aXvVJL0K1WKnwtTkpJn1bFrzEH87YQu6QGHuugoyhhJZ4H9DNacHrBH0lQXnUU/lSQdB7ZQvT33kil6nB7VnvNrkj7tqtMFZb5mthx4Zu4M8D91S4CNgfC79UOMZraBajvjX0iarsWtnru/87+CyyuBQ0nwHIoxDsUYh2KMw9+ZHv8EJjGI7wAAAABJRU5ErkJggg==';
+ const icon = nativeImage.createFromDataURL(dragIconBase64);
+ event.sender.startDrag({
+ file: filePath,
+ icon: icon,
+ });
+ });
ipcMain.handle('postRequest', async (event, payload, endpoint) => {
try {
const result = await postRequest(payload, endpoint);
diff --git a/src/main/preload.ts b/src/main/preload.ts
index 239d8bb0c..5c02bfa53 100644
--- a/src/main/preload.ts
+++ b/src/main/preload.ts
@@ -94,6 +94,7 @@ const electronHandler = {
removeAllListeners(channel: string) {
ipcRenderer.removeAllListeners(channel);
},
+ startDrag: (fileName) => ipcRenderer.send('ondragstart', fileName),
getPathForFile(file: File) {
return webUtils.getPathForFile(file);
},
diff --git a/src/renderer/locales/en/core.json b/src/renderer/locales/en/core.json
index fd864e4c8..39788037a 100644
--- a/src/renderer/locales/en/core.json
+++ b/src/renderer/locales/en/core.json
@@ -853,5 +853,7 @@
"switchEncryption": "Server Side Encryption",
"generateEncryptionKey": "Generate random encryption key",
"toggleKeyVisibility": "Toggle key visibility",
+ "dragMode": "Enable dragging files outside of the application",
+ "dragOutsideApp": "Drag outside the app",
"-": "-"
}
diff --git a/src/renderer/perspectives/common/CellView.tsx b/src/renderer/perspectives/common/CellView.tsx
index aff1d36de..9fc947e7d 100644
--- a/src/renderer/perspectives/common/CellView.tsx
+++ b/src/renderer/perspectives/common/CellView.tsx
@@ -16,7 +16,7 @@
*
*/
-import React from 'react';
+import React, { useEffect, useRef } from 'react';
import { useSelector } from 'react-redux';
import AppConfig from '-/AppConfig';
import { TS } from '-/tagspaces.namespace';
@@ -30,6 +30,9 @@ import TargetFileBox from '-/components/TargetFileBox';
import CustomDragLayer from '-/components/CustomDragLayer';
import TargetMoveFileBox from '-/components/TargetMoveFileBox';
import DragItemTypes from '-/components/DragItemTypes';
+import DragHandleIcon from '@mui/icons-material/DragHandleOutlined';
+import Typography from '@mui/material/Typography';
+import { useTheme } from '@mui/material/styles';
import {
fileOperationsEnabled,
folderOperationsEnabled,
@@ -40,6 +43,7 @@ import { useIOActionsContext } from '-/hooks/useIOActionsContext';
import { useDirectoryContentContext } from '-/hooks/useDirectoryContentContext';
import { useNotificationContext } from '-/hooks/useNotificationContext';
import { useEntryExistDialogContext } from '-/components/dialogs/hooks/useEntryExistDialogContext';
+import i18n from '-/services/i18n';
interface Props {
fsEntry: TS.FileSystemEntry;
@@ -72,6 +76,7 @@ function CellView(props: Props) {
} = props;
const { showDirectories, singleClickAction } =
usePerspectiveSettingsContext();
+ const theme = useTheme();
const { openEntryInternal } = useOpenedEntryContext();
const { openDirectory } = useDirectoryContentContext();
const { moveFiles, openFileNatively } = useIOActionsContext();
@@ -80,10 +85,27 @@ function CellView(props: Props) {
useSelectedEntriesContext();
const { handleEntryExist, openEntryExistDialog } =
useEntryExistDialogContext();
- const { sortedDirContent } = useSortedDirContext();
+ const { sortedDirContent, nativeDragModeEnabled } = useSortedDirContext();
const { showNotification } = useNotificationContext();
const desktopMode = useSelector(getDesktopMode);
+ // const fileSourceRef = useRef<HTMLDivElement | null>(null);
+
+ /*useEffect(() => {
+ const dragItem = fileSourceRef.current;
+ if (dragItem) {
+ const handleDragStart = (e) => {
+ e.preventDefault()
+ window.electronIO.ipcRenderer.startDrag(fsEntry.path);
+ };
+
+ dragItem.addEventListener('dragstart', handleDragStart);
+
+ return () => {
+ dragItem.removeEventListener('dragstart', handleDragStart);
+ };
+ }
+ }, [fileSourceRef.current]);*/
if (!fsEntry.isFile && !showDirectories) {
return null;
@@ -273,17 +295,41 @@ function CellView(props: Props) {
if (fsEntry.isFile) {
return (
- <FileSourceDnd key={key}>
- {cellContent(
- fsEntry,
- selectedEntries,
- index,
- handleGridContextMenu,
- handleGridCellClick,
- handleGridCellDblClick,
- isLast,
+ <div>
+ {nativeDragModeEnabled && (
+ <div
+ style={{
+ display: 'flex',
+ }}
+ draggable="true"
+ onDragStart={(e) => {
+ e.preventDefault();
+ window.electronIO.ipcRenderer.startDrag(fsEntry.path);
+ }}
+ >
+ <DragHandleIcon style={{ color: theme.palette.text.primary }} />
+ <Typography
+ color="textSecondary"
+ variant="caption"
+ style={{ alignSelf: 'center' }}
+ >
+ {i18n.t('dragOutsideApp')}
+ </Typography>
+ </div>
)}
- </FileSourceDnd>
+
+ <FileSourceDnd key={key}>
+ {cellContent(
+ fsEntry,
+ selectedEntries,
+ index,
+ handleGridContextMenu,
+ handleGridCellClick,
+ handleGridCellDblClick,
+ isLast,
+ )}
+ </FileSourceDnd>
+ </div>
);
}
const { FILE } = NativeTypes;
diff --git a/src/renderer/perspectives/grid/components/MainToolbar.tsx b/src/renderer/perspectives/grid/components/MainToolbar.tsx
index 49871037f..96c947b32 100644
--- a/src/renderer/perspectives/grid/components/MainToolbar.tsx
+++ b/src/renderer/perspectives/grid/components/MainToolbar.tsx
@@ -25,6 +25,10 @@ import Tooltip from '-/components/Tooltip';
import IconButton from '@mui/material/IconButton';
import SortingIcon from '@mui/icons-material/SwapVerticalCircle';
import ShareIcon from '@mui/icons-material/Share';
+// import DragOnIcon from '@mui/icons-material/PanTool';
+import DragOnIcon from '@mui/icons-material/CopyAllTwoTone';
+// import DragOffIcon from '@mui/icons-material/PanToolOutlined';
+import DragOffIcon from '@mui/icons-material/CopyAll';
import {
SelectedIcon,
UnSelectedIcon,
@@ -52,6 +56,7 @@ import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
import { useSelectedEntriesContext } from '-/hooks/useSelectedEntriesContext';
import { usePerspectiveSettingsContext } from '-/hooks/usePerspectiveSettingsContext';
import { useDeleteMultipleEntriesDialogContext } from '-/components/dialogs/hooks/useDeleteMultipleEntriesDialogContext';
+import { useSortedDirContext } from '-/perspectives/grid/hooks/useSortedDirContext';
interface Props {
prefixDataTID?: string;
@@ -76,7 +81,10 @@ function MainToolbar(props: Props) {
openShareFilesDialog,
} = props;
- const { haveLocalSetting } = usePerspectiveSettingsContext();
+ const { haveLocalSetting, setSettings, saveSettings } =
+ usePerspectiveSettingsContext();
+ const { nativeDragModeEnabled, setNativeDragModeEnabled } =
+ useSortedDirContext();
const { t } = useTranslation();
const theme = useTheme();
@@ -280,6 +288,18 @@ function MainToolbar(props: Props) {
<ZoomComponent preview={false} />
<Divider flexItem orientation="vertical" sx={{ mx: 0.5, my: 1 }} />
</Box>
+ {AppConfig.isElectron && (
+ <Tooltip title={t('core:dragMode')}>
+ <IconButton
+ data-tid={prefixDataTID + 'PerspectiveDragNative'}
+ onClick={() => {
+ setNativeDragModeEnabled(!nativeDragModeEnabled);
+ }}
+ >
+ {nativeDragModeEnabled ? <DragOnIcon /> : <DragOffIcon />}
+ </IconButton>
+ </Tooltip>
+ )}
<Box sx={{ flexGrow: 1 }} />
<Box sx={{ display: 'flex' }}>
<Tooltip
diff --git a/src/renderer/perspectives/grid/hooks/SortedDirContextProvider.tsx b/src/renderer/perspectives/grid/hooks/SortedDirContextProvider.tsx
index 3d284a6e5..8d7a6320a 100644
--- a/src/renderer/perspectives/grid/hooks/SortedDirContextProvider.tsx
+++ b/src/renderer/perspectives/grid/hooks/SortedDirContextProvider.tsx
@@ -29,16 +29,20 @@ type SortedDirContextData = {
sortedDirContent: TS.FileSystemEntry[];
sortBy: string;
orderBy: null | boolean;
+ nativeDragModeEnabled: boolean;
setSortBy: (sort: string) => void;
setOrderBy: (isAsc: null | boolean) => void;
+ setNativeDragModeEnabled: (enabled: boolean) => void;
};
export const SortedDirContext = createContext<SortedDirContextData>({
sortedDirContent: undefined,
sortBy: undefined,
orderBy: undefined,
- setSortBy: () => {},
- setOrderBy: () => {},
+ nativeDragModeEnabled: undefined,
+ setSortBy: undefined,
+ setOrderBy: undefined,
+ setNativeDragModeEnabled: undefined,
});
export type SortedDirContextProviderProps = {
@@ -72,6 +76,8 @@ export const SortedDirContextProvider = ({
? settings.orderBy
: defaultSettings.orderBy,
);
+ const [nativeDragModeEnabled, setNativeDragModeEnabled] =
+ useState<boolean>(false);
/*function getSettings(meta, persp = PerspectiveIDs.GRID): TS.FolderSettings {
if (persp === PerspectiveIDs.UNSPECIFIED) {
@@ -118,8 +124,10 @@ export const SortedDirContextProvider = ({
orderBy,
setSortBy,
setOrderBy,
+ nativeDragModeEnabled,
+ setNativeDragModeEnabled,
};
- }, [sortedDirContent, sortBy, orderBy]);
+ }, [sortedDirContent, sortBy, orderBy, nativeDragModeEnabled]);
return (
<SortedDirContext.Provider value={context}>
diff --git a/tests/e2e/general.helpers.js b/tests/e2e/general.helpers.js
index 00aa5cba6..1c2d499a5 100644
--- a/tests/e2e/general.helpers.js
+++ b/tests/e2e/general.helpers.js
@@ -12,8 +12,10 @@ export const defaultLocationName = 'supported-filestypes';
export const perspectiveGridTable = '//*[@data-tid="perspectiveGridFileTable"]';
export const newLocationName = 'Location Name Changed';
export const tsFolder = '\\.ts'; // escape dot
-export const selectorFile = '//*[@data-tid="perspectiveGridFileTable"]/span';
-export const selectorFolder = '//*[@data-tid="perspectiveGridFileTable"]/div';
+export const selectorFile =
+ '//*[@data-tid="perspectiveGridFileTable"]/div/span';
+export const selectorFolder =
+ '//*[@data-tid="perspectiveGridFileTable"]/div/div';
// const newHTMLFileName = 'newHTMLFile.html';
const testFolder = 'testFolder';