Skip to content

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

enabling creating the index for every location

Ilian Sapundshievc2cf06531bfc
diff --git a/app/components/LocationManager.tsx b/app/components/LocationManager.tsx
index caae07a67..3933cf556 100644
--- a/app/components/LocationManager.tsx
+++ b/app/components/LocationManager.tsx
@@ -94,7 +94,11 @@ interface Props {
openFileNatively: (path: string) => void;
openDirectory: (path: string) => void;
showInFileManager: (path: string) => void;
- createDirectoryIndex: (path: string) => void;
+ createDirectoryIndex: (
+ path: string,
+ fullTextIndex: boolean,
+ isCurrentLocation: boolean
+ ) => void;
addLocation: (location: Location, openAfterCreate?: boolean) => void;
editLocation: () => void;
moveLocationUp: (locationId: string) => void;
@@ -118,7 +122,7 @@ interface State {
locationContextMenuOpened?: boolean;
directoryContextMenuAnchorEl?: Object | null;
directoryContextMenuOpened?: boolean;
- selectedLocation?: Location | null;
+ selectedLocation: Location | null;
selectedDirectoryPath?: string | null;
locationRootPath?: string | null;
isCreateLocationDialogOpened?: boolean;
@@ -363,9 +367,17 @@ class LocationManager extends React.Component<Props, State> {
indexLocation = () => {
this.handleRequestCloseContextMenus();
- if (this.state.selectedLocation && this.state.selectedLocation.uuid) {
- this.props.createDirectoryIndex(this.state.selectedLocation.paths[0]);
- }
+ const { selectedLocation } = this.state;
+ const { currentLocationId, createDirectoryIndex } = this.props;
+ const isCurrentLocation =
+ selectedLocation &&
+ selectedLocation.uuid &&
+ selectedLocation.uuid === currentLocationId;
+ createDirectoryIndex(
+ selectedLocation.paths[0],
+ selectedLocation.fullTextIndex,
+ isCurrentLocation
+ );
};
moveLocationUp = () => {
@@ -817,16 +829,12 @@ class LocationManager extends React.Component<Props, State> {
</ListItemIcon>
<ListItemText primary={i18n.t('core:editLocationTitle')} />
</MenuItem>
- {this.state.selectedLocation &&
- this.props.currentLocationId ===
- this.state.selectedLocation.uuid && (
- <MenuItem data-tid="indexLocation" onClick={this.indexLocation}>
- <ListItemIcon>
- <RefreshIcon />
- </ListItemIcon>
- <ListItemText primary={i18n.t('core:indexLocation')} />
- </MenuItem>
- )}
+ <MenuItem data-tid="indexLocation" onClick={this.indexLocation}>
+ <ListItemIcon>
+ <RefreshIcon />
+ </ListItemIcon>
+ <ListItemText primary={i18n.t('core:indexLocation')} />
+ </MenuItem>
<MenuItem data-tid="moveLocationUp" onClick={this.moveLocationUp}>
<ListItemIcon>
<ArrowUpwardIcon />
diff --git a/app/reducers/location-index.ts b/app/reducers/location-index.ts
index 63801fda2..a2c495aba 100644
--- a/app/reducers/location-index.ts
+++ b/app/reducers/location-index.ts
@@ -31,7 +31,7 @@ import AppConfig from '../config';
import i18n from '../services/i18n';
// import PlatformIO from '../services/platform-io';
import { Tag } from './taglibrary';
-import SearchIndex from '../services/search-index';
+import GlobalSearch from '../services/search-index';
export const types = {
INDEX_DIRECTORY: 'INDEX_DIRECTORY',
@@ -61,8 +61,8 @@ export default (state: any = initialState, action: any) => {
};
}
case types.INDEX_DIRECTORY_CLEAR: {
- // SearchIndex.length = 0;
- SearchIndex.splice(0, SearchIndex.length);
+ // GlobalSearch.index.length = 0;
+ GlobalSearch.index.splice(0, GlobalSearch.index.length);
return {
...state,
isIndexing: false
@@ -79,7 +79,7 @@ export default (state: any = initialState, action: any) => {
};
}
case types.INDEX_DIRECTORY_FAILURE: {
- // SearchIndex.length = 0;
+ // GlobalSearch.index.length = 0;
return {
...state,
lastError: action.error,
@@ -87,9 +87,9 @@ export default (state: any = initialState, action: any) => {
};
}
case types.REFLECT_DELETE_ENTRY: {
- for (let i = 0; i < SearchIndex.length; i += 1) {
- if (SearchIndex[i].path === action.path) {
- SearchIndex.splice(i, 1);
+ for (let i = 0; i < GlobalSearch.index.length; i += 1) {
+ if (GlobalSearch.index[i].path === action.path) {
+ GlobalSearch.index.splice(i, 1);
i -= 1;
}
}
@@ -97,26 +97,28 @@ export default (state: any = initialState, action: any) => {
}
case types.REFLECT_CREATE_ENTRY: {
let entryFound = false;
- for (let i = 0; i < SearchIndex.length; i += 1) {
- if (SearchIndex[i].path === action.path) {
+ for (let i = 0; i < GlobalSearch.index.length; i += 1) {
+ if (GlobalSearch.index[i].path === action.path) {
entryFound = true;
}
}
if (entryFound) {
return state;
}
- SearchIndex.push(action.newEntry);
+ GlobalSearch.index.push(action.newEntry);
return state;
}
case types.REFLECT_RENAME_ENTRY: {
- for (let i = 0; i < SearchIndex.length; i += 1) {
- if (SearchIndex[i].path === action.path) {
- SearchIndex[i].path = action.newPath;
+ for (let i = 0; i < GlobalSearch.index.length; i += 1) {
+ if (GlobalSearch.index[i].path === action.path) {
+ GlobalSearch.index[i].path = action.newPath;
// thumbPath: getThumbFileLocationForFile(action.newPath), // disabled due performance concerns
- SearchIndex[i].name = extractFileName(action.newPath);
- SearchIndex[i].extension = extractFileExtension(action.newPath);
- SearchIndex[i].tags = [
- ...SearchIndex[i].tags.filter(tag => tag.type === 'sidecar'), // add only sidecar tags
+ GlobalSearch.index[i].name = extractFileName(action.newPath);
+ GlobalSearch.index[i].extension = extractFileExtension(
+ action.newPath
+ );
+ GlobalSearch.index[i].tags = [
+ ...GlobalSearch.index[i].tags.filter(tag => tag.type === 'sidecar'), // add only sidecar tags
...extractTagsAsObjects(action.newPath) // , getTagDelimiter(state))
];
}
@@ -147,10 +149,10 @@ export default (state: any = initialState, action: any) => {
return state; */
}
case types.REFLECT_UPDATE_SIDECARTAGS: {
- for (let i = 0; i < SearchIndex.length; i += 1) {
- if (SearchIndex[i].path === action.path) {
- SearchIndex[i].tags = [
- ...SearchIndex[i].tags.filter(tag => tag.type === 'plain'),
+ for (let i = 0; i < GlobalSearch.index.length; i += 1) {
+ if (GlobalSearch.index[i].path === action.path) {
+ GlobalSearch.index[i].tags = [
+ ...GlobalSearch.index[i].tags.filter(tag => tag.type === 'plain'),
...action.tags
];
}
@@ -177,10 +179,10 @@ export default (state: any = initialState, action: any) => {
return state; */
}
case types.REFLECT_UPDATE_SIDECARMETA: {
- for (let i = 0; i < SearchIndex.length; i += 1) {
- if (SearchIndex[i].path === action.path) {
- SearchIndex[i] = {
- ...SearchIndex[i],
+ for (let i = 0; i < GlobalSearch.index.length; i += 1) {
+ if (GlobalSearch.index[i].path === action.path) {
+ GlobalSearch.index[i] = {
+ ...GlobalSearch.index[i],
...action.entryMeta
};
}
@@ -212,10 +214,11 @@ export default (state: any = initialState, action: any) => {
export const actions = {
startDirectoryIndexing: () => ({ type: types.INDEX_DIRECTORY_START }),
cancelDirectoryIndexing: () => ({ type: types.INDEX_DIRECTORY_CANCEL }),
- createDirectoryIndex: (directoryPath: string, extractText: boolean) => (
- dispatch: (actions: Object) => void,
- getState: () => any
- ) => {
+ createDirectoryIndex: (
+ directoryPath: string,
+ extractText: boolean,
+ isCurrentLocation: boolean = true
+ ) => (dispatch: (actions: Object) => void, getState: () => any) => {
const state = getState();
const currentLocation: Location = getLocation(
state,
@@ -223,13 +226,16 @@ export const actions = {
);
dispatch(actions.startDirectoryIndexing());
createDirectoryIndex(directoryPath, extractText)
- .then(() => {
+ .then(directoryIndex => {
+ if (isCurrentLocation) {
+ GlobalSearch.index = directoryIndex;
+ }
dispatch(actions.indexDirectorySuccess());
if (Pro && Pro.Indexer) {
// && (currentLocation.persistIndex || PlatformIO.haveObjectStoreSupport())) { // always persist on s3 stores
Pro.Indexer.persistIndex(
directoryPath,
- SearchIndex,
+ GlobalSearch.index,
currentLocation.type === locationType.TYPE_CLOUD
? '/'
: AppConfig.dirSeparator
@@ -344,5 +350,5 @@ export const actions = {
};
// Selectors
-export const getIndexedEntriesCount = (state: any) => SearchIndex.length;
+export const getIndexedEntriesCount = (state: any) => GlobalSearch.index.length;
export const isIndexing = (state: any) => state.locationIndex.isIndexing;
diff --git a/app/services/search-index.ts b/app/services/search-index.ts
index 4e714ab84..9149b8da8 100644
--- a/app/services/search-index.ts
+++ b/app/services/search-index.ts
@@ -16,5 +16,7 @@
*
*/
-const searchIndex = [];
-export default searchIndex;
+const GlobalSearch = {
+ index: []
+};
+export default GlobalSearch;
diff --git a/app/services/search.ts b/app/services/search.ts
index 0349ac951..0387db80a 100644
--- a/app/services/search.ts
+++ b/app/services/search.ts
@@ -20,7 +20,7 @@ import Fuse from 'fuse.js';
import jmespath from 'jmespath';
import { Tag } from '../reducers/taglibrary';
import { Pro } from '../pro';
-import SearchIndex from './search-index';
+import GlobalSearch from './search-index';
// export type FileTypeGroups = 'images' | 'notes' | 'documents' | 'audio' | 'video' | 'archives';
@@ -250,7 +250,7 @@ export default class Search {
new Promise(resolve => {
console.time('searchtime');
const jmespathQuery = constructjmespathQuery(searchQuery);
- let results = prepareIndex(SearchIndex);
+ let results = prepareIndex(GlobalSearch.index);
let searched = false;
// Limiting the search to current folder only (with sub-folders)
diff --git a/app/services/utils-io.ts b/app/services/utils-io.ts
index e67c59e37..857a8203d 100644
--- a/app/services/utils-io.ts
+++ b/app/services/utils-io.ts
@@ -31,7 +31,6 @@ import {
// import { formatDateTime4Tag } from '../utils/misc';
import versionMeta from '../version.json';
import { Tag } from '../reducers/taglibrary';
-import SearchIndex from '../services/search-index';
export interface FileSystemEntry {
uuid?: string;
@@ -105,15 +104,16 @@ export function enhanceEntry(entry: any): FileSystemEntry {
export function createDirectoryIndex(
directoryPath: string,
extractText: boolean = false
-): Promise<number> {
+): Promise<Array<FileSystemEntry>> {
const dirPath = cleanTrailingDirSeparator(directoryPath);
- // if (PlatformIO.isWorkerAvailable() && !PlatformIO.haveObjectStoreSupport()) {
- // // Start indexing in worker if not in the object store mode
- // return PlatformIO.createDirectoryIndexInWorker(dirPath, extractText);
- // }
+ if (PlatformIO.isWorkerAvailable() && !PlatformIO.haveObjectStoreSupport()) {
+ // Start indexing in worker if not in the object store mode
+ return PlatformIO.createDirectoryIndexInWorker(dirPath, extractText);
+ }
- SearchIndex.length = 0;
+ const SearchIndex = [];
+ // eslint-disable-next-line compat/compat
return new Promise((resolve, reject) => {
setTimeout(() => {
let counter = 0;
@@ -151,7 +151,7 @@ export function createDirectoryIndex(
SearchIndex.length
);
console.timeEnd('createDirectoryIndex');
- resolve(SearchIndex.length);
+ resolve(SearchIndex);
return true;
})
.catch(err => {