Skip to content

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

feat: Added debounced auto-save (3s delay) for the description of files and folders, configurable in the settings

Ilian Sapundshievd3bcc83de77f
diff --git a/src/renderer/components/dialogs/components/SettingsGeneral.tsx b/src/renderer/components/dialogs/components/SettingsGeneral.tsx
index 708c68fe0..501fcf2ff 100644
--- a/src/renderer/components/dialogs/components/SettingsGeneral.tsx
+++ b/src/renderer/components/dialogs/components/SettingsGeneral.tsx
@@ -578,6 +578,33 @@ function SettingsGeneral() {
</ListItem>
),
},
+ {
+ label: t('core:autoSaveDescription'),
+ jsx: (
+ <ListItem>
+ <ListItemText
+ primary={
+ <>
+ {t('core:autoSaveDescription')}
+ <ProLabel />
+ </>
+ }
+ />
+ <Switch
+ data-tid="settingsAutoSaveDescriptionTID"
+ disabled={!Pro}
+ onClick={() =>
+ dispatch(
+ SettingsActions.setAutoSaveDescription(
+ !settings.autoSaveDescription,
+ ),
+ )
+ }
+ checked={settings.autoSaveDescription}
+ />
+ </ListItem>
+ ),
+ },
{
label: t('core:tagBackgroundColor'),
jsx: (
diff --git a/src/renderer/hooks/FilePropertiesContextProvider.tsx b/src/renderer/hooks/FilePropertiesContextProvider.tsx
index 65dd15e37..68e42c6ac 100644
--- a/src/renderer/hooks/FilePropertiesContextProvider.tsx
+++ b/src/renderer/hooks/FilePropertiesContextProvider.tsx
@@ -27,11 +27,13 @@ import React, {
import { Pro } from '-/pro';
import AppConfig from '-/AppConfig';
import { useTranslation } from 'react-i18next';
+import { useSelector } from 'react-redux';
import { useOpenedEntryContext } from '-/hooks/useOpenedEntryContext';
import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
import { useNotificationContext } from '-/hooks/useNotificationContext';
import { TS } from '-/tagspaces.namespace';
import { useIOActionsContext } from '-/hooks/useIOActionsContext';
+import { isAutoSaveDescription } from '-/reducers/settings';
type FilePropertiesContextData = {
description: string;
@@ -73,8 +75,10 @@ export const FilePropertiesContextProvider = ({
const { showNotification, openConfirmDialog } = useNotificationContext();
const { setDescriptionChange } = useIOActionsContext();
+ const autoSaveDesc = useSelector(isAutoSaveDescription);
const lastOpenedFile = useRef<TS.OpenedEntry>({ ...openedEntry });
const isDescriptionChanged = useRef<boolean>(false);
+ const autoSaveTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
const [isEditMode, setIsEditMode] = useState<boolean>(false);
const [isEditDescriptionMode, setIsEditDescriptionMode] =
@@ -160,6 +164,14 @@ export const FilePropertiesContextProvider = ({
});
}
+ useEffect(() => {
+ return () => {
+ if (autoSaveTimer.current) {
+ clearTimeout(autoSaveTimer.current);
+ }
+ };
+ }, []);
+
function setDescription(d: string, changed = true) {
if (lastOpenedFile.current?.meta?.description !== d) {
lastOpenedFile.current = {
@@ -173,6 +185,23 @@ export const FilePropertiesContextProvider = ({
isDescriptionChanged.current = changed;
forceUpdate();
}
+ if (changed && autoSaveDesc && Pro) {
+ if (autoSaveTimer.current) {
+ clearTimeout(autoSaveTimer.current);
+ }
+ autoSaveTimer.current = setTimeout(() => {
+ const entry = lastOpenedFile.current;
+ const location = findLocation(entry?.locationID);
+ if (entry && location && !location.isReadOnly) {
+ setDescriptionChange(entry, entry.meta?.description ?? '').then(
+ () => {
+ isDescriptionChanged.current = false;
+ forceUpdate();
+ },
+ );
+ }
+ }, 3000);
+ }
}
}
diff --git a/src/renderer/locales/en/core.json b/src/renderer/locales/en/core.json
index ec25104d2..c783471af 100644
--- a/src/renderer/locales/en/core.json
+++ b/src/renderer/locales/en/core.json
@@ -77,6 +77,7 @@
"extensionsTab": "Extensions",
"enableDevMode": "Enable Developer Mode",
"hideProFeatures": "Hide Pro Features",
+ "autoSaveDescription": "Auto-save description changes",
"fileViewer": "File Viewer",
"reloadDocument": "Reload Document",
"selectAllFiles": "Select all files",
diff --git a/src/renderer/pro/index.ts b/src/renderer/pro/index.ts
index 6dc1d8da4..0f7379aa1 100644
--- a/src/renderer/pro/index.ts
+++ b/src/renderer/pro/index.ts
@@ -34,4 +34,6 @@ if (AppConfig.isCordovaAndroid) {
tsPro = undefined;
}
+// tsPro = undefined; // disable pro
+
export { tsPro as Pro };
diff --git a/src/renderer/reducers/settings-default.ts b/src/renderer/reducers/settings-default.ts
index 72729bbdc..1d2d12bf2 100644
--- a/src/renderer/reducers/settings-default.ts
+++ b/src/renderer/reducers/settings-default.ts
@@ -69,6 +69,7 @@ export default {
desktopMode,
devMode: AppConfig.ExtDevMode ? AppConfig.ExtDevMode : false,
hideProFeatures: false,
+ autoSaveDescription: false,
saveTagInLocation:
typeof AppConfig.ExtUseLocationTags === 'undefined'
? false
diff --git a/src/renderer/reducers/settings.ts b/src/renderer/reducers/settings.ts
index 42b54603d..d92aba779 100644
--- a/src/renderer/reducers/settings.ts
+++ b/src/renderer/reducers/settings.ts
@@ -106,6 +106,7 @@ export const types = {
SET_FOLDER_OPEN_HISTORY: 'SET_FOLDER_OPEN_HISTORY',
SET_FILE_EDIT_HISTORY: 'SET_FILE_EDIT_HISTORY',
SET_HIDE_PRO_FEATURES: 'SETTINGS/SET_HIDE_PRO_FEATURES',
+ SET_AUTO_SAVE_DESCRIPTION: 'SETTINGS/SET_AUTO_SAVE_DESCRIPTION',
};
function generateUniqueName(array: Array<any>, baseName: string): string {
@@ -199,6 +200,9 @@ export default (state: any = defaultSettings, action: any) => {
case types.SET_HIDE_PRO_FEATURES: {
return { ...state, hideProFeatures: action.hideProFeatures };
}
+ case types.SET_AUTO_SAVE_DESCRIPTION: {
+ return { ...state, autoSaveDescription: action.autoSaveDescription };
+ }
case types.SET_ENABLE_WS: {
return { ...state, enableWS: action.enableWS };
}
@@ -634,6 +638,10 @@ export const actions = {
type: types.SET_HIDE_PRO_FEATURES,
hideProFeatures,
}),
+ setAutoSaveDescription: (autoSaveDescription: boolean) => ({
+ type: types.SET_AUTO_SAVE_DESCRIPTION,
+ autoSaveDescription,
+ }),
setEnableWS: (enableWS: boolean) => ({
type: types.SET_ENABLE_WS,
enableWS,
@@ -919,6 +927,8 @@ export const isHideProFeatures = (state: any) =>
AppConfig.ExtHideProFeatures !== undefined
? AppConfig.ExtHideProFeatures
: state.settings.hideProFeatures;
+export const isAutoSaveDescription = (state: any) =>
+ state.settings.autoSaveDescription;
export const isRevisionsEnabled = (state: any) =>
state.settings.isRevisionsEnabled;
export const isReorderTags = (state: any) => state.settings.reorderTags;