Skip to content

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

fix set autoFocus on tagging and optimize #2011 (#2165)

Stanimir0e2a53cb6573
diff --git a/src/renderer/components/EntryContainer.tsx b/src/renderer/components/EntryContainer.tsx
index 6ad52bb78..fe304c1bb 100644
--- a/src/renderer/components/EntryContainer.tsx
+++ b/src/renderer/components/EntryContainer.tsx
@@ -37,7 +37,6 @@ import { getUuid } from '@tagspaces/tagspaces-common/utils-io';
import AppConfig from '-/AppConfig';
import {
extractContainingDirectoryPath,
- getBackupFileLocation,
extractFileExtension,
} from '@tagspaces/tagspaces-common/paths';
import ConfirmDialog from '-/components/dialogs/ConfirmDialog';
@@ -53,7 +52,6 @@ import { TS } from '-/tagspaces.namespace';
import FileView from '-/components/FileView';
import { Pro } from '-/pro';
import { Switch } from '@mui/material';
-import ResolveConflictDialog from '-/components/dialogs/ResolveConflictDialog';
import { useTheme } from '@mui/material/styles';
import EntryContainerTabs from '-/components/EntryContainerTabs';
import EntryContainerNav from '-/components/EntryContainerNav';
@@ -61,14 +59,13 @@ import EntryContainerTitle from '-/components/EntryContainerTitle';
import { useTranslation } from 'react-i18next';
import { useFilePropertiesContext } from '-/hooks/useFilePropertiesContext';
import { useOpenedEntryContext } from '-/hooks/useOpenedEntryContext';
-import { useDirectoryContentContext } from '-/hooks/useDirectoryContentContext';
import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
import { useNotificationContext } from '-/hooks/useNotificationContext';
-import { usePlatformFacadeContext } from '-/hooks/usePlatformFacadeContext';
import { SaveIcon, EditIcon } from '-/components/CommonIcons';
import { useIOActionsContext } from '-/hooks/useIOActionsContext';
import { usePerspectiveActionsContext } from '-/hooks/usePerspectiveActionsContext';
import { useEditedEntryContext } from '-/hooks/useEditedEntryContext';
+import { useResolveConflictContext } from '-/components/dialogs/hooks/useResolveConflictContext';
//const historyKeys = Pro ? Pro.keys.historyKeys : {};
@@ -80,11 +77,9 @@ function EntryContainer() {
reloadOpenedFile,
toggleEntryFullWidth,
isEntryInFullWidth,
- reflectUpdateOpenedFileContent,
fileChanged,
setFileChanged,
} = useOpenedEntryContext();
- const { setReflectActions } = useEditedEntryContext();
const { setActions } = usePerspectiveActionsContext();
const {
saveDescription,
@@ -93,11 +88,10 @@ function EntryContainer() {
setEditMode,
isEditDescriptionMode,
} = useFilePropertiesContext();
- const { setAutoSave, getMetadataID } = useIOActionsContext();
+ const { setAutoSave } = useIOActionsContext();
const { findLocation, readOnlyMode } = useCurrentLocationContext();
- const { openDirectory, currentDirectoryPath } = useDirectoryContentContext();
- const { copyFilePromiseOverwrite, copyFilePromise, saveTextFilePromise } =
- usePlatformFacadeContext();
+ const { saveFileOpen } = useResolveConflictContext();
+
const { showNotification } = useNotificationContext();
const tabIndex = useSelector(getEntryContainerTab);
const keyBindings = useSelector(getKeyBindingObject);
@@ -127,8 +121,6 @@ function EntryContainer() {
] = useState<boolean>(false);
const [isEditTagsModalOpened, setEditTagsModalOpened] =
useState<boolean>(false);
- const [isConflictDialogOpen, setConflictDialogOpen] =
- useState<boolean>(false);
const isSavingInProgress = useRef<boolean>(false);
const [entryPropertiesHeight, setEntryPropertiesHeight] =
useState<number>(100);
@@ -432,11 +424,13 @@ function EntryContainer() {
// @ts-ignore
fileViewer.current.contentWindow.getContent
) {
+ // @ts-ignore
+ const fileContent = fileViewer.current.contentWindow.getContent();
try {
//check if file is changed
if (fileChanged || force) {
isSavingInProgress.current = true;
- save(openedEntry).then((success) => {
+ saveFileOpen(openedEntry, fileContent).then((success) => {
if (success) {
setFileChanged(false);
// showNotification(
@@ -455,117 +449,6 @@ function EntryContainer() {
}
};
- const override = (): Promise<boolean> => {
- return cLocation
- .getPropertiesPromise(openedEntry.path)
- .then((entryProp: TS.FileSystemEntry) =>
- save({ ...openedEntry, lmdt: entryProp.lmdt }),
- );
- };
-
- const saveAs = (newFilePath: string): Promise<boolean> => {
- return copyFilePromise(openedEntry.path, newFilePath).then(() =>
- cLocation
- .getPropertiesPromise(newFilePath)
- .then((entryProp: TS.FileSystemEntry) =>
- save({
- ...openedEntry,
- path: entryProp.path,
- lmdt: entryProp.lmdt,
- }).then(() => {
- const openedEntryDir = extractContainingDirectoryPath(
- entryProp.path,
- );
- if (currentDirectoryPath === openedEntryDir) {
- openDirectory(openedEntryDir);
- }
- return true;
- }),
- ),
- );
- };
-
- async function save(fileOpen: TS.OpenedEntry): Promise<boolean> {
- // @ts-ignore
- const textContent = fileViewer.current.contentWindow.getContent();
- const location = findLocation(fileOpen.locationID);
- if (location) {
- if (
- Pro &&
- revisionsEnabled &&
- fileOpen.path.indexOf(
- location.getDirSeparator() +
- AppConfig.metaFolder +
- location.getDirSeparator(),
- ) === -1
- ) {
- const id = await getMetadataID(fileOpen.path, fileOpen.uuid, location);
- const targetPath = getBackupFileLocation(
- fileOpen.path,
- id,
- location.getDirSeparator(),
- );
- try {
- await copyFilePromiseOverwrite(
- fileOpen.path,
- targetPath,
- fileOpen.locationID,
- false,
- ); // todo test what happened if remove await?
- } catch (error) {
- console.log('copyFilePromiseOverwrite', error);
- }
- }
- return saveTextFilePromise(
- {
- path: fileOpen.path,
- lmdt: fileOpen.lmdt,
- locationID: fileOpen.locationID,
- },
- textContent,
- true,
- )
- .then((entry) => {
- reflectUpdateOpenedFileContent(entry);
- // send action to save in history
- const action: TS.EditAction = {
- action: 'edit',
- entry: entry,
- };
- setReflectActions(action);
- /*if (Pro) {
- const relativePath = getRelativeEntryPath(
- currentLocationPath,
- fileOpen.path,
- );
- historyContext.saveHistory(
- historyKeys.fileEditKey,
- {
- creationTimeStamp: new Date().getTime(),
- path: relativePath,
- url: generateSharingLink(fileOpen.locationID, relativePath),
- lid: fileOpen.locationID,
- },
- fileEditHistoryKey,
- );
- }*/
- return true;
- /*return updateOpenedFile(fileOpen.path, {
- id: '',
- ...fileOpen,
- editMode: true,
- //changed: false,
- shouldReload: undefined,
- }).then(() => true);*/
- })
- .catch((error) => {
- setConflictDialogOpen(true);
- console.log('Error saving file ' + fileOpen.path + ' - ' + error);
- return false;
- });
- }
- }
-
const editOpenedFile = () => {
// addToEntryContainer(openedEntry);
setEditMode(true);
@@ -943,12 +826,6 @@ function EntryContainer() {
}
/>
)}
- <ResolveConflictDialog
- open={isConflictDialogOpen}
- onClose={() => setConflictDialogOpen(false)}
- saveAs={saveAs}
- override={override}
- />
</GlobalHotKeys>
);
}
diff --git a/src/renderer/components/EntryProperties.tsx b/src/renderer/components/EntryProperties.tsx
index 690ded667..375737f6c 100644
--- a/src/renderer/components/EntryProperties.tsx
+++ b/src/renderer/components/EntryProperties.tsx
@@ -660,6 +660,7 @@ function EntryProperties(props: Props) {
tagMode="default"
handleChange={handleChange}
selectedEntryPath={openedEntry.path}
+ autoFocus={true}
/>
</TagDropContainer>
</Grid>
diff --git a/src/renderer/components/TagsSelect.tsx b/src/renderer/components/TagsSelect.tsx
index 333e02e5c..931c022b1 100644
--- a/src/renderer/components/TagsSelect.tsx
+++ b/src/renderer/components/TagsSelect.tsx
@@ -16,7 +16,7 @@
*
*/
-import React, { useRef, useState } from 'react';
+import React, { useEffect, useRef, useState } from 'react';
import { useSelector } from 'react-redux';
import TextField from '@mui/material/TextField';
import Autocomplete from '@mui/material/Autocomplete';
diff --git a/src/renderer/components/dialogs/ResolveConflictDialog.tsx b/src/renderer/components/dialogs/ResolveConflictDialog.tsx
index 7e17e9480..39e075839 100644
--- a/src/renderer/components/dialogs/ResolveConflictDialog.tsx
+++ b/src/renderer/components/dialogs/ResolveConflictDialog.tsx
@@ -32,19 +32,19 @@ import {
extractFileName,
} from '@tagspaces/tagspaces-common/paths';
import { useTranslation } from 'react-i18next';
-import { useOpenedEntryContext } from '-/hooks/useOpenedEntryContext';
+import { TS } from '-/tagspaces.namespace';
interface Props {
open: boolean;
+ openedEntry: TS.OpenedEntry;
onClose: () => void;
saveAs: (newFilePath: string) => Promise<boolean>;
override: () => Promise<boolean>;
}
function ResolveConflictDialog(props: Props) {
- const { open, onClose } = props;
+ const { open, onClose, openedEntry } = props;
const { t } = useTranslation();
- const { openedEntry } = useOpenedEntryContext();
const copyFileName = React.useRef<string>(getFileName());
const [isSaveAs, setSaveAs] = React.useState<boolean>(false);
@@ -52,9 +52,12 @@ function ResolveConflictDialog(props: Props) {
const fullScreen = useMediaQuery(theme.breakpoints.down('md'));
function getFileName() {
- const fileName = extractFileName(openedEntry.path);
- const ext = extractFileExtension(openedEntry.path);
- return fileName.slice(0, -(ext.length + 1)) + '-copy.' + ext;
+ if (openedEntry) {
+ const fileName = extractFileName(openedEntry.path);
+ const ext = extractFileExtension(openedEntry.path);
+ return fileName.slice(0, -(ext.length + 1)) + '-copy.' + ext;
+ }
+ return '';
}
return (
diff --git a/src/renderer/components/dialogs/hooks/ResolveConflictContextProvider.tsx b/src/renderer/components/dialogs/hooks/ResolveConflictContextProvider.tsx
new file mode 100644
index 000000000..a72fc8002
--- /dev/null
+++ b/src/renderer/components/dialogs/hooks/ResolveConflictContextProvider.tsx
@@ -0,0 +1,225 @@
+/**
+ * TagSpaces - universal file and folder organizer
+ * Copyright (C) 2017-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, { createContext, useMemo, useReducer, useRef } from 'react';
+import {
+ extractContainingDirectoryPath,
+ getBackupFileLocation,
+} from '@tagspaces/tagspaces-common/paths';
+import LoadingLazy from '-/components/LoadingLazy';
+import { TS } from '-/tagspaces.namespace';
+import { Pro } from '-/pro';
+import AppConfig from '-/AppConfig';
+import { useEditedEntryContext } from '-/hooks/useEditedEntryContext';
+import { useIOActionsContext } from '-/hooks/useIOActionsContext';
+import { usePlatformFacadeContext } from '-/hooks/usePlatformFacadeContext';
+import { useSelector } from 'react-redux';
+import { isRevisionsEnabled } from '-/reducers/settings';
+import { useOpenedEntryContext } from '-/hooks/useOpenedEntryContext';
+import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
+import { useDirectoryContentContext } from '-/hooks/useDirectoryContentContext';
+
+type ResolveConflictContextData = {
+ openResolveConflictDialog: (oEntry: TS.OpenedEntry, fContent: string) => void;
+ closeResolveConflictDialog: () => void;
+ saveFileOpen: (
+ fileOpen: TS.OpenedEntry,
+ textContent: string,
+ ) => Promise<boolean>;
+};
+
+export const ResolveConflictContext = createContext<ResolveConflictContextData>(
+ {
+ openResolveConflictDialog: undefined,
+ closeResolveConflictDialog: undefined,
+ saveFileOpen: undefined,
+ },
+);
+
+export type ResolveConflictContextProviderProps = {
+ children: React.ReactNode;
+};
+
+const ResolveConflictDialog = React.lazy(
+ () =>
+ import(
+ /* webpackChunkName: "ResolveConflictDialog" */ '../ResolveConflictDialog'
+ ),
+);
+
+export const ResolveConflictContextProvider = ({
+ children,
+}: ResolveConflictContextProviderProps) => {
+ const open = useRef<boolean>(false);
+ const openedEntry = useRef<TS.OpenedEntry>(undefined);
+ const fileContent = useRef<string>(undefined);
+
+ const { findLocation } = useCurrentLocationContext();
+ const { currentDirectoryPath, openDirectory } = useDirectoryContentContext();
+ const { copyFilePromiseOverwrite, copyFilePromise, saveTextFilePromise } =
+ usePlatformFacadeContext();
+ const { getMetadataID } = useIOActionsContext();
+ const { reflectUpdateOpenedFileContent } = useOpenedEntryContext();
+ const { setReflectActions } = useEditedEntryContext();
+
+ const revisionsEnabled = useSelector(isRevisionsEnabled);
+
+ const [ignored, forceUpdate] = useReducer((x) => x + 1, 0, undefined);
+
+ function openDialog(oEntry: TS.OpenedEntry, fContent: string) {
+ open.current = true;
+ openedEntry.current = oEntry;
+ fileContent.current = fContent;
+ forceUpdate();
+ }
+
+ function closeDialog() {
+ open.current = false;
+ forceUpdate();
+ }
+
+ const override = (): Promise<boolean> => {
+ const cLocation = findLocation(openedEntry.current.locationID);
+ return cLocation
+ .getPropertiesPromise(openedEntry.current.path)
+ .then((entryProp: TS.FileSystemEntry) =>
+ saveFileOpen(
+ { ...openedEntry.current, lmdt: entryProp.lmdt },
+ fileContent.current,
+ ),
+ );
+ };
+
+ const saveAs = (newFilePath: string): Promise<boolean> => {
+ const cLocation = findLocation(openedEntry.current.locationID);
+ return copyFilePromise(openedEntry.current.path, newFilePath).then(() =>
+ cLocation
+ .getPropertiesPromise(newFilePath)
+ .then((entryProp: TS.FileSystemEntry) =>
+ saveFileOpen(
+ {
+ ...openedEntry.current,
+ path: entryProp.path,
+ lmdt: entryProp.lmdt,
+ },
+ fileContent.current,
+ ).then(() => {
+ const openedEntryDir = extractContainingDirectoryPath(
+ entryProp.path,
+ );
+ if (currentDirectoryPath === openedEntryDir) {
+ return openDirectory(openedEntryDir).then(() => true);
+ }
+ return true;
+ }),
+ ),
+ );
+ };
+
+ async function saveFileOpen(
+ fileOpen: TS.OpenedEntry,
+ textContent: string,
+ ): Promise<boolean> {
+ const location = findLocation(fileOpen.locationID);
+ if (location) {
+ if (
+ Pro &&
+ revisionsEnabled &&
+ fileOpen.path.indexOf(
+ location.getDirSeparator() +
+ AppConfig.metaFolder +
+ location.getDirSeparator(),
+ ) === -1
+ ) {
+ const id = await getMetadataID(fileOpen.path, fileOpen.uuid, location);
+ const targetPath = getBackupFileLocation(
+ fileOpen.path,
+ id,
+ location.getDirSeparator(),
+ );
+ try {
+ await copyFilePromiseOverwrite(
+ fileOpen.path,
+ targetPath,
+ fileOpen.locationID,
+ false,
+ ); // todo test what happened if remove await?
+ } catch (error) {
+ console.log('copyFilePromiseOverwrite', error);
+ }
+ }
+ return saveTextFilePromise(
+ {
+ path: fileOpen.path,
+ lmdt: fileOpen.lmdt,
+ locationID: fileOpen.locationID,
+ },
+ textContent,
+ true,
+ )
+ .then((entry) => {
+ reflectUpdateOpenedFileContent(entry);
+ // send action to save in history
+ const action: TS.EditAction = {
+ action: 'edit',
+ entry: entry,
+ };
+ setReflectActions(action);
+ return true;
+ })
+ .catch((error) => {
+ openDialog(fileOpen, textContent);
+ console.log('Error saving file ' + fileOpen.path + ' - ' + error);
+ return false;
+ });
+ }
+ }
+
+ function ResolveConflictDialogAsync(props) {
+ return (
+ <React.Suspense fallback={<LoadingLazy />}>
+ <ResolveConflictDialog {...props} />
+ </React.Suspense>
+ );
+ }
+
+ const context = useMemo(() => {
+ return {
+ openResolveConflictDialog: openDialog,
+ closeResolveConflictDialog: closeDialog,
+ saveFileOpen: saveFileOpen,
+ };
+ }, []);
+
+ return (
+ <ResolveConflictContext.Provider value={context}>
+ <ResolveConflictDialogAsync
+ open={open.current}
+ onClose={(event, reason) => {
+ if (reason !== 'backdropClick') {
+ closeDialog();
+ }
+ }}
+ openedEntry={openedEntry.current}
+ override={override}
+ saveAs={saveAs}
+ />
+ {children}
+ </ResolveConflictContext.Provider>
+ );
+};
diff --git a/src/renderer/components/dialogs/hooks/useResolveConflictContext.tsx b/src/renderer/components/dialogs/hooks/useResolveConflictContext.tsx
new file mode 100644
index 000000000..d591889b5
--- /dev/null
+++ b/src/renderer/components/dialogs/hooks/useResolveConflictContext.tsx
@@ -0,0 +1,6 @@
+import { useContext } from 'react';
+
+import { ResolveConflictContext } from '-/components/dialogs/hooks/ResolveConflictContextProvider';
+
+export const useResolveConflictContext = () =>
+ useContext(ResolveConflictContext);
diff --git a/src/renderer/containers/DialogsRoot.tsx b/src/renderer/containers/DialogsRoot.tsx
index 415186b99..facda95cb 100644
--- a/src/renderer/containers/DialogsRoot.tsx
+++ b/src/renderer/containers/DialogsRoot.tsx
@@ -35,6 +35,7 @@ import { LinkDialogContextProvider } from '-/components/dialogs/hooks/LinkDialog
import { ProTeaserDialogContextProvider } from '-/components/dialogs/hooks/ProTeaserDialogContextProvider';
import { SettingsDialogContextProvider } from '-/components/dialogs/hooks/SettingsDialogContextProvider';
import { DeleteMultipleEntriesDialogContextProvider } from '-/components/dialogs/hooks/DeleteMultipleEntriesDialogContextProvider';
+import { ResolveConflictContextProvider } from '-/components/dialogs/hooks/ResolveConflictContextProvider';
export type DialogsRootProps = {
children: React.ReactNode;
@@ -60,7 +61,9 @@ function DialogsRoot({ children }: DialogsRootProps) {
<LinkDialogContextProvider>
<ProTeaserDialogContextProvider>
<SettingsDialogContextProvider>
- {children}
+ <ResolveConflictContextProvider>
+ {children}
+ </ResolveConflictContextProvider>
</SettingsDialogContextProvider>
</ProTeaserDialogContextProvider>
</LinkDialogContextProvider>