TagSpaces is an offline, open source, document manager with tagging support
CellView-refactoring (#2159)
Stanimirc9838b852cfe
diff --git a/package.json b/package.jsonindex 578adea0d..15310b5e5 100644--- a/package.json+++ b/package.json@@ -235,7 +235,7 @@"uuid": "^9.0.0"},"optionalDependencies": {- "@tagspacespro/tagspacespro": "5.0.70"+ "@tagspacespro/tagspacespro": "5.0.71"},"devDependencies": {"@electron/notarize": "^2.3.2",diff --git a/src/renderer/perspectives/common/CellView.tsx b/src/renderer/perspectives/common/CellView.tsxnew file mode 100644index 000000000..aff1d36de--- /dev/null+++ b/src/renderer/perspectives/common/CellView.tsx@@ -0,0 +1,320 @@+/**+ * TagSpaces - universal file and folder organizer+ * Copyright (C) 2023-present TagSpaces UG (haftungsbeschraenkt)+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License (version 3) as+ * published by the Free Software Foundation.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ */++import React from 'react';+import { useSelector } from 'react-redux';+import AppConfig from '-/AppConfig';+import { TS } from '-/tagspaces.namespace';+import { getDesktopMode } from '-/reducers/settings';+import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';+import { useSelectedEntriesContext } from '-/hooks/useSelectedEntriesContext';+import { usePerspectiveSettingsContext } from '-/hooks/usePerspectiveSettingsContext';+import FileSourceDnd from '-/components/FileSourceDnd';+import { NativeTypes } from 'react-dnd-html5-backend';+import TargetFileBox from '-/components/TargetFileBox';+import CustomDragLayer from '-/components/CustomDragLayer';+import TargetMoveFileBox from '-/components/TargetMoveFileBox';+import DragItemTypes from '-/components/DragItemTypes';+import {+ fileOperationsEnabled,+ folderOperationsEnabled,+} from '-/perspectives/common/main-container';+import { useSortedDirContext } from '-/perspectives/grid/hooks/useSortedDirContext';+import { useOpenedEntryContext } from '-/hooks/useOpenedEntryContext';+import { useIOActionsContext } from '-/hooks/useIOActionsContext';+import { useDirectoryContentContext } from '-/hooks/useDirectoryContentContext';+import { useNotificationContext } from '-/hooks/useNotificationContext';+import { useEntryExistDialogContext } from '-/components/dialogs/hooks/useEntryExistDialogContext';++interface Props {+ fsEntry: TS.FileSystemEntry;+ index: number;+ cellContent: (+ fsEntry: TS.FileSystemEntry,+ selectedEntries: Array<TS.FileSystemEntry>,+ index: number,+ handleGridContextMenu: (+ event: React.MouseEvent<HTMLDivElement>,+ fsEntry: TS.FileSystemEntry,+ ) => void,+ handleGridCellClick,+ handleGridCellDblClick,+ isLast?: boolean,+ ) => any;+ setFileContextMenuAnchorEl;+ setDirContextMenuAnchorEl;+ isLast?: boolean;+}++function CellView(props: Props) {+ const {+ fsEntry,+ index,+ cellContent,+ setFileContextMenuAnchorEl,+ setDirContextMenuAnchorEl,+ isLast,+ } = props;+ const { showDirectories, singleClickAction } =+ usePerspectiveSettingsContext();+ const { openEntryInternal } = useOpenedEntryContext();+ const { openDirectory } = useDirectoryContentContext();+ const { moveFiles, openFileNatively } = useIOActionsContext();+ const { readOnlyMode, currentLocation } = useCurrentLocationContext();+ const { selectedEntries, setSelectedEntries, lastSelectedEntryPath } =+ useSelectedEntriesContext();+ const { handleEntryExist, openEntryExistDialog } =+ useEntryExistDialogContext();+ const { sortedDirContent } = useSortedDirContext();+ const { showNotification } = useNotificationContext();++ const desktopMode = useSelector(getDesktopMode);++ if (!fsEntry.isFile && !showDirectories) {+ return null;+ }++ const handleGridContextMenu = (event, fsEntry: TS.FileSystemEntry) => {+ event.preventDefault();+ event.stopPropagation();+ // setMouseX(event.clientX);+ // setMouseY(event.clientY);+ const isEntryExist = selectedEntries.some(+ (entry) => entry.uuid === fsEntry.uuid,+ );+ if (fsEntry.isFile) {+ if (!desktopMode) {+ if (!isEntryExist) {+ if (selectedEntries.length > 0) {+ setSelectedEntries([...selectedEntries, fsEntry]);+ } else {+ setSelectedEntries([fsEntry]);+ }+ }+ } else if (+ selectedEntries.length === 0 ||+ !fileOperationsEnabled(selectedEntries)+ ) {+ setSelectedEntries([fsEntry]);+ } else if (event.ctrlKey) {+ if (!isEntryExist) {+ setSelectedEntries([...selectedEntries, fsEntry]);+ }+ } else if (isEntryExist) {+ // update selected entry+ setSelectedEntries([+ ...selectedEntries.filter((entry) => entry.uuid !== fsEntry.uuid),+ fsEntry,+ ]);+ } else {+ setSelectedEntries([fsEntry]);+ }+ setFileContextMenuAnchorEl(event.currentTarget);+ } else {+ if (+ selectedEntries.length === 0 ||+ !folderOperationsEnabled(selectedEntries)+ ) {+ setSelectedEntries([fsEntry]);+ } else if (isEntryExist) {+ // update selected entry+ setSelectedEntries([+ ...selectedEntries.filter((entry) => entry.uuid !== fsEntry.uuid),+ fsEntry,+ ]);+ } else {+ setSelectedEntries([fsEntry]);+ }+ if (setDirContextMenuAnchorEl) {+ setDirContextMenuAnchorEl(event.currentTarget);+ }+ }+ };++ const openLocation = (fsEntry: TS.FileSystemEntry) => {+ if (fsEntry.isFile) {+ setSelectedEntries([fsEntry]);+ openEntryInternal(fsEntry);+ //openEntry(fsEntry.path);+ } else {+ console.log('Handle Grid cell db click, selected path : ', fsEntry.path);+ openDirectory(fsEntry.path);+ }+ };++ const handleGridCellDblClick = (event, fsEntry: TS.FileSystemEntry) => {+ setSelectedEntries([]);+ openLocation(fsEntry);+ };++ const handleGridCellClick = (event, fsEntry: TS.FileSystemEntry) => {+ const selectHelperKey = AppConfig.isMacLike ? event.metaKey : event.ctrlKey;+ if (event.shiftKey) {+ let lastSelectedIndex = -1;+ if (lastSelectedEntryPath) {+ lastSelectedIndex = sortedDirContent.findIndex(+ (entry) => entry.path === lastSelectedEntryPath,+ );+ }+ const currentSelectedIndex = sortedDirContent.findIndex(+ (entry) => entry.path === fsEntry.path,+ );+ if (lastSelectedIndex < 0) {+ lastSelectedIndex = currentSelectedIndex;+ }++ let entriesToSelect;+ // console.log('lastSelectedIndex: ' + lastSelectedIndex + ' currentSelectedIndex: ' + currentSelectedIndex);+ if (currentSelectedIndex > lastSelectedIndex) {+ entriesToSelect = sortedDirContent.slice(+ lastSelectedIndex,+ currentSelectedIndex + 1,+ );+ } else if (currentSelectedIndex < lastSelectedIndex) {+ entriesToSelect = sortedDirContent+ .slice(currentSelectedIndex, lastSelectedIndex + 1)+ .reverse();+ } else if (currentSelectedIndex === lastSelectedIndex) {+ entriesToSelect = [fsEntry];+ }++ setSelectedEntries(entriesToSelect);+ } else if (event.type === 'drag') {+ if (+ selectedEntries &&+ selectedEntries.some((entry) => entry.path === fsEntry.path)+ ) {+ } else {+ const currentSelectedEntry = sortedDirContent.find(+ (entry) => entry.path === fsEntry.path,+ );+ if (currentSelectedEntry) {+ // in KanBan sortedDirContent not content dragging entry from subfolder+ setSelectedEntries([currentSelectedEntry]);+ }+ }+ } else if (selectHelperKey) {+ if (+ selectedEntries &&+ selectedEntries.some((entry) => entry.path === fsEntry.path)+ ) {+ setSelectedEntries(+ selectedEntries.filter((entry) => entry.path !== fsEntry.path),+ ); // deselect selected entry+ } else {+ setSelectedEntries([...selectedEntries, fsEntry]);+ }+ } else {+ setSelectedEntries([fsEntry]);+ if (fsEntry.isFile) {+ if (singleClickAction === 'openInternal') {+ openEntryInternal(fsEntry);+ } else if (singleClickAction === 'openExternal') {+ openFileNatively(fsEntry.path);+ }+ }+ }+ };++ const handleFileMoveDrop = (item, monitor) => {+ if (readOnlyMode) {+ showNotification(+ 'Importing files is disabled because the location is in read-only mode.',+ 'error',+ true,+ ); //i18n.t('core:dndDisabledReadOnlyMode')+ return;+ }+ if (item) {+ // const mItem = monitor.getItem();+ let arrPath;+ if (item.selectedEntries && item.selectedEntries.length > 0) {+ const arrSelected = item.selectedEntries+ .map((entry) => entry.path)+ // remove target folder selection+ .filter((epath) => epath !== item.targetPath);+ if (arrSelected.length > 0) {+ arrPath = arrSelected;+ } else {+ arrPath = [item.path];+ }+ } else {+ arrPath = [item.path];+ }+ console.log('Dropped files: ' + item.path);+ handleEntryExist(item.selectedEntries, item.targetPath).then((exist) => {+ if (exist) {+ openEntryExistDialog(exist, () => {+ moveFiles(arrPath, item.targetPath, currentLocation.uuid);+ });+ } else {+ moveFiles(arrPath, item.targetPath, currentLocation.uuid);+ }+ });+ }+ };++ const key = fsEntry.path;++ if (fsEntry.isFile) {+ return (+ <FileSourceDnd key={key}>+ {cellContent(+ fsEntry,+ selectedEntries,+ index,+ handleGridContextMenu,+ handleGridCellClick,+ handleGridCellDblClick,+ isLast,+ )}+ </FileSourceDnd>+ );+ }+ const { FILE } = NativeTypes;++ return (+ <div+ style={{+ position: 'relative',+ }}+ key={key}+ >+ <TargetFileBox accepts={[FILE]} directoryPath={fsEntry.path}>+ <CustomDragLayer />+ <TargetMoveFileBox+ accepts={[DragItemTypes.FILE]}+ targetPath={fsEntry.path}+ onDrop={handleFileMoveDrop}+ >+ {cellContent(+ fsEntry,+ selectedEntries,+ index,+ handleGridContextMenu,+ handleGridCellClick,+ handleGridCellDblClick,+ isLast,+ )}+ </TargetMoveFileBox>+ </TargetFileBox>+ </div>+ );+}++export default CellView;diff --git a/src/renderer/perspectives/common/main-container.tsx b/src/renderer/perspectives/common/main-container.tsxindex 8448a2eab..20b4e3f6c 100644--- a/src/renderer/perspectives/common/main-container.tsx+++ b/src/renderer/perspectives/common/main-container.tsx@@ -30,7 +30,7 @@ export const folderOperationsEnabled = (selectedEntries) => {return !selectionContainsFiles;};-export const renderCell = (+/*export const renderCell = (fsEntry: TS.FileSystemEntry,index: number,cellContent: (@@ -154,12 +154,12 @@ export const renderCell = (};const handleGridCellClick = (event, fsEntry: TS.FileSystemEntry) => {- /*const {+ /!*const {selectedEntries,directoryContent,lastSelectedEntry,setSelectedEntries- } = props;*/+ } = props;*!/const selectHelperKey = AppConfig.isMacLike ? event.metaKey : event.ctrlKey;if (event.shiftKey) {let lastSelectedIndex = -1;@@ -312,7 +312,7 @@ export const renderCell = (</TargetFileBox></div>);-};+};*/export const renderCellPlaceholder = () => {return (diff --git a/src/renderer/perspectives/grid/components/GridPagination.tsx b/src/renderer/perspectives/grid/components/GridPagination.tsxindex 745e5dbff..58922a6ef 100644--- a/src/renderer/perspectives/grid/components/GridPagination.tsx+++ b/src/renderer/perspectives/grid/components/GridPagination.tsx@@ -32,7 +32,6 @@ import TagContainer from '-/components/TagContainer';import { TS } from '-/tagspaces.namespace';import { getDescriptionPreview } from '-/services/utils-io';import { MilkdownEditor } from '@tagspaces/tagspaces-md';-import { renderCell } from '-/perspectives/common/main-container';import { useTranslation } from 'react-i18next';import { useOpenedEntryContext } from '-/hooks/useOpenedEntryContext';import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';@@ -46,6 +45,7 @@ import GridCellsContainer from './GridCellsContainer';import { useSortedDirContext } from '-/perspectives/grid/hooks/useSortedDirContext';import { useEntryExistDialogContext } from '-/components/dialogs/hooks/useEntryExistDialogContext';import { dataTidFormat } from '-/services/test';+import CellView from '-/perspectives/common/CellView';interface Props {directories: Array<TS.FileSystemEntry>;@@ -335,59 +335,25 @@ function GridPagination(props: Props) {</Grid><GridCellsContainer>{page === 1 &&- directories.map((entry, index) =>- renderCell(- entry,- index,- getCellContent,- showDirectories,- readOnlyMode,- desktopMode,- singleClickAction,- currentLocation,- selectedEntries,- setSelectedEntries,- lastSelectedEntryPath,- sortedDirContent,- openEntryInternal,- openFileNatively,- openDirectory,- setFileContextMenuAnchorEl,- setDirContextMenuAnchorEl,- showNotification,- moveFiles,- handleEntryExist,- openEntryExistDialog,- clearSelection,- ),- )}- {pageFiles.map((entry, index, dArray) =>- renderCell(- entry,- index,- getCellContent,- showDirectories,- readOnlyMode,- desktopMode,- singleClickAction,- currentLocation,- selectedEntries,- setSelectedEntries,- lastSelectedEntryPath,- sortedDirContent,- openEntryInternal,- openFileNatively,- openDirectory,- setFileContextMenuAnchorEl,- setDirContextMenuAnchorEl,- showNotification,- moveFiles,- handleEntryExist,- openEntryExistDialog,- clearSelection,- index === dArray.length - 1,- ),- )}+ directories.map((entry, index) => (+ <CellView+ fsEntry={entry}+ index={index}+ cellContent={getCellContent}+ setFileContextMenuAnchorEl={setFileContextMenuAnchorEl}+ setDirContextMenuAnchorEl={setDirContextMenuAnchorEl}+ />+ ))}+ {pageFiles.map((entry, index, dArray) => (+ <CellView+ fsEntry={entry}+ index={index}+ cellContent={getCellContent}+ setFileContextMenuAnchorEl={setFileContextMenuAnchorEl}+ setDirContextMenuAnchorEl={setDirContextMenuAnchorEl}+ isLast={index === dArray.length - 1}+ />+ ))}{pageFiles.length < 1 && directories.length < 1 && (<div style={{ textAlign: 'center' }}>{!showDescription &&