Skip to content

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

tagging actions refactoring and optimize (#2186)

Stanimir81b3665bb950
diff --git a/src/renderer/components/EntryContainer.tsx b/src/renderer/components/EntryContainer.tsx
index 9dcd5c600..4675d6777 100644
--- a/src/renderer/components/EntryContainer.tsx
+++ b/src/renderer/components/EntryContainer.tsx
@@ -336,14 +336,13 @@ function EntryContainer() {
cLocation?.getDirSeparator(),
);
if (AppConfig.isWeb) {
+ const webDir = extractContainingDirectoryPath(
+ // eslint-disable-next-line no-restricted-globals
+ location.href,
+ cLocation?.getDirSeparator(),
+ );
fileDirectory =
- extractContainingDirectoryPath(
- // eslint-disable-next-line no-restricted-globals
- location.href,
- cLocation?.getDirSeparator(),
- ) +
- '/' +
- fileDirectory;
+ (webDir && webDir !== '/' ? webDir + '/' : '') + fileDirectory;
}
if (
fileViewer &&
diff --git a/src/renderer/components/EntryProperties.tsx b/src/renderer/components/EntryProperties.tsx
index 6ca585e50..8068f3029 100644
--- a/src/renderer/components/EntryProperties.tsx
+++ b/src/renderer/components/EntryProperties.tsx
@@ -336,11 +336,13 @@ function EntryProperties(props: Props) {
const renameEntry = () => {
if (editName !== undefined) {
+ const dirSeparator = location?.getDirSeparator();
const path = extractContainingDirectoryPath(
openedEntry.path,
- location?.getDirSeparator(),
+ dirSeparator,
);
- const nextPath = path + location?.getDirSeparator() + editName;
+ const nextPath =
+ (path && path !== dirSeparator ? path + dirSeparator : '') + editName;
if (openedEntry.isFile) {
renameFile(openedEntry.path, nextPath, openedEntry.locationID).catch(
diff --git a/src/renderer/components/dialogs/FilePreviewDialog.tsx b/src/renderer/components/dialogs/FilePreviewDialog.tsx
index 5758f9465..b92c4dfcb 100644
--- a/src/renderer/components/dialogs/FilePreviewDialog.tsx
+++ b/src/renderer/components/dialogs/FilePreviewDialog.tsx
@@ -119,14 +119,13 @@ function FilePreviewDialog(props: Props) {
openLocation?.getDirSeparator(),
);
if (AppConfig.isWeb) {
+ const webDir = extractContainingDirectoryPath(
+ // eslint-disable-next-line no-restricted-globals
+ location.href,
+ openLocation?.getDirSeparator(),
+ );
fileDirectory =
- extractContainingDirectoryPath(
- // eslint-disable-next-line no-restricted-globals
- location.href,
- openLocation?.getDirSeparator(),
- ) +
- '/' +
- fileDirectory;
+ (webDir && webDir !== '/' ? webDir + '/' : '') + fileDirectory;
}
if (
fileViewer &&
diff --git a/src/renderer/components/dialogs/RenameEntryDialog.tsx b/src/renderer/components/dialogs/RenameEntryDialog.tsx
index c28735032..28adfb9d9 100644
--- a/src/renderer/components/dialogs/RenameEntryDialog.tsx
+++ b/src/renderer/components/dialogs/RenameEntryDialog.tsx
@@ -138,12 +138,15 @@ function RenameEntryDialog(props: Props) {
if (!disableConfirmButton.current) {
onClose();
if (isFile) {
+ const dirSeparator = currentLocation?.getDirSeparator();
const fileDirectory = extractContainingDirectoryPath(
lastSelectedEntry.path,
- currentLocation?.getDirSeparator(),
+ dirSeparator,
);
const newFilePath =
- fileDirectory + currentLocation.getDirSeparator() + name.current;
+ (fileDirectory && fileDirectory !== dirSeparator
+ ? fileDirectory + dirSeparator
+ : '') + name.current;
return renameFile(originPath, newFilePath, currentLocation.uuid);
} else {
return renameDirectory(originPath, name.current, currentLocation.uuid);
diff --git a/src/renderer/components/dialogs/ResolveConflictDialog.tsx b/src/renderer/components/dialogs/ResolveConflictDialog.tsx
index 2540395b6..a08632f90 100644
--- a/src/renderer/components/dialogs/ResolveConflictDialog.tsx
+++ b/src/renderer/components/dialogs/ResolveConflictDialog.tsx
@@ -114,15 +114,15 @@ function ResolveConflictDialog(props: Props) {
data-tid="saveTID"
title={t('core:save')}
onClick={() => {
- props
- .saveAs(
- extractContainingDirectoryPath(openedEntry.path) +
- '/' +
- copyFileName.current,
- )
- .then(() => {
- onClose();
- });
+ const dirPath = extractContainingDirectoryPath(
+ openedEntry.path,
+ );
+ const newFilePath =
+ (dirPath && dirPath !== '/' ? dirPath + '/' : '') +
+ copyFileName.current;
+ props.saveAs(newFilePath).then(() => {
+ onClose();
+ });
}}
color="primary"
>
diff --git a/src/renderer/components/dialogs/hooks/ResolveConflictContextProvider.tsx b/src/renderer/components/dialogs/hooks/ResolveConflictContextProvider.tsx
index 380b54c11..53e4760f4 100644
--- a/src/renderer/components/dialogs/hooks/ResolveConflictContextProvider.tsx
+++ b/src/renderer/components/dialogs/hooks/ResolveConflictContextProvider.tsx
@@ -20,6 +20,7 @@ import React, { createContext, useMemo, useReducer, useRef } from 'react';
import {
extractContainingDirectoryPath,
getBackupFileLocation,
+ cleanTrailingDirSeparator,
} from '@tagspaces/tagspaces-common/paths';
import LoadingLazy from '-/components/LoadingLazy';
import { TS } from '-/tagspaces.namespace';
@@ -122,7 +123,10 @@ export const ResolveConflictContextProvider = ({
const openedEntryDir = extractContainingDirectoryPath(
entryProp.path,
);
- if (currentDirectoryPath === openedEntryDir) {
+ if (
+ cleanTrailingDirSeparator(currentDirectoryPath) ===
+ cleanTrailingDirSeparator(openedEntryDir)
+ ) {
return openDirectory(openedEntryDir).then(() => true);
}
return true;
diff --git a/src/renderer/hooks/CurrentLocationContextProvider.tsx b/src/renderer/hooks/CurrentLocationContextProvider.tsx
index cea2766a7..fb5c8da68 100644
--- a/src/renderer/hooks/CurrentLocationContextProvider.tsx
+++ b/src/renderer/hooks/CurrentLocationContextProvider.tsx
@@ -491,7 +491,7 @@ export const CurrentLocationContextProvider = ({
return locationPersistTagsInSidecarFile;
}
return settingsPersistTagsInSidecarFile;
- }, [currentLocation.current, settingsPersistTagsInSidecarFile]);
+ }, [location, settingsPersistTagsInSidecarFile]);
function changeLocation(
location: CommonLocation,
diff --git a/src/renderer/hooks/OpenedEntryContextProvider.tsx b/src/renderer/hooks/OpenedEntryContextProvider.tsx
index 0052ad6c2..a4118ba06 100644
--- a/src/renderer/hooks/OpenedEntryContextProvider.tsx
+++ b/src/renderer/hooks/OpenedEntryContextProvider.tsx
@@ -600,9 +600,14 @@ export const OpenedEntryContextProvider = ({
entryForOpening.url = fsEntry.url;
}
//set meta and generate new meta id if not exist
- const meta = await getMetadata(fsEntry.path, fsEntry.uuid, loc);
+ let meta;
+ if (fsEntry.meta && fsEntry.meta.id) {
+ meta = { ...fsEntry.meta };
+ } else {
+ meta = await getMetadata(fsEntry.path, fsEntry.uuid, loc);
+ }
entryForOpening.uuid = meta.id;
- entryForOpening.meta = { ...fsEntry.meta, ...meta };
+ entryForOpening.meta = meta;
if (
fsEntry.isNewFile &&
diff --git a/src/renderer/hooks/TaggingActionsContextProvider.tsx b/src/renderer/hooks/TaggingActionsContextProvider.tsx
index 33993e19a..71c1da23f 100644
--- a/src/renderer/hooks/TaggingActionsContextProvider.tsx
+++ b/src/renderer/hooks/TaggingActionsContextProvider.tsx
@@ -44,6 +44,7 @@ import {
extractContainingDirectoryPath,
extractFileName,
extractTags,
+ extractTagsAsObjects,
generateFileName,
} from '@tagspaces/tagspaces-common/paths';
import { getUuid } from '@tagspaces/tagspaces-common/utils-io';
@@ -66,7 +67,7 @@ type TaggingActionsContextData = {
path: string,
tags: Array<TS.Tag>,
reflect?: boolean,
- ) => Promise<string>;
+ ) => Promise<TS.FileSystemEntry>;
editTagForEntry: (path: string, tag: TS.Tag, newTagTitle?: string) => void;
removeTags: (paths: Array<string>, tags?: Array<TS.Tag>) => Promise<boolean>;
removeTagsFromEntry: (path: string, tags?: Array<TS.Tag>) => Promise<string>;
@@ -185,29 +186,26 @@ export const TaggingActionsContextProvider = ({
function addTagsToFilePath(path: string, tags: string[]) {
if (tags && tags.length > 0) {
+ const dirSeparator = currentLocation
+ ? currentLocation.getDirSeparator()
+ : AppConfig.dirSeparator;
const extractedTags: string[] = extractTags(
path,
tagDelimiter,
- currentLocation?.getDirSeparator(),
+ dirSeparator,
);
const uniqueTags = tags.filter(
(tag) => !extractedTags.some((tagName) => tagName === tag),
);
- const fileName = extractFileName(
- path,
- currentLocation?.getDirSeparator(),
- );
+ const fileName = extractFileName(path, dirSeparator);
const containingDirectoryPath = extractContainingDirectoryPath(
path,
- currentLocation?.getDirSeparator(),
+ dirSeparator,
);
return (
- (containingDirectoryPath
- ? containingDirectoryPath +
- (currentLocation
- ? currentLocation.getDirSeparator()
- : AppConfig.dirSeparator)
+ (containingDirectoryPath && containingDirectoryPath !== dirSeparator
+ ? containingDirectoryPath + dirSeparator
: '') +
generateFileName(
fileName,
@@ -227,34 +225,26 @@ export const TaggingActionsContextProvider = ({
}): Promise<boolean> {
if (files) {
const promises = Object.entries(files).map(([filePath, tags]) =>
- addTagsToEntry(filePath, tags, false).then((newPath) => ({
+ addTagsToEntry(filePath, tags, false).then((newEntry) => ({
filePath,
- newPath,
+ newEntry,
})),
);
return Promise.all(promises).then((editedPaths) => {
- const promiseReflect: Promise<TS.EditAction>[] = [];
+ const reflects: TS.EditAction[] = [];
for (let i = 0; i < editedPaths.length; i++) {
- const { filePath, newPath } = editedPaths[i];
- if (newPath !== undefined) {
- promiseReflect.push(
- getAllPropertiesPromise(newPath).then(
- (fsEntry: TS.FileSystemEntry) => {
- const currentAction: TS.EditAction = {
- action: 'update',
- entry: fsEntry,
- oldEntryPath: filePath,
- };
- return currentAction;
- },
- ),
- );
+ const { filePath, newEntry } = editedPaths[i];
+ if (newEntry !== undefined) {
+ const currentAction: TS.EditAction = {
+ action: 'update',
+ entry: newEntry,
+ oldEntryPath: filePath,
+ };
+ reflects.push(currentAction);
}
}
- return Promise.all(promiseReflect).then((actionsArray) => {
- setReflectActions(...actionsArray);
- return true;
- });
+ setReflectActions(...reflects);
+ return true;
});
}
return Promise.resolve(true);
@@ -409,13 +399,13 @@ export const TaggingActionsContextProvider = ({
* @param entry: TS.FileSystemEntry
* @param tags
* @param reflect
- * return newPath
+ * return newFsEntry updated
*/
async function addTagsToFsEntry(
entry: TS.FileSystemEntry,
tags: Array<TS.Tag>,
reflect: boolean = true,
- ): Promise<string> {
+ ): Promise<TS.FileSystemEntry> {
if (entry) {
let fsEntryMeta;
try {
@@ -449,7 +439,7 @@ export const TaggingActionsContextProvider = ({
if (reflect) {
reflectUpdateMeta({ ...entry, meta });
}
- return entry.path;
+ return { ...entry, meta };
})
.catch((err) => {
console.log(
@@ -460,7 +450,7 @@ export const TaggingActionsContextProvider = ({
'error',
true,
);
- return entry.path;
+ return entry;
});
}
} else {
@@ -470,7 +460,7 @@ export const TaggingActionsContextProvider = ({
if (reflect) {
reflectUpdateMeta({ ...entry, meta });
}
- return entry.path;
+ return { ...entry, meta };
})
.catch((error) => {
console.log(
@@ -481,7 +471,7 @@ export const TaggingActionsContextProvider = ({
'error',
true,
);
- return entry.path;
+ return entry;
});
}
} else if (fsEntryMeta) {
@@ -508,7 +498,21 @@ export const TaggingActionsContextProvider = ({
entry.locationID,
reflect,
).then((success) => {
- return success ? newFilePath : undefined;
+ return success
+ ? {
+ ...entry,
+ tags: extractTagsAsObjects(
+ newFilePath,
+ tagDelimiter,
+ currentLocation?.getDirSeparator(),
+ ),
+ name: extractFileName(
+ newFilePath,
+ currentLocation?.getDirSeparator(),
+ ),
+ path: newFilePath,
+ }
+ : undefined;
});
}
}
@@ -525,11 +529,25 @@ export const TaggingActionsContextProvider = ({
entry.locationID,
reflect,
).then((success) => {
- return success ? newFilePath : undefined;
+ return success
+ ? {
+ ...entry,
+ tags: extractTagsAsObjects(
+ newFilePath,
+ tagDelimiter,
+ currentLocation?.getDirSeparator(),
+ ),
+ name: extractFileName(
+ newFilePath,
+ currentLocation?.getDirSeparator(),
+ ),
+ path: newFilePath,
+ }
+ : undefined;
});
}
}
- return Promise.resolve(entry.path);
+ return Promise.resolve(entry);
function getNonExistingTags(
newTagsArray: Array<TS.Tag>,
@@ -553,19 +571,19 @@ export const TaggingActionsContextProvider = ({
return newTags;
}
}
- return Promise.resolve('');
+ return Promise.resolve(undefined);
}
/**
* @param path
* @param tags
* @param reflect
- * return newPath
+ * return newFsEntry updated
*/
function addTagsToEntry(
path: string,
tags: Array<TS.Tag>,
reflect: boolean = true,
- ): Promise<string> {
+ ): Promise<TS.FileSystemEntry> {
return currentLocation
.getPropertiesPromise(path)
.then((entry) => addTagsToFsEntry(entry, tags, reflect));
@@ -583,6 +601,9 @@ export const TaggingActionsContextProvider = ({
tag: TS.Tag,
newTagTitle?: string,
) {
+ const dirSeparator = currentLocation
+ ? currentLocation.getDirSeparator()
+ : AppConfig.dirSeparator;
if (newTagTitle === undefined) {
// eslint-disable-next-line no-param-reassign
newTagTitle = tag.title;
@@ -605,19 +626,16 @@ export const TaggingActionsContextProvider = ({
const extractedTags: string[] = extractTags(
path,
tagDelimiter,
- currentLocation?.getDirSeparator(),
+ dirSeparator,
);
// TODO: Handle adding already added tags
if (extractedTags.includes(tag.title)) {
//tag.type === 'plain') {
- const fileName = extractFileName(
- path,
- currentLocation?.getDirSeparator(),
- );
+ const fileName = extractFileName(path, dirSeparator);
const containingDirectoryPath = extractContainingDirectoryPath(
path,
- currentLocation?.getDirSeparator(),
+ dirSeparator,
);
let tagFoundPosition = -1;
@@ -641,20 +659,16 @@ export const TaggingActionsContextProvider = ({
fileName,
extractedTags,
tagDelimiter,
- currentLocation?.getDirSeparator(),
+ dirSeparator,
prefixTagContainer,
filenameTagPlacedAtEnd,
);
if (newFileName !== fileName) {
- await renameFile(
- path,
- containingDirectoryPath +
- (currentLocation
- ? currentLocation.getDirSeparator()
- : AppConfig.dirSeparator) +
- newFileName,
- currentLocation.uuid,
- );
+ const newFilePath =
+ (containingDirectoryPath && containingDirectoryPath !== dirSeparator
+ ? containingDirectoryPath + dirSeparator
+ : '') + newFileName;
+ await renameFile(path, newFilePath, currentLocation.uuid);
}
} else {
//if (tag.type === 'sidecar') {
@@ -898,19 +912,15 @@ export const TaggingActionsContextProvider = ({
return Promise.resolve(path);
}
return new Promise(async (resolve, reject) => {
- let extractedTags = extractTags(
- path,
- tagDelimiter,
- currentLocation?.getDirSeparator(),
- );
+ const dirSeparator = currentLocation
+ ? currentLocation.getDirSeparator()
+ : AppConfig.dirSeparator;
+ let extractedTags = extractTags(path, tagDelimiter, dirSeparator);
if (extractedTags.length > 0) {
- const fileName = extractFileName(
- path,
- currentLocation?.getDirSeparator(),
- );
+ const fileName = extractFileName(path, dirSeparator);
const containingDirectoryPath = extractContainingDirectoryPath(
path,
- currentLocation?.getDirSeparator(),
+ dirSeparator,
);
if (tagTitlesForRemoving) {
for (let i = 0; i < tagTitlesForRemoving.length; i += 1) {
@@ -927,14 +937,14 @@ export const TaggingActionsContextProvider = ({
extractedTags = [];
}
const newFilePath =
- (containingDirectoryPath
- ? containingDirectoryPath + currentLocation.getDirSeparator()
+ (containingDirectoryPath && containingDirectoryPath !== dirSeparator
+ ? containingDirectoryPath + dirSeparator
: '') +
generateFileName(
fileName,
extractedTags,
tagDelimiter,
- currentLocation?.getDirSeparator(),
+ dirSeparator,
prefixTagContainer,
filenameTagPlacedAtEnd,
);
diff --git a/src/renderer/services/utils-io.ts b/src/renderer/services/utils-io.ts
index b4dbef7b2..0bb66f876 100644
--- a/src/renderer/services/utils-io.ts
+++ b/src/renderer/services/utils-io.ts
@@ -21,6 +21,7 @@ import { prepareTagForExport } from '@tagspaces/tagspaces-common/misc';
import {
baseName,
extractFileName,
+ extractTagsAsObjects,
cleanTrailingDirSeparator,
cleanFrontDirSeparator,
} from '@tagspaces/tagspaces-common/paths';
@@ -38,14 +39,23 @@ export function getAllTags(entry: TS.FileSystemEntry): Array<TS.Tag> {
if (entry.meta && entry.meta.tags && entry.meta.tags.length > 0) {
tags.push(...entry.meta.tags);
}
+ let fileNameTags;
if (entry.tags && entry.tags.length > 0) {
+ fileNameTags = entry.tags;
+ } else if (
+ entry.path.indexOf(AppConfig.beginTagContainer) !== -1 &&
+ entry.path.indexOf(AppConfig.endTagContainer) !== -1
+ ) {
+ fileNameTags = extractTagsAsObjects(entry.name, AppConfig.tagDelimiter);
+ }
+ if (fileNameTags) {
if (tags.length > 0) {
- const filteredTags = entry.tags.filter(
+ const filteredTags = fileNameTags.filter(
(tag) => !tags.some((t) => t.title === tag.title),
);
tags.push(...filteredTags);
} else {
- tags.push(...entry.tags);
+ tags.push(...fileNameTags);
}
}
return tags;