Skip to content

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

Don't close file properties if it's have changes in file or description (#2325)

Stanimir1b49d7956a87
diff --git a/package-lock.json b/package-lock.json
index 9c5996c77..ff4bb8af8 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -13769,7 +13769,7 @@
"node_modules/@electron/node-gyp": {
"version": "10.2.0-electron.1",
"resolved": "git+ssh://[email protected]/electron/node-gyp.git#06b29aafb7708acef8b3669835c8a7857ebc92d2",
- "integrity": "sha512-CrYo6TntjpoMO1SHjl5Pa/JoUsECNqNdB7Kx49WLQpWzPw53eEITJ2Hs9fh/ryUYDn4pxZz11StaBYBrLFJdqg==",
+ "integrity": "sha512-4MSBTT8y07YUDqf69/vSh80Hh791epYqGtWHO3zSKhYFwQg+gx9wi1PqbqP6YqC4WMsNxZ5l9oDmnWdK5pfCKQ==",
"dev": true,
"license": "MIT",
"dependencies": {
@@ -20038,9 +20038,9 @@
}
},
"node_modules/@types/lodash": {
- "version": "4.17.17",
- "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.17.tgz",
- "integrity": "sha512-RRVJ+J3J+WmyOTqnz3PiBLA501eKwXl2noseKOrNo/6+XEHjTAxO4xHvxQB6QuNm+s4WRbn6rSiap8+EA+ykFQ==",
+ "version": "4.17.18",
+ "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.18.tgz",
+ "integrity": "sha512-KJ65INaxqxmU6EoCiJmRPZC9H9RVWCRd349tXM2M3O5NA7cY6YL7c0bHAHQ93NOfTObEQ004kd2QVHs/r0+m4g==",
"license": "MIT"
},
"node_modules/@types/lodash-es": {
@@ -26610,9 +26610,9 @@
}
},
"node_modules/electron-to-chromium": {
- "version": "1.5.168",
- "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.168.tgz",
- "integrity": "sha512-RUNQmFLNIWVW6+z32EJQ5+qx8ci6RGvdtDC0Ls+F89wz6I2AthpXF0w0DIrn2jpLX0/PU9ZCo+Qp7bg/EckJmA==",
+ "version": "1.5.169",
+ "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.169.tgz",
+ "integrity": "sha512-q7SQx6mkLy0GTJK9K9OiWeaBMV4XQtBSdf6MJUzDB/H/5tFXfIiX38Lci1Kl6SsgiEhz1SQI1ejEOU5asWEhwQ==",
"license": "ISC"
},
"node_modules/electron-updater": {
@@ -47639,9 +47639,9 @@
}
},
"node_modules/terser": {
- "version": "5.42.0",
- "resolved": "https://registry.npmjs.org/terser/-/terser-5.42.0.tgz",
- "integrity": "sha512-UYCvU9YQW2f/Vwl+P0GfhxJxbUGLwd+5QrrGgLajzWAtC/23AX0vcise32kkP7Eu0Wu9VlzzHAXkLObgjQfFlQ==",
+ "version": "5.43.0",
+ "resolved": "https://registry.npmjs.org/terser/-/terser-5.43.0.tgz",
+ "integrity": "sha512-CqNNxKSGKSZCunSvwKLTs8u8sGGlp27sxNZ4quGh0QeNuyHM0JSEM/clM9Mf4zUp6J+tO2gUXhgXT2YMMkwfKQ==",
"license": "BSD-2-Clause",
"dependencies": {
"@jridgewell/source-map": "^0.3.3",
diff --git a/src/renderer/components/EditDescriptionButtons.tsx b/src/renderer/components/EditDescriptionButtons.tsx
index b49898ead..a2e11dddf 100644
--- a/src/renderer/components/EditDescriptionButtons.tsx
+++ b/src/renderer/components/EditDescriptionButtons.tsx
@@ -164,7 +164,7 @@ const EditDescriptionButtons: React.FC<ButtonsProps> = ({
}
disabled={
!Pro ||
- isEditMode ||
+ //isEditMode ||
findLocation(openedEntry.locationID)?.isReadOnly
}
style={{
diff --git a/src/renderer/components/EntryContainer.tsx b/src/renderer/components/EntryContainer.tsx
index 3a8efa5d6..c8e7e48af 100644
--- a/src/renderer/components/EntryContainer.tsx
+++ b/src/renderer/components/EntryContainer.tsx
@@ -69,7 +69,6 @@ function EntryContainer() {
const dispatch: AppDispatch = useDispatch();
const {
openedEntry,
- closeAllFiles,
reloadOpenedFile,
toggleEntryFullWidth,
isEntryInFullWidth,
@@ -78,7 +77,7 @@ function EntryContainer() {
} = useOpenedEntryContext();
const { setActions } = usePerspectiveActionsContext();
const { toggleFullScreen } = useFullScreenContext();
- const { saveDescription, isEditMode, setEditMode } =
+ const { saveDescription, isEditMode, setEditMode, closeOpenedEntries } =
useFilePropertiesContext();
const { setAutoSave } = useIOActionsContext();
const { findLocation } = useCurrentLocationContext();
@@ -397,7 +396,7 @@ function EntryContainer() {
};
const closeFile = () => {
- closeAllFiles();
+ closeOpenedEntries();
// setEditingSupported(false);
};
diff --git a/src/renderer/components/md/DescriptionMdEditor.tsx b/src/renderer/components/md/DescriptionMdEditor.tsx
index a07569c99..f78ed6174 100644
--- a/src/renderer/components/md/DescriptionMdEditor.tsx
+++ b/src/renderer/components/md/DescriptionMdEditor.tsx
@@ -122,6 +122,18 @@ const DescriptionMdEditor = forwardRef<CrepeRef, CrepeMdEditorProps>(
}
}, [openedEntry]); //, isDescriptionChanged]);
+ useEffect(() => {
+ const crepe = crepeRef.current;
+ if (!loading && crepe && crepe.editor.status === EditorStatus.Created) {
+ // Double‐check that the current editor content differs:
+ try {
+ crepe.setReadonly(!isEditDescriptionMode);
+ } catch (e) {
+ console.error('Failed to setReadonly:', e);
+ }
+ }
+ }, [isEditDescriptionMode]);
+
// If some external “metaActions” say “hey, description was updated,”
// we re‐push it:
useEffect(() => {
diff --git a/src/renderer/hooks/FilePropertiesContextProvider.tsx b/src/renderer/hooks/FilePropertiesContextProvider.tsx
index 9499e3de4..58d22a9b7 100644
--- a/src/renderer/hooks/FilePropertiesContextProvider.tsx
+++ b/src/renderer/hooks/FilePropertiesContextProvider.tsx
@@ -45,6 +45,7 @@ type FilePropertiesContextData = {
setSaveDescriptionConfirmOpened: (open: boolean) => void;
setDescription: (description: string) => void;
saveDescription: () => Promise<boolean>;
+ closeOpenedEntries: () => void;
};
export const FilePropertiesContext = createContext<FilePropertiesContextData>({
@@ -58,6 +59,7 @@ export const FilePropertiesContext = createContext<FilePropertiesContextData>({
setSaveDescriptionConfirmOpened: () => {},
setDescription: () => {},
saveDescription: undefined,
+ closeOpenedEntries: undefined,
});
export type FilePropertiesContextProviderProps = {
@@ -68,24 +70,25 @@ export const FilePropertiesContextProvider = ({
children,
}: FilePropertiesContextProviderProps) => {
const { t } = useTranslation();
- const { openedEntry } = useOpenedEntryContext();
+ const { openedEntry, actuallyCloseFiles, openFsEntry } =
+ useOpenedEntryContext();
const { findLocation } = useCurrentLocationContext();
const { showNotification } = useNotificationContext();
const { setDescriptionChange } = useIOActionsContext();
const lastOpenedFile = useRef<TS.OpenedEntry>({ ...openedEntry });
const isDescriptionChanged = useRef<boolean>(false);
+ const isSaveDescriptionConfirmOpened = useRef<boolean>(false);
const [isEditMode, setIsEditMode] = useState<boolean>(false);
const [isEditDescriptionMode, setIsEditDescriptionMode] =
useState<boolean>(true);
- const [isSaveDescriptionConfirmOpened, saveDescriptionConfirmOpened] =
- useState<boolean>(false);
const [ignored, forceUpdate] = useReducer((x) => x + 1, 0, undefined);
useEffect(() => {
if (openedEntry) {
+ // auto set EditDescriptionMode on opening
const openedLocation = findLocation(openedEntry.locationID);
if (openedLocation) {
if (openedLocation.isReadOnly) {
@@ -96,27 +99,40 @@ export const FilePropertiesContextProvider = ({
setIsEditDescriptionMode(true);
}
}
- if (isDescriptionChanged.current) {
- // handle not saved changes
- saveDescriptionConfirmOpened(true);
- } else if (
+
+ if (
JSON.stringify(lastOpenedFile.current) !== JSON.stringify(openedEntry)
) {
if (lastOpenedFile.current?.path !== openedEntry.path) {
- setIsEditMode(false);
+ if (isDescriptionChanged.current) {
+ isDescriptionChanged.current = false;
+ // handle not saved changes
+ isSaveDescriptionConfirmOpened.current = true;
+ } else {
+ lastOpenedFile.current = { ...openedEntry };
+ setIsEditMode(false);
+ }
+ } else {
+ lastOpenedFile.current = { ...openedEntry };
}
- lastOpenedFile.current = { ...openedEntry };
forceUpdate();
}
- } else {
+ }
+ }, [openedEntry]);
+
+ function closeOpenedEntries() {
+ if (openedEntry) {
if (isDescriptionChanged.current) {
- saveDescriptionConfirmOpened(true);
+ isDescriptionChanged.current = false;
+ // handle not saved changes
+ isSaveDescriptionConfirmOpened.current = true;
+ forceUpdate();
+ } else {
+ actuallyCloseFiles();
}
- //lastOpenedFile.current = undefined;
}
- isDescriptionChanged.current = false;
- }, [openedEntry]);
+ }
function saveDescription(): Promise<boolean> {
if (!lastOpenedFile.current) return Promise.resolve(false);
@@ -195,17 +211,18 @@ export const FilePropertiesContextProvider = ({
function setSaveDescriptionConfirmOpened(isOpened: boolean) {
if (!isOpened) {
isDescriptionChanged.current = false;
- lastOpenedFile.current = { ...openedEntry };
setIsEditDescriptionMode(false);
}
- saveDescriptionConfirmOpened(isOpened);
+ lastOpenedFile.current = { ...openedEntry };
+ isSaveDescriptionConfirmOpened.current = isOpened;
+ forceUpdate();
}
const context = useMemo(
() => ({
description: lastOpenedFile.current?.meta?.description,
isDescriptionChanged: isDescriptionChanged.current,
- isSaveDescriptionConfirmOpened,
+ isSaveDescriptionConfirmOpened: isSaveDescriptionConfirmOpened.current,
setSaveDescriptionConfirmOpened,
setDescription,
saveDescription,
@@ -213,6 +230,7 @@ export const FilePropertiesContextProvider = ({
setEditMode,
isEditDescriptionMode,
setEditDescriptionMode,
+ closeOpenedEntries,
}),
[
openedEntry,
@@ -220,7 +238,7 @@ export const FilePropertiesContextProvider = ({
lastOpenedFile.current,
isEditMode,
isEditDescriptionMode,
- isSaveDescriptionConfirmOpened,
+ isSaveDescriptionConfirmOpened.current,
],
);
@@ -228,15 +246,18 @@ export const FilePropertiesContextProvider = ({
<FilePropertiesContext.Provider value={context}>
{children}
<ConfirmDialog
- open={isSaveDescriptionConfirmOpened}
+ open={isSaveDescriptionConfirmOpened.current}
onClose={() => setSaveDescriptionConfirmOpened(false)}
title={t('core:confirm')}
content={t('core:saveDescriptionOnClosing')}
confirmCallback={(result) => {
if (result) {
- saveDescription();
+ saveDescription().then(() => {
+ openFsEntry(openedEntry);
+ });
} else {
setSaveDescriptionConfirmOpened(false);
+ openFsEntry(openedEntry);
}
}}
cancelDialogTID="cancelSaveDescCloseDialog"
diff --git a/src/renderer/hooks/OpenedEntryContextProvider.tsx b/src/renderer/hooks/OpenedEntryContextProvider.tsx
index 8573d00d4..d1565c5f4 100644
--- a/src/renderer/hooks/OpenedEntryContextProvider.tsx
+++ b/src/renderer/hooks/OpenedEntryContextProvider.tsx
@@ -83,7 +83,10 @@ type OpenedEntryContextData = {
setEntryInFullWidth: (fullWidth: boolean) => void;
setFileChanged: (isChanged: boolean) => void;
addToEntryContainer: (fsEntry: TS.OpenedEntry) => void;
- closeAllFiles: () => void;
+ /**
+ * don't use this direct. In FilePropertiesContextProvider have wrapper function closeOpenedEntries that check for unsaved changes before close file
+ */
+ actuallyCloseFiles: () => void;
reflectUpdateOpenedFileContent: (entry: TS.FileSystemEntry) => void;
reloadOpenedFile: () => Promise<boolean>;
/*updateOpenedFile: (
@@ -125,7 +128,7 @@ export const OpenedEntryContext = createContext<OpenedEntryContextData>({
setEntryInFullWidth: undefined,
setFileChanged: undefined,
addToEntryContainer: () => {},
- closeAllFiles: () => {},
+ actuallyCloseFiles: () => {},
reflectUpdateOpenedFileContent: () => {},
reloadOpenedFile: undefined,
//updateOpenedFile: () => Promise.resolve(false),
@@ -219,7 +222,7 @@ export const OpenedEntryContextProvider = ({
currentEntry.current &&
currentEntry.current.path.startsWith(action.entry.path)
) {
- closeAllFiles();
+ actuallyCloseFiles();
}
} else if (action.action === 'update' || action.action === 'move') {
if (
@@ -477,7 +480,7 @@ export const OpenedEntryContextProvider = ({
});
}
- function closeAllFiles() {
+ function actuallyCloseFiles() {
const appName = versionMeta.name;
document.title = appName;
if (currentLocation) {
@@ -1064,7 +1067,7 @@ export const OpenedEntryContextProvider = ({
setEntryInFullWidth,
setFileChanged,
addToEntryContainer,
- closeAllFiles,
+ actuallyCloseFiles,
reflectUpdateOpenedFileContent,
reloadOpenedFile,
//updateOpenedFile,