TagSpaces is an offline, open source, document manager with tagging support
enhancement entry details (#1987)
Stanimir68eb4bc137c6
diff --git a/app/components/EditDescription.tsx b/app/components/EditDescription.tsxindex 3625b2699..a7e61fc72 100644--- a/app/components/EditDescription.tsx+++ b/app/components/EditDescription.tsx@@ -7,14 +7,10 @@ import { ProTooltip } from '-/components/HelperComponents';import { Pro } from '-/pro';import { convertMarkDown } from '-/services/utils-io';import { useTranslation } from 'react-i18next';+import { useSelector } from 'react-redux';+import { getDirectoryPath, getOpenedFiles, OpenedEntry } from '-/reducers/app';+import { useDescriptionContext } from '-/components/hooks/useDescriptionContext';-interface Props {- toggleEditDescriptionField: () => void;- // fileDescriptionRef: MutableRefObject<MilkdownRef>;- description: string;- setEditDescription: (md: string) => void;- currentFolder: string;-}const PREFIX = 'EditDescription';const classes = {@@ -43,16 +39,18 @@ const Root = styled('div')(({ theme }) => ({}}));-function EditDescription(props: Props) {+function EditDescription() {const { t } = useTranslation();const theme = useTheme();+ const openedFiles: Array<OpenedEntry> = useSelector(getOpenedFiles);+ const openedFile: OpenedEntry = openedFiles[0];+ const currentFolder = useSelector(getDirectoryPath);const fileDescriptionRef = useRef<MilkdownRef>(null);const {- currentFolder,description,- setEditDescription,- toggleEditDescriptionField- } = props;+ setDescription,+ saveDescription+ } = useDescriptionContext();const [editMode, setEditMode] = useState<boolean>(false);const descriptionFocus = useRef<boolean>(false);@@ -60,13 +58,20 @@ function EditDescription(props: Props) {fileDescriptionRef.current?.setDarkMode(theme.palette.mode === 'dark');}, [theme]); // , settings]);+ /*const keyBindingHandlers = {+ saveDocument: () => {+ //setEditMode(!editMode);+ toggleEditDescriptionField();+ } /!*dispatch(AppActions.openNextFile())*!/+ };*/+const milkdownOnFocus = React.useCallback(() => (descriptionFocus.current = true),[]);const milkdownListener = React.useCallback((markdown: string) => {if (descriptionFocus.current && markdown !== description) {- setEditDescription(markdown);+ setDescription(markdown);}// update codeMirror/*const { current } = codeMirrorRef;@@ -118,7 +123,7 @@ function EditDescription(props: Props) {disabled={!Pro}onClick={() => {setEditMode(!editMode);- toggleEditDescriptionField();+ saveDescription();}}>{editMode ? t('core:confirmSaveButton') : t('core:edit')}@@ -149,13 +154,12 @@ function EditDescription(props: Props) {{t('core:filePropertiesDescription')}</Typography>*/}</span>- {toggleEditDescriptionField && editSaveActions}+ {!openedFile.editMode && editSaveActions}<divdata-tid="descriptionTID"onDoubleClick={() => {- if (!editMode && toggleEditDescriptionField) {+ if (!editMode && !openedFile.editMode) {setEditMode(true);- toggleEditDescriptionField();}}}style={{@@ -178,7 +182,11 @@ function EditDescription(props: Props) {lineHeight: 4}}>- {t('core:addMarkdownDescription')}+ {t(+ openedFile.editMode+ ? 'core:editDisabled'+ : 'core:addMarkdownDescription'+ )}</Typography>) : (<>diff --git a/app/components/EntryContainer.tsx b/app/components/EntryContainer.tsxindex 0a252ba33..12c8f4e1f 100644--- a/app/components/EntryContainer.tsx+++ b/app/components/EntryContainer.tsx@@ -77,6 +77,7 @@ import EntryContainerTabs from '-/components/EntryContainerTabs';import EntryContainerNav from '-/components/EntryContainerNav';import EntryContainerTitle from '-/components/EntryContainerTitle';import { useTranslation } from 'react-i18next';+import { useDescriptionContext } from '-/components/hooks/useDescriptionContext';//const defaultSplitSize = '7.86%'; // '7.2%'; // 103;@@ -173,6 +174,7 @@ function EntryContainer(props: Props) {showNotification} = props;const { t } = useTranslation();+ const { saveDescription } = useDescriptionContext();const tabIndex = useSelector(getEntryContainerTab);const currentDirectoryPath = useSelector(getDirectoryPath);const theme = useTheme();@@ -548,7 +550,13 @@ function EntryContainer(props: Props) {// setEditingSupported(false);};- const startSavingFile = () => savingFile();+ const startSavingFile = () => {+ if (openedFile.editMode) {+ savingFile();+ } else {+ saveDescription();+ }+ };function savingFile(force = false) {if (diff --git a/app/components/EntryContainerTabs.tsx b/app/components/EntryContainerTabs.tsxindex 511be4f06..d6b72884a 100644--- a/app/components/EntryContainerTabs.tsx+++ b/app/components/EntryContainerTabs.tsx@@ -16,8 +16,8 @@**/-import React, { useRef } from 'react';-import { styled, useTheme } from '@mui/material/styles';+import React from 'react';+import { styled } from '@mui/material/styles';import { useSelector, useDispatch } from 'react-redux';import Tabs from '@mui/material/Tabs';import Tab from '@mui/material/Tab';@@ -25,11 +25,9 @@ import Box from '@mui/material/Box';import {actions as AppActions,AppDispatch,- getDirectoryPath,isReadOnlyMode,OpenedEntry} from '-/reducers/app';-import { Pro } from '-/pro';import Revisions from '-/components/Revisions';import EntryProperties from '-/components/EntryProperties';import TaggingActions from '-/reducers/tagging-actions';@@ -119,9 +117,6 @@ interface TabPanelProps {}function EntryContainerTabs(props: EntryContainerTabsProps) {- const { t } = useTranslation();- const tabIndex = useSelector(getEntryContainerTab);- // const [value, setValue] = React.useState(0);const {openedFile,openPanel,@@ -129,10 +124,10 @@ function EntryContainerTabs(props: EntryContainerTabsProps) {marginRight,isEditable} = props;- const editDescription = useRef<string>(undefined);- //const theme = useTheme();++ const { t } = useTranslation();+ const tabIndex = useSelector(getEntryContainerTab);const readOnlyMode = useSelector(isReadOnlyMode);- const directoryPath = useSelector(getDirectoryPath);const tileServer = useSelector(getMapTileServer);const dispatch: AppDispatch = useDispatch();const { isDesktopMode } = props;@@ -190,7 +185,7 @@ function EntryContainerTabs(props: EntryContainerTabsProps) {}};- const toggleEditDescriptionField = () => {+ /*const toggleEditDescriptionField = () => {if (readOnlyMode) {editDescription.current = undefined;return;@@ -234,7 +229,11 @@ function EntryContainerTabs(props: EntryContainerTabsProps) {} else {editDescription.current = '';}- };+ };*/++ // directories must be always opened+ const selectedTabIndex =+ !openedFile.isFile && tabIndex === undefined ? 0 : tabIndex;return (<div@@ -248,7 +247,7 @@ function EntryContainerTabs(props: EntryContainerTabsProps) {><Box sx={{ ...(marginRight && { marginRight }) }}><StyledTabs- value={tabIndex}+ value={selectedTabIndex}onChange={handleChange}aria-label="basic tabs example">@@ -277,7 +276,7 @@ function EntryContainerTabs(props: EntryContainerTabsProps) {)}</StyledTabs></Box>- <TsTabPanel value={tabIndex} index={0}>+ <TsTabPanel value={selectedTabIndex} index={0}><EntryPropertieskey={openedFile.path}openedEntry={openedFile}@@ -288,18 +287,11 @@ function EntryContainerTabs(props: EntryContainerTabsProps) {tileServer={tileServer}/></TsTabPanel>- <TsTabPanel value={tabIndex} index={1}>- <EditDescription- toggleEditDescriptionField={- !readOnlyMode && !openedFile.editMode && toggleEditDescriptionField- }- description={openedFile.description}- setEditDescription={md => (editDescription.current = md)}- currentFolder={directoryPath}- />+ <TsTabPanel value={selectedTabIndex} index={1}>+ <EditDescription /></TsTabPanel>{isEditable && (- <TsTabPanel value={tabIndex} index={2}>+ <TsTabPanel value={selectedTabIndex} index={2}><Revisions /></TsTabPanel>)}diff --git a/app/components/hooks/DescriptionContextProvider.tsx b/app/components/hooks/DescriptionContextProvider.tsxnew file mode 100644index 000000000..854535761--- /dev/null+++ b/app/components/hooks/DescriptionContextProvider.tsx@@ -0,0 +1,140 @@+/**+ * 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, useEffect, useMemo, useRef } from 'react';+import { useDispatch, useSelector } from 'react-redux';+import {+ actions as AppActions,+ AppDispatch,+ getOpenedFiles,+ isReadOnlyMode,+ OpenedEntry+} from '-/reducers/app';+import { Pro } from '-/pro';+import { useTranslation } from 'react-i18next';++type DescriptionContextData = {+ description: string;+ setDescription: (description: string) => void;+ saveDescription: () => void;+};++export const DescriptionContext = createContext<DescriptionContextData>({+ description: undefined,+ setDescription: () => {},+ saveDescription: () => {}+});++export type DescriptionContextProviderProps = {+ children: React.ReactNode;+};++export const DescriptionContextProvider = ({+ children+}: DescriptionContextProviderProps) => {+ const { t } = useTranslation();+ const dispatch: AppDispatch = useDispatch();+ const readOnlyMode = useSelector(isReadOnlyMode);+ const openedFiles: Array<OpenedEntry> = useSelector(getOpenedFiles);+ const openedFile: OpenedEntry = openedFiles[0];+ // const [ignored, forceUpdate] = useReducer(x => x + 1, 0, undefined);++ const description = useRef<string>(openedFile.description);++ useEffect(() => {+ if (openedFile.description !== description.current) {+ description.current = openedFile.description;+ } /*else {+ forceUpdate();+ }*/+ }, [openedFile.description]);++ const saveDescription = () => {+ if (readOnlyMode) {+ return;+ }+ if (!Pro) {+ dispatch(+ AppActions.showNotification(t('core:thisFunctionalityIsAvailableInPro'))+ );+ return;+ }+ if (!Pro.MetaOperations) {+ dispatch(+ AppActions.showNotification(t('Saving description not supported'))+ );+ return;+ }+ if (description.current !== undefined) {+ if (openedFile.locationId) {+ dispatch(AppActions.switchLocationTypeByID(openedFile.locationId)).then(+ currentLocationId => {+ saveMetaData()+ .then(() =>+ dispatch(+ AppActions.switchCurrentLocationType(currentLocationId)+ )+ )+ .catch(error => {+ console.warn('Error saving description ' + error);+ dispatch(+ AppActions.switchCurrentLocationType(currentLocationId)+ );+ dispatch(+ AppActions.showNotification(t('Error saving description'))+ );+ });+ }+ );+ } else {+ console.debug(+ 'openedFile:' ++ openedFile.path ++ ' dont have locationId! Current Location can be changed. Trying to save opened file in current location'+ );+ saveMetaData();+ }+ }+ };++ function saveMetaData() {+ return Pro.MetaOperations.saveFsEntryMeta(openedFile.path, {+ description: description.current+ }).then(entryMeta =>+ dispatch(AppActions.updateOpenedFile(openedFile.path, entryMeta))+ );+ }++ function setDescription(d: string) {+ description.current = d;+ }++ const context = useMemo(() => {+ return {+ description: description.current,+ setDescription,+ saveDescription+ };+ }, [description.current, openedFile.description]);++ return (+ <DescriptionContext.Provider value={context}>+ {children}+ </DescriptionContext.Provider>+ );+};diff --git a/app/components/hooks/useDescriptionContext.tsx b/app/components/hooks/useDescriptionContext.tsxnew file mode 100644index 000000000..8eb3417ee--- /dev/null+++ b/app/components/hooks/useDescriptionContext.tsx@@ -0,0 +1,5 @@+import { useContext } from 'react';++import { DescriptionContext } from './DescriptionContextProvider';++export const useDescriptionContext = () => useContext(DescriptionContext);diff --git a/app/containers/MainPage.tsx b/app/containers/MainPage.tsxindex 15d0fecde..a6a8cbd28 100644--- a/app/containers/MainPage.tsx+++ b/app/containers/MainPage.tsx@@ -94,6 +94,7 @@ import NewFileDialog from '-/components/dialogs/NewFileDialog';import IsTruncatedConfirmDialog from '-/components/dialogs/IsTruncatedConfirmDialog';import { styled, useTheme } from '@mui/material/styles';import { useTranslation } from 'react-i18next';+import { DescriptionContextProvider } from '-/components/hooks/DescriptionContextProvider';const drawerWidth = 320;const body = document.getElementsByTagName('body')[0];@@ -576,11 +577,13 @@ function MainPage(props: Props) {}/>{isFileOpened && (- <EntryContainer- key="EntryContainerID"- loadDirectoryContent={loadDirectoryContent}- openedFiles={openedFiles}- />+ <DescriptionContextProvider>+ <EntryContainer+ key="EntryContainerID"+ loadDirectoryContent={loadDirectoryContent}+ openedFiles={openedFiles}+ />+ </DescriptionContextProvider>)}</Split>);diff --git a/app/locales/en/core.json b/app/locales/en/core.jsonindex 6abac3170..48295b313 100644--- a/app/locales/en/core.json+++ b/app/locales/en/core.json@@ -803,5 +803,6 @@"generatedHTMLCopied": "Download links copied to clipboard","generatedHTMLFailed": "Error while copying to clipboard","details": "Details",+ "editDisabled": "Editing the description is disabled while editing the current file","-": "-"}diff --git a/app/perspectives/grid-perspective/components/MainToolbar.tsx b/app/perspectives/grid-perspective/components/MainToolbar.tsxindex 8e1d74039..bb5b8b39e 100644--- a/app/perspectives/grid-perspective/components/MainToolbar.tsx+++ b/app/perspectives/grid-perspective/components/MainToolbar.tsx@@ -89,7 +89,7 @@ function MainToolbar(props: Props) {const directoryPath = useSelector(getDirectoryPath);function showProperties() {- return dispatch(AppActions.openEntry(directoryPath));+ return dispatch(AppActions.openEntry(directoryPath, true));}return (diff --git a/app/reducers/settings-default.ts b/app/reducers/settings-default.tsindex 1fb4ad586..3b17995ea 100644--- a/app/reducers/settings-default.ts+++ b/app/reducers/settings-default.ts@@ -53,7 +53,7 @@ export default {newHTMLFileContent:'<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body></body></html>',showUnixHiddenEntries: false,- entryContainerTab: 0,+ entryContainerTab: undefined,checkForUpdates,reorderTags: false,coloredFileExtension: false,