Skip to content

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

Move methods without state change from location-index reducer (#1939)

Stanimir486249776b57
diff --git a/app/reducers/app.ts b/app/reducers/app.ts
index 119137ae3..d8ff88b69 100644
--- a/app/reducers/app.ts
+++ b/app/reducers/app.ts
@@ -1605,7 +1605,7 @@ export const actions = {
.then(() => {
if (directoryPath === currentDirectoryPath) {
dispatch(actions.loadParentDirectoryContent());
- dispatch(LocationIndexActions.reflectDeleteEntry(directoryPath));
+ GlobalSearch.getInstance().reflectDeleteEntry(directoryPath);
// close opened entries in deleted dir
if (
openedFiles.length > 0 &&
@@ -1675,8 +1675,9 @@ export const actions = {
dispatch(actions.addToEntryContainer(openedFile));
}
}
- dispatch(
- LocationIndexActions.reflectRenameEntry(directoryPath, newDirPath)
+ GlobalSearch.getInstance().reflectRenameEntry(
+ directoryPath,
+ newDirPath
);
} else {
dispatch(actions.reflectRenameEntry(directoryPath, newDirPath));
@@ -2416,7 +2417,7 @@ export const actions = {
}),
reflectDeleteEntry: (path: string) => (dispatch: (action) => void) => {
dispatch(actions.reflectDeleteEntryInt(path));
- dispatch(LocationIndexActions.reflectDeleteEntry(path));
+ GlobalSearch.getInstance().reflectDeleteEntry(path);
},
reflectCreateEntryInt: (newEntry: TS.FileSystemEntry) => ({
type: types.REFLECT_CREATE_ENTRY,
@@ -2451,7 +2452,7 @@ export const actions = {
) => {
dispatch(actions.setSelectedEntries([newEntry]));
dispatch(actions.reflectCreateEntryInt(newEntry));
- dispatch(LocationIndexActions.reflectCreateEntry(newEntry));
+ GlobalSearch.getInstance().reflectCreateEntry(newEntry);
},
reflectRenameEntryInt: (path: string, newPath: string) => ({
type: types.REFLECT_RENAME_ENTRY,
@@ -2479,7 +2480,7 @@ export const actions = {
GlobalSearch.getInstance().setResults(results);
}
dispatch(actions.reflectRenameEntryInt(path, newPath));
- dispatch(LocationIndexActions.reflectRenameEntry(path, newPath));
+ GlobalSearch.getInstance().reflectRenameEntry(path, newPath);
dispatch(actions.setSelectedEntries([]));
},
/**
@@ -2540,7 +2541,7 @@ export const actions = {
dispatch(actions.updateCurrentDirEntry(path, { tags }));
// }
if (updateIndex) {
- dispatch(LocationIndexActions.reflectUpdateSidecarTags(path, tags));
+ GlobalSearch.getInstance().reflectUpdateSidecarTags(path, tags);
}
},
deleteFile: (filePath: string, uuid: string) => (
diff --git a/app/reducers/location-index.ts b/app/reducers/location-index.ts
index 2810c40ff..2a79c8f55 100644
--- a/app/reducers/location-index.ts
+++ b/app/reducers/location-index.ts
@@ -18,13 +18,7 @@
import { loadIndex, hasIndex } from '@tagspaces/tagspaces-platforms/indexer';
import { locationType } from '@tagspaces/tagspaces-common/misc';
-import {
- extractFileExtension,
- extractFileName,
- extractTagsAsObjects,
- getThumbFileLocationForFile,
- generateSharingLink
-} from '@tagspaces/tagspaces-common/paths';
+import { getThumbFileLocationForFile } from '@tagspaces/tagspaces-common/paths';
import AppConfig from '-/AppConfig';
import {
getCurrentLocation,
@@ -32,16 +26,9 @@ import {
getLocationByPath,
getLocations
} from './locations';
-import {
- createDirectoryIndex,
- getRelativeEntryPath
-} from '-/services/utils-io';
+import { createDirectoryIndex } from '-/services/utils-io';
import Search, { defaultTitle } from '../services/search';
-import {
- actions as AppActions,
- getCurrentLocationId,
- getCurrentLocationPath
-} from './app';
+import { actions as AppActions } from './app';
import i18n from '../services/i18n';
import PlatformIO from '../services/platform-facade';
import GlobalSearch from '../services/search-index';
@@ -56,12 +43,12 @@ export const types = {
INDEX_DIRECTORY_CANCEL: 'INDEX_DIRECTORY_CANCEL',
INDEX_DIRECTORY_SUCCESS: 'INDEX_DIRECTORY_SUCCESS',
INDEX_DIRECTORY_FAILURE: 'INDEX_DIRECTORY_FAILURE',
- INDEX_DIRECTORY_SEARCH: 'INDEX_DIRECTORY_SEARCH',
- REFLECT_DELETE_ENTRY: 'INDEX/REFLECT_DELETE_ENTRY',
+ INDEX_DIRECTORY_SEARCH: 'INDEX_DIRECTORY_SEARCH'
+ /*REFLECT_DELETE_ENTRY: 'INDEX/REFLECT_DELETE_ENTRY',
REFLECT_CREATE_ENTRY: 'INDEX/REFLECT_CREATE_ENTRY',
REFLECT_RENAME_ENTRY: 'INDEX/REFLECT_RENAME_ENTRY',
REFLECT_UPDATE_SIDECARTAGS: 'INDEX/REFLECT_UPDATE_SIDECARTAGS',
- REFLECT_UPDATE_SIDECARMETA: 'INDEX/REFLECT_UPDATE_SIDECARMETA'
+ REFLECT_UPDATE_SIDECARMETA: 'INDEX/REFLECT_UPDATE_SIDECARMETA'*/
};
export const initialState = {
@@ -108,7 +95,7 @@ export default (state: any = initialState, action: any) => {
isIndexing: false
};
}
- case types.REFLECT_DELETE_ENTRY: {
+ /*case types.REFLECT_DELETE_ENTRY: {
const index = GlobalSearch.getInstance().getIndex();
if (!index || index.length < 1) {
return state;
@@ -120,8 +107,8 @@ export default (state: any = initialState, action: any) => {
}
}
return state;
- }
- case types.REFLECT_CREATE_ENTRY: {
+ }*/
+ /*case types.REFLECT_CREATE_ENTRY: {
const index = GlobalSearch.getInstance().getIndex();
if (!index || index.length < 1) {
return state;
@@ -137,9 +124,9 @@ export default (state: any = initialState, action: any) => {
}
GlobalSearch.getInstance().setIndex([...index, action.newEntry]);
return state;
- }
+ }*/
// TODO move it from reducer to GlobalSearch singleton
- case types.REFLECT_RENAME_ENTRY: {
+ /* case types.REFLECT_RENAME_ENTRY: {
const index = GlobalSearch.getInstance().getIndex();
if (!index || index.length < 1) {
return state;
@@ -166,8 +153,8 @@ export default (state: any = initialState, action: any) => {
}
}
return state;
- }
- case types.REFLECT_UPDATE_SIDECARTAGS: {
+ }*/
+ /*case types.REFLECT_UPDATE_SIDECARTAGS: {
const index = GlobalSearch.getInstance().getIndex();
for (let i = 0; i < index.length; i += 1) {
if (index[i].path === action.path) {
@@ -178,8 +165,8 @@ export default (state: any = initialState, action: any) => {
}
}
return state;
- }
- case types.REFLECT_UPDATE_SIDECARMETA: {
+ }*/
+ /*case types.REFLECT_UPDATE_SIDECARMETA: {
const index = GlobalSearch.getInstance().getIndex();
for (let i = 0; i < index.length; i += 1) {
if (index[i].path === action.path) {
@@ -190,7 +177,7 @@ export default (state: any = initialState, action: any) => {
}
}
return state;
- }
+ }*/
default: {
return state;
}
@@ -684,30 +671,30 @@ export const actions = {
indexDirectoryFailure: (error: string) => ({
type: types.INDEX_DIRECTORY_FAILURE,
error
- }),
- reflectDeleteEntry: (path: string) => ({
+ })
+ /*reflectDeleteEntry: (path: string) => ({
type: types.REFLECT_DELETE_ENTRY,
path
- }),
- reflectCreateEntry: (newEntry: Object) => ({
+ }),*/
+ /*reflectCreateEntry: (newEntry: Object) => ({
type: types.REFLECT_CREATE_ENTRY,
newEntry
- }),
- reflectRenameEntry: (path: string, newPath: string) => ({
+ }),*/
+ /*reflectRenameEntry: (path: string, newPath: string) => ({
type: types.REFLECT_RENAME_ENTRY,
path,
newPath
- }),
- reflectUpdateSidecarTags: (path: string, tags: Array<TS.Tag>) => ({
+ }),*/
+ /*reflectUpdateSidecarTags: (path: string, tags: Array<TS.Tag>) => ({
type: types.REFLECT_UPDATE_SIDECARTAGS,
path,
tags
- }),
- reflectUpdateSidecarMeta: (path: string, entryMeta: Object) => ({
+ }),*/
+ /*reflectUpdateSidecarMeta: (path: string, entryMeta: Object) => ({
type: types.REFLECT_UPDATE_SIDECARMETA,
path,
entryMeta
- })
+ })*/
};
// Selectors
diff --git a/app/services/search-index.ts b/app/services/search-index.ts
index ada7b77a9..880a52a27 100644
--- a/app/services/search-index.ts
+++ b/app/services/search-index.ts
@@ -21,6 +21,14 @@
index: [],
results: []
};*/
+import {
+ extractFileName,
+ extractFileExtension,
+ extractTagsAsObjects
+} from '@tagspaces/tagspaces-common/paths';
+import { TS } from '-/tagspaces.namespace';
+import PlatformIO from '-/services/platform-facade';
+import AppConfig from '-/AppConfig';
const GlobalSearch = (function() {
let instance;
@@ -42,7 +50,80 @@ const GlobalSearch = (function() {
setResults: function(r) {
results = r;
},
- getResults: () => results
+ getResults: () => results,
+ reflectDeleteEntry: (path: string) => {
+ if (!index || index.length < 1) {
+ return;
+ }
+ for (let i = 0; i < index.length; i += 1) {
+ if (index[i].path === path) {
+ index = index.splice(i, 1);
+ i -= 1;
+ }
+ }
+ },
+ reflectCreateEntry: (newEntry: TS.FileSystemEntry) => {
+ if (!index || index.length < 1) {
+ return;
+ }
+ let entryFound = index.some(entry => entry.path === newEntry.path);
+ if (!entryFound) {
+ index = [...index, newEntry];
+ }
+ // else todo update index entry ?
+ },
+ reflectRenameEntry: (path: string, newPath: string) => {
+ if (!index || index.length < 1) {
+ return;
+ }
+ for (let i = 0; i < index.length; i += 1) {
+ if (index[i].path === path) {
+ index[i].path = newPath;
+ index[i].name = extractFileName(
+ newPath,
+ PlatformIO.getDirSeparator()
+ );
+ index[i].extension = extractFileExtension(
+ newPath,
+ PlatformIO.getDirSeparator()
+ );
+ index[i].tags = [
+ ...index[i].tags.filter(tag => tag.type === 'sidecar'), // add only sidecar tags
+ ...extractTagsAsObjects(
+ newPath,
+ AppConfig.tagDelimiter,
+ PlatformIO.getDirSeparator()
+ )
+ ];
+ }
+ }
+ },
+ reflectUpdateSidecarTags: (path: string, tags: Array<TS.Tag>) => {
+ if (!index || index.length < 1) {
+ return;
+ }
+ for (let i = 0; i < index.length; i += 1) {
+ if (index[i].path === path) {
+ index[i].tags = [
+ ...index[i].tags.filter(tag => tag.type === 'plain'),
+ ...tags
+ ];
+ }
+ }
+ },
+ reflectUpdateSidecarMeta: (path: string, entryMeta: Object) => {
+ if (!index || index.length < 1) {
+ return;
+ }
+ for (let i = 0; i < index.length; i += 1) {
+ if (index[i].path === path) {
+ index[i] = {
+ ...index[i],
+ ...entryMeta
+ };
+ }
+ }
+ }
};
}