Skip to content

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

add history functionality for the opened files and folders (#2164)

Stanimir16c2951957c5
diff --git a/package-lock.json b/package-lock.json
index ad6f400ad..15d62d394 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -198,7 +198,7 @@
"yarn": "please-use-npm"
},
"optionalDependencies": {
- "@tagspacespro/tagspacespro": "5.0.71"
+ "@tagspacespro/tagspacespro": "5.0.72"
}
},
"node_modules/@adobe/css-tools": {
@@ -17215,9 +17215,9 @@
}
},
"node_modules/@tagspacespro/tagspacespro": {
- "version": "5.0.71",
- "resolved": "https://npm.pkg.github.com/download/@tagspacespro/tagspacespro/5.0.71/abf9c913b5985cb4f35303f6b12b9cf30a29f7b2",
- "integrity": "sha512-8yavd9ZGKS9qbqM+2AC82qFRK6irBVqTg/XCMenLrUf5B89Kd2kYFS8OTi9QfiAzFTiftM2KpNkhAKmBtM2Ujw==",
+ "version": "5.0.72",
+ "resolved": "https://npm.pkg.github.com/download/@tagspacespro/tagspacespro/5.0.72/18a8aead5c506ee4af98b1e4cdf7b3fa87ac5041",
+ "integrity": "sha512-R1JZUhG4d55++9PjhINj0ed4pgmBhgcxvNPcHvA3Ww/xvEW1VFajHwFBcyhAYFLmcbf1oAd9QDJQtKdtLGLz2w==",
"license": "Proprietary",
"optional": true
},
diff --git a/package.json b/package.json
index 15310b5e5..6e5b51671 100644
--- a/package.json
+++ b/package.json
@@ -235,7 +235,7 @@
"uuid": "^9.0.0"
},
"optionalDependencies": {
- "@tagspacespro/tagspacespro": "5.0.71"
+ "@tagspacespro/tagspacespro": "5.0.72"
},
"devDependencies": {
"@electron/notarize": "^2.3.2",
diff --git a/src/main/main.ts b/src/main/main.ts
index d13f72ef6..31ae964d0 100644
--- a/src/main/main.ts
+++ b/src/main/main.ts
@@ -204,12 +204,12 @@ function openHelpFeedbackPanel() {
function goBack() {
const focusedWindow = BrowserWindow.getFocusedWindow();
- focusedWindow?.webContents.send('opened-entry', 'go-back');
+ focusedWindow?.webContents.send('history', 'go-back');
}
function goForward() {
const focusedWindow = BrowserWindow.getFocusedWindow();
- focusedWindow?.webContents.send('opened-entry', 'go-forward');
+ focusedWindow?.webContents.send('history', 'go-forward');
}
function setZoomResetApp() {
diff --git a/src/main/preload.ts b/src/main/preload.ts
index 363613170..45e6a18d4 100644
--- a/src/main/preload.ts
+++ b/src/main/preload.ts
@@ -62,7 +62,7 @@ export type Channels =
| 'toggle-third-party-libs-dialog'
| 'perspective'
| 'panels'
- | 'opened-entry'
+ | 'history'
| 'progress'
| 'uploadAbort';
diff --git a/src/renderer/components/FolderContainer.tsx b/src/renderer/components/FolderContainer.tsx
index 587534265..5f6022e40 100644
--- a/src/renderer/components/FolderContainer.tsx
+++ b/src/renderer/components/FolderContainer.tsx
@@ -30,11 +30,7 @@ import ToggleButton from '@mui/material/ToggleButton';
import ToggleButtonGroup from '@mui/material/ToggleButtonGroup';
import AppConfig from '-/AppConfig';
import { getDesktopMode, getKeyBindingObject } from '-/reducers/settings';
-import {
- actions as AppActions,
- AppDispatch,
- getProgress,
-} from '../reducers/app';
+import { getProgress } from '../reducers/app';
import {
GoBackIcon,
GoForwardIcon,
@@ -56,20 +52,20 @@ import { useProTeaserDialogContext } from '-/components/dialogs/hooks/useProTeas
import CustomDragLayer from '-/components/CustomDragLayer';
import TargetFileBox from '-/components/TargetFileBox';
import { NativeTypes } from 'react-dnd-html5-backend';
+import { useBrowserHistoryContext } from '-/hooks/useBrowserHistoryContext';
interface Props {
toggleDrawer?: () => void;
drawerOpened: boolean;
- goBack: () => void;
- goForward: () => void;
}
function FolderContainer(props: Props) {
- const { toggleDrawer, goBack, goForward, drawerOpened } = props;
+ const { toggleDrawer, drawerOpened } = props;
const { t } = useTranslation();
const theme = useTheme();
const keyBindings = useSelector(getKeyBindingObject);
+ const { goForward, goBack, historyIndex } = useBrowserHistoryContext();
const { openFileUploadDialog } = useFileUploadDialogContext();
const { openProTeaserDialog } = useProTeaserDialogContext();
const {
@@ -229,7 +225,7 @@ function FolderContainer(props: Props) {
>
<IconButton
id="goBackButton"
- disabled={window.history.length < 2}
+ disabled={historyIndex === 0}
onClick={goBack}
style={{
// @ts-ignore
@@ -243,7 +239,7 @@ function FolderContainer(props: Props) {
<Tooltip title={t('core:goforward') + ' - BETA'}>
<IconButton
id="goForwardButton"
- disabled={window.history.length < 2}
+ disabled={historyIndex === 0}
onClick={goForward}
style={{
// @ts-ignore
diff --git a/src/renderer/components/RenderHistory.tsx b/src/renderer/components/RenderHistory.tsx
index 6d8b11d0d..71522517e 100644
--- a/src/renderer/components/RenderHistory.tsx
+++ b/src/renderer/components/RenderHistory.tsx
@@ -34,6 +34,7 @@ import { dataTidFormat } from '-/services/test';
import { useTranslation } from 'react-i18next';
import AppConfig from '-/AppConfig';
import TooltipTS from '-/components/Tooltip';
+import { useBrowserHistoryContext } from '-/hooks/useBrowserHistoryContext';
interface Props {
historyKey: string;
@@ -44,6 +45,7 @@ interface Props {
}
function RenderHistory(props: Props) {
const { t } = useTranslation();
+ const { openHistoryItem } = useBrowserHistoryContext();
const bookmarksContext = Pro?.contextProviders?.BookmarksContext
? useContext<TS.BookmarksContextData>(Pro.contextProviders.BookmarksContext)
: undefined;
@@ -73,9 +75,7 @@ function RenderHistory(props: Props) {
fontWeight: 'normal',
justifyContent: 'start',
}}
- onClick={() =>
- historyContext.openItem(item as TS.HistoryItem)
- }
+ onClick={() => openHistoryItem(item as TS.HistoryItem)}
>
<Tooltip
title={
diff --git a/src/renderer/components/SearchAutocomplete.tsx b/src/renderer/components/SearchAutocomplete.tsx
index 8563d8ce7..3f8fb3ce0 100644
--- a/src/renderer/components/SearchAutocomplete.tsx
+++ b/src/renderer/components/SearchAutocomplete.tsx
@@ -72,6 +72,7 @@ import { useLocationIndexContext } from '-/hooks/useLocationIndexContext';
import { removePrefix } from '-/services/utils-io';
import { isDesktopMode } from '-/reducers/settings';
import { useSavedSearchesContext } from '-/hooks/useSavedSearchesContext';
+import { useBrowserHistoryContext } from '-/hooks/useBrowserHistoryContext';
interface Props {
style?: any;
@@ -104,6 +105,7 @@ function SearchAutocomplete(props: Props) {
searchQuery,
setSearchQuery,
} = useDirectoryContentContext();
+ const { openHistoryItem } = useBrowserHistoryContext();
const { isIndexing, searchAllLocations, searchLocationIndex } =
useLocationIndexContext();
const { searches } = useSavedSearchesContext();
@@ -887,28 +889,28 @@ function SearchAutocomplete(props: Props) {
setSearchQuery(option.searchQuery);
} else {
}
- } else if (Pro && historyContext) {
+ } else if (Pro) {
const item: TS.HistoryItem = {
path: option.label,
url: option.fullName,
lid: option.id,
creationTimeStamp: 0,
};
- historyContext.openItem(item);
+ openHistoryItem(item);
}
searchOptions.current = getSearchOptions();
currentOptions.current = undefined;
isOpen.current = false;
return [];
} else if (option.action === ExecActions.OPEN_BOOKMARK) {
- if (Pro && historyContext) {
+ if (Pro) {
const item: TS.HistoryItem = {
path: option.label,
url: option.fullName,
lid: undefined,
creationTimeStamp: 0,
};
- historyContext.openItem(item);
+ openHistoryItem(item);
}
searchOptions.current = getSearchOptions();
currentOptions.current = undefined;
diff --git a/src/renderer/containers/MainPage.tsx b/src/renderer/containers/MainPage.tsx
index 990ad9de4..31447418d 100644
--- a/src/renderer/containers/MainPage.tsx
+++ b/src/renderer/containers/MainPage.tsx
@@ -43,6 +43,7 @@ import { useOpenedEntryContext } from '-/hooks/useOpenedEntryContext';
import { useDirectoryContentContext } from '-/hooks/useDirectoryContentContext';
import { usePanelsContext } from '-/hooks/usePanelsContext';
import { useUserContext } from '-/hooks/useUserContext';
+import { useBrowserHistoryContext } from '-/hooks/useBrowserHistoryContext';
const drawerWidth = 320;
const body = document.getElementsByTagName('body')[0];
@@ -92,14 +93,8 @@ interface Props {
}
function MainPage(props: Props) {
- const {
- openLink,
- openedEntry,
- isEntryInFullWidth,
- goForward,
- goBack,
- setEntryInFullWidth,
- } = useOpenedEntryContext();
+ const { openLink, openedEntry, isEntryInFullWidth, setEntryInFullWidth } =
+ useOpenedEntryContext();
const {
loadParentDirectoryContent,
@@ -270,8 +265,6 @@ function MainPage(props: Props) {
<FolderContainer
toggleDrawer={toggleDrawer}
drawerOpened={drawerOpened}
- goBack={goBack}
- goForward={goForward}
/*openMoveCopyFilesDialog={() =>
setMoveCopyDialogOpened(props.selectedEntries)
}*/
diff --git a/src/renderer/containers/Root.tsx b/src/renderer/containers/Root.tsx
index e033ba88c..af5198a57 100644
--- a/src/renderer/containers/Root.tsx
+++ b/src/renderer/containers/Root.tsx
@@ -50,6 +50,7 @@ import { PanelsContextProvider } from '-/hooks/PanelsContextProvider';
import { UserContextProvider } from '-/hooks/UserContextProvider';
import { SavedSearchesContextProvider } from '-/hooks/SavedSearchesContextProvider';
import DialogsRoot from '-/containers/DialogsRoot';
+import { BrowserHistoryContextProvider } from '-/hooks/BrowserHistoryContextProvider';
type RootType = {
store: Store<{}>;
@@ -92,21 +93,23 @@ export default function Root({ store, persistor }: RootType) {
<PanelsContextProvider>
<UserContextProvider>
<SavedSearchesContextProvider>
- {Pro ? (
- <Pro.contextProviders.BookmarksContextProvider>
- <Pro.contextProviders.HistoryContextProvider>
- <Pro.contextProviders.KanBanImportDialogContextProvider>
- <DialogsRoot>
- <MainPage />
- </DialogsRoot>
- </Pro.contextProviders.KanBanImportDialogContextProvider>
- </Pro.contextProviders.HistoryContextProvider>
- </Pro.contextProviders.BookmarksContextProvider>
- ) : (
- <DialogsRoot>
- <MainPage />
- </DialogsRoot>
- )}
+ <BrowserHistoryContextProvider>
+ {Pro ? (
+ <Pro.contextProviders.BookmarksContextProvider>
+ <Pro.contextProviders.HistoryContextProvider>
+ <Pro.contextProviders.KanBanImportDialogContextProvider>
+ <DialogsRoot>
+ <MainPage />
+ </DialogsRoot>
+ </Pro.contextProviders.KanBanImportDialogContextProvider>
+ </Pro.contextProviders.HistoryContextProvider>
+ </Pro.contextProviders.BookmarksContextProvider>
+ ) : (
+ <DialogsRoot>
+ <MainPage />
+ </DialogsRoot>
+ )}
+ </BrowserHistoryContextProvider>
</SavedSearchesContextProvider>
</UserContextProvider>
</PanelsContextProvider>
diff --git a/src/renderer/hooks/BrowserHistoryContextProvider.tsx b/src/renderer/hooks/BrowserHistoryContextProvider.tsx
new file mode 100644
index 000000000..7ed3012b4
--- /dev/null
+++ b/src/renderer/hooks/BrowserHistoryContextProvider.tsx
@@ -0,0 +1,172 @@
+/*
+Copyright (c) 2024-present The TagSpaces UG (Haftungsbeschraenkt). All rights reserved.
+*/
+
+import React, { createContext, useEffect, useReducer, useRef } from 'react';
+import { TS } from '-/tagspaces.namespace';
+import { generateSharingLink } from '@tagspaces/tagspaces-common/paths';
+import { useOpenedEntryContext } from '-/hooks/useOpenedEntryContext';
+import { getRelativeEntryPath } from '-/services/utils-io';
+import { useDirectoryContentContext } from '-/hooks/useDirectoryContentContext';
+import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
+import AppConfig from '-/AppConfig';
+import { FixedSizeArray } from '-/services/FixedSizeArray';
+
+type BrowserHistoryContextData = {
+ historyIndex: number;
+ goBack: () => void;
+ goForward: () => void;
+ openHistoryItem: (item: TS.HistoryItem) => void;
+};
+
+export const BrowserHistoryContext = createContext<BrowserHistoryContextData>({
+ historyIndex: undefined,
+ goBack: undefined,
+ goForward: undefined,
+ openHistoryItem: undefined,
+});
+
+export type BrowserHistoryContextProviderProps = {
+ children: React.ReactNode;
+};
+
+export const BrowserHistoryContextProvider = ({
+ children,
+}: BrowserHistoryContextProviderProps) => {
+ const { currentLocation, openLocationById } = useCurrentLocationContext();
+ const { openedEntry, openLink, openEntry } = useOpenedEntryContext();
+ const { currentLocationPath, currentDirectoryPath } =
+ useDirectoryContentContext();
+ const history = useRef<FixedSizeArray<TS.HistoryItem>>(
+ new FixedSizeArray<TS.HistoryItem>(50),
+ );
+ const historyIndex = useRef<number>(0);
+ const [ignored, forceUpdate] = useReducer((x) => x + 1, 0, undefined);
+
+ useEffect(() => {
+ if (AppConfig.isElectron) {
+ window.electronIO.ipcRenderer.on('history', (arg) => {
+ if (arg === 'go-back') {
+ goBack();
+ } else if (arg === 'go-forward') {
+ goForward();
+ }
+ });
+
+ return () => {
+ if (window.electronIO.ipcRenderer) {
+ window.electronIO.ipcRenderer.removeAllListeners('history');
+ }
+ };
+ }
+ }, []);
+
+ useEffect(() => {
+ if (openedEntry) {
+ const relEntryPath = getRelativeEntryPath(
+ currentLocationPath,
+ openedEntry.path,
+ );
+ if (openedEntry.isFile) {
+ addHistory(
+ openedEntry.path,
+ generateSharingLink(openedEntry.locationID, relEntryPath),
+ openedEntry.locationID,
+ );
+ } else {
+ addHistory(
+ openedEntry.path,
+ generateSharingLink(
+ openedEntry.locationID,
+ relEntryPath,
+ relEntryPath,
+ ),
+ openedEntry.locationID,
+ );
+ }
+ }
+ }, [openedEntry]);
+
+ useEffect(() => {
+ if (
+ currentLocation &&
+ currentDirectoryPath &&
+ currentLocation.path !== currentDirectoryPath
+ ) {
+ const relEntryPath = getRelativeEntryPath(
+ currentLocationPath,
+ currentDirectoryPath,
+ );
+ addHistory(
+ currentDirectoryPath,
+ generateSharingLink(currentLocation.uuid, relEntryPath, relEntryPath),
+ currentLocation.uuid,
+ );
+ }
+ }, [currentDirectoryPath]);
+
+ function addHistory(path, url, lid) {
+ const lastItem = lastHistoryItem();
+ if (!lastItem || lastItem.path !== path) {
+ const sliced = history.current.slice(0, historyIndex.current + 1);
+ //const filtered = sliced.filter((item) => item.path !== path); // unique history items
+ history.current.setArray([...sliced, { path, url, lid }]);
+ historyIndex.current = history.current.getArray().length - 1;
+ forceUpdate();
+ }
+ }
+
+ /*function haveHistory() {
+ return historyIndex.current > 0;
+ }*/
+
+ function lastHistoryItem() {
+ return history.current.get(historyIndex.current);
+ }
+
+ function goBack() {
+ if (historyIndex.current > 0) {
+ historyIndex.current = historyIndex.current - 1;
+ const lastItem = lastHistoryItem();
+ //const filtered = history.current.filter((item) => item.path !== lastItem.path);
+ //history.current = [...filtered, lastItem];
+ openHistoryItem(lastItem);
+ forceUpdate();
+ }
+ }
+
+ function goForward() {
+ if (historyIndex.current < history.current.getArray().length) {
+ historyIndex.current = historyIndex.current + 1;
+ const lastItem = lastHistoryItem();
+ //const filtered = history.current.filter((item) => item.path !== lastItem.path);
+ //history.current = [...filtered, lastItem];
+ openHistoryItem(lastItem);
+ forceUpdate();
+ }
+ }
+
+ function openHistoryItem(item: TS.HistoryItem) {
+ if (item.url) {
+ openLink(item.url, { fullWidth: false });
+ } else {
+ if (item.lid !== currentLocation.uuid) {
+ openLocationById(item.lid);
+ }
+ openEntry(item.path);
+ }
+ }
+
+ const context = {
+ historyIndex: historyIndex.current,
+ goBack,
+ goForward,
+ openHistoryItem,
+ };
+
+ return (
+ <BrowserHistoryContext.Provider value={context}>
+ {children}
+ </BrowserHistoryContext.Provider>
+ );
+};
diff --git a/src/renderer/hooks/OpenedEntryContextProvider.tsx b/src/renderer/hooks/OpenedEntryContextProvider.tsx
index 39f3c2526..c64db7d4a 100644
--- a/src/renderer/hooks/OpenedEntryContextProvider.tsx
+++ b/src/renderer/hooks/OpenedEntryContextProvider.tsx
@@ -93,8 +93,8 @@ type OpenedEntryContextData = {
openPrevFile: (entries: TS.FileSystemEntry[]) => TS.FileSystemEntry;
toggleEntryFullWidth: () => void;
openLink: (url: string, options?) => void;
- goForward: () => void;
- goBack: () => void;
+ //goForward: () => void;
+ //goBack: () => void;
createFileAdvanced: (
targetPath: string,
fileName: string,
@@ -125,8 +125,8 @@ export const OpenedEntryContext = createContext<OpenedEntryContextData>({
openPrevFile: undefined,
toggleEntryFullWidth: () => {},
openLink: () => {},
- goForward: () => {},
- goBack: () => {},
+ //goForward: () => {},
+ //goBack: () => {},
createFile: () => {},
createFileAdvanced: () => {},
getOpenedDirProps: undefined,
@@ -193,21 +193,6 @@ export const OpenedEntryContextProvider = ({
}, 1000);
}
}
- if (AppConfig.isElectron) {
- window.electronIO.ipcRenderer.on('opened-entry', (arg) => {
- if (arg === 'go-back') {
- goBack();
- } else if (arg === 'go-forward') {
- goForward();
- }
- });
-
- return () => {
- if (window.electronIO.ipcRenderer) {
- window.electronIO.ipcRenderer.removeAllListeners('opened-entry');
- }
- };
- }
}, []);
useEffect(() => {
@@ -625,6 +610,12 @@ export const OpenedEntryContextProvider = ({
);
addToEntryContainer(entryForOpening);
+ if (
+ selectedEntries.length !== 1 ||
+ selectedEntries.some((e) => e.path !== fsEntry.path)
+ ) {
+ setSelectedEntries([fsEntry]);
+ }
return Promise.resolve(true);
}
@@ -648,7 +639,7 @@ export const OpenedEntryContextProvider = ({
}
}
- function goForward() {
+ /*function goForward() {
window.history.forward();
window.addEventListener(
'popstate',
@@ -657,9 +648,9 @@ export const OpenedEntryContextProvider = ({
},
{ once: true },
);
- }
+ }*/
- function goBack() {
+ /*function goBack() {
// console.log(
// '>>> current href: ' + decodeURIComponent(window.location.href)
// );
@@ -674,7 +665,7 @@ export const OpenedEntryContextProvider = ({
},
{ once: true },
);
- }
+ }*/
function openLink(url: string, options = { fullWidth: true }) {
try {
@@ -987,8 +978,8 @@ export const OpenedEntryContextProvider = ({
openNextFile,
openPrevFile,
toggleEntryFullWidth,
- goForward,
- goBack,
+ //goForward,
+ //goBack,
openLink,
createFile,
createFileAdvanced,
diff --git a/src/renderer/hooks/useBrowserHistoryContext.tsx b/src/renderer/hooks/useBrowserHistoryContext.tsx
new file mode 100644
index 000000000..2cb736944
--- /dev/null
+++ b/src/renderer/hooks/useBrowserHistoryContext.tsx
@@ -0,0 +1,23 @@
+/**
+ * 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 { useContext } from 'react';
+
+import { BrowserHistoryContext } from '-/hooks/BrowserHistoryContextProvider';
+
+export const useBrowserHistoryContext = () => useContext(BrowserHistoryContext);
diff --git a/src/renderer/services/FixedSizeArray.ts b/src/renderer/services/FixedSizeArray.ts
new file mode 100644
index 000000000..eef20400e
--- /dev/null
+++ b/src/renderer/services/FixedSizeArray.ts
@@ -0,0 +1,39 @@
+export class FixedSizeArray<T> {
+ private size: number;
+ private array: T[];
+
+ constructor(size: number) {
+ this.size = size;
+ this.array = [];
+ }
+
+ push(item: T): void {
+ if (this.array.length >= this.size) {
+ this.array.shift(); // Remove the first item if the array is full
+ }
+ this.array.push(item); // Add the new item to the end
+ }
+
+ slice(start?: number, end?: number): T[] {
+ return this.array.slice(start, end);
+ }
+
+ get(index: number): T | undefined {
+ if (index >= 0 && index < this.array.length) {
+ return this.array[index];
+ }
+ return undefined; // Return undefined if the index is out of bounds
+ }
+
+ getArray(): T[] {
+ return this.array;
+ }
+
+ setArray(newArray: T[]): void {
+ if (newArray.length > this.size) {
+ this.array = newArray.slice(-this.size); // Keep only the last `size` elements
+ } else {
+ this.array = newArray;
+ }
+ }
+}
diff --git a/src/renderer/tagspaces.namespace.d.ts b/src/renderer/tagspaces.namespace.d.ts
index 67543abfc..e44439f9f 100644
--- a/src/renderer/tagspaces.namespace.d.ts
+++ b/src/renderer/tagspaces.namespace.d.ts
@@ -276,7 +276,7 @@ export namespace TS {
saveHistory: (key, historyItem: TS.HistoryItem, limit) => void;
delAllHistory: (key) => void;
delHistory: (key, creationTimeStamp) => void;
- openItem: (item: TS.HistoryItem) => void;
+ //openItem: (item: TS.HistoryItem) => void;
};
type KanBanImportDialogContextData = {
@@ -297,7 +297,7 @@ export namespace TS {
path: string;
url: string;
lid: string;
- creationTimeStamp: number;
+ creationTimeStamp?: number;
searchQuery?: TS.SearchQuery;
}