TagSpaces is an offline, open source, document manager with tagging support
extending search to support time periods and plus codes
Ilian Sapundshievb8ec37baf5ae
diff --git a/app/components/Search.js b/app/components/Search.jsindex 9005d57d5..b5a61fd28 100644--- a/app/components/Search.js+++ b/app/components/Search.js@@ -36,8 +36,11 @@ import FileIcon from '@material-ui/icons/InsertDriveFileOutlined';import ClearSearchIcon from '@material-ui/icons/Clear';import BookmarkIcon from '@material-ui/icons/BookmarkBorder';import BookIcon from '@material-ui/icons/LocalLibraryOutlined';+import PlaceIcon from '@material-ui/icons/Place';+import DateIcon from '@material-ui/icons/DateRange';import Button from '@material-ui/core/Button';import Input from '@material-ui/core/Input';+import TextField from '@material-ui/core/TextField';import InputLabel from '@material-ui/core/InputLabel';import InputAdornment from '@material-ui/core/InputAdornment';import IconButton from '@material-ui/core/IconButton';@@ -53,6 +56,15 @@ import i18n from '../services/i18n';import { FileTypeGroups, type SearchQuery } from '../services/search';import { Pro } from '../pro';import type { Tag } from '../reducers/taglibrary';+import ocl from '../utils/openlocationcode';+import {+ formatDateTime,+ extractTimePeriod+} from '../utils/dates';+import {+ isPlusCode,+ parseLatLon+} from '../utils/misc';type Props = {classes: Object,@@ -73,6 +85,15 @@ type State = {fileTypes: Array<string>,searchBoxing: 'location' | 'folder',lastModified: string,+ tagTimePeriod: string,+ tagTimePeriodHelper: string,+ tagPlace: string,+ tagPlaceHelper: string,+ tagTimePeriodFrom: Date | null,+ tagTimePeriodTo: Date | null,+ tagPlaceLat: number | null,+ tagPlaceLong: number | null,+ tagPlaceRadius: number,fileSize: string};@@ -85,6 +106,15 @@ class Search extends React.Component<Props, State> {fileTypes: FileTypeGroups.any,searchBoxing: 'location',lastModified: '',+ tagTimePeriod: '',+ tagTimePeriodHelper: '',+ tagPlace: '',+ tagPlaceHelper: '',+ tagTimePeriodFrom: null,+ tagTimePeriodTo: null,+ tagPlaceLat: null,+ tagPlaceLong: null,+ tagPlaceRadius: 0,fileSize: ''};@@ -98,6 +128,59 @@ class Search extends React.Component<Props, State> {});};+ handleTimePeriodChange = event => {+ const target = event.target;+ const value = target.value;+ let tagTimePeriodHelper = '';+ const { fromDateTime, toDateTime } = extractTimePeriod(value);++ if (toDateTime && fromDateTime) {+ tagTimePeriodHelper = 'From: ' + formatDateTime(fromDateTime) + ' To: ' + formatDateTime(toDateTime);+ } else {+ tagTimePeriodHelper = 'No period selected';+ }++ this.setState({+ tagTimePeriod: value,+ tagTimePeriodFrom: fromDateTime,+ tagTimePeriodTo: toDateTime,+ tagTimePeriodHelper+ });+ };++ handlePlaceChange = event => {+ const target = event.target;+ const value = target.value;+ let lat = null;+ let lon = null;+ let tagPlaceHelper;++ if (isPlusCode(value)) {+ const coord = ocl.decode(value);+ lat = Number(coord.latitudeCenter.toFixed(7));+ lon = Number(coord.longitudeCenter.toFixed(7));+ } else {+ const latLon = parseLatLon(value);+ if (latLon) {+ lat = latLon.lat;+ lon = latLon.lon;+ }+ }++ if (lat && lon) {+ tagPlaceHelper = 'Place at lat: ' + lat + ' long: ' + lon;+ } else {+ tagPlaceHelper = 'No place selected';+ }++ this.setState({+ tagPlace: value,+ tagPlaceLat: lat,+ tagPlaceLong: lon,+ tagPlaceHelper+ });+ };+startSearch = event => {if (event.key === 'Enter' || event.keyCode === 13) {this.executeSearch();@@ -105,19 +188,25 @@ class Search extends React.Component<Props, State> {};clearSearch = () => {- this.setState(- {- textQuery: '',- tagsAND: [],- tagsOR: [],- tagsNOT: [],- searchBoxing: 'location',- fileTypes: FileTypeGroups.any,- lastModified: '',- fileSize: ''- },- () => this.props.loadDirectoryContent(this.props.currentDirectory)- );+ this.setState({+ textQuery: '',+ tagsAND: [],+ tagsOR: [],+ tagsNOT: [],+ searchBoxing: 'location',+ fileTypes: FileTypeGroups.any,+ lastModified: '',+ tagTimePeriod: '',+ tagTimePeriodHelper: '',+ tagPlace: '',+ tagPlaceHelper: '',+ tagTimePeriodFrom: null,+ tagTimePeriodTo: null,+ tagPlaceLat: null,+ tagPlaceLong: null,+ tagPlaceRadius: 0,+ fileSize: ''+ }, () => this.props.loadDirectoryContent(this.props.currentDirectory));};toggleSearchBoxing = () => {@@ -138,6 +227,11 @@ class Search extends React.Component<Props, State> {fileTypes: this.state.fileTypes,lastModified: this.state.lastModified,fileSize: this.state.fileSize,+ tagTimePeriodFrom: this.state.tagTimePeriodFrom ? this.state.tagTimePeriodFrom.getTime() : null,+ tagTimePeriodTo: this.state.tagTimePeriodTo ? this.state.tagTimePeriodTo.getTime() : null,+ tagPlaceLat: this.state.tagPlaceLat,+ tagPlaceLong: this.state.tagPlaceLong,+ tagPlaceRadius: this.state.tagPlaceRadius,maxSearchResults: this.props.maxSearchResults,currentDirectory: this.props.currentDirectory};@@ -228,7 +322,7 @@ class Search extends React.Component<Props, State> {<FormControlclassName={classes.formControl}disabled={indexing || !Pro}- title={i18n.t('core:thisFunctionalityIsAvailableInPro')}+ title={!Pro && i18n.t('core:thisFunctionalityIsAvailableInPro')}><InputLabel htmlFor="file-type">{i18n.t('core:fileType')}</InputLabel><Select@@ -336,6 +430,44 @@ class Search extends React.Component<Props, State> {className={classes.formControl}disabled={indexing || !Pro}title={i18n.t('core:thisFunctionalityIsAvailableInPro')}+ >+ <InputLabel shrink htmlFor="file-size">{i18n.t('core:sizeSearchTitle')}</InputLabel>+ <Select+ value={this.state.fileSize}+ onChange={this.handleInputChange}+ input={<Input name="fileSize" id="file-size" />}+ displayEmpty+ >+ <MenuItem value="">+ {i18n.t('core:sizeAny')}+ </MenuItem>+ <MenuItem value="sizeEmpty">+ {i18n.t('core:sizeEmpty')}+ </MenuItem>+ <MenuItem value="sizeTiny">+ {i18n.t('core:sizeTiny')} (< 10KB)+ </MenuItem>+ <MenuItem value="sizeVerySmall">+ {i18n.t('core:sizeVerySmall')} (< 100KB)+ </MenuItem>+ <MenuItem value="sizeSmall">+ {i18n.t('core:sizeSmall')} (< 1MB)+ </MenuItem>+ <MenuItem value="sizeMedium">+ {i18n.t('core:sizeMedium')} (< 50MB)+ </MenuItem>+ <MenuItem value="sizeLarge">+ {i18n.t('core:sizeLarge')} (< 1GB)+ </MenuItem>+ <MenuItem value="sizeHuge">+ {i18n.t('core:sizeHuge')} (> 1GB)+ </MenuItem>+ </Select>+ </FormControl>+ <FormControl+ className={classes.formControl}+ disabled={indexing || !Pro}+ title={!Pro && i18n.t('core:thisFunctionalityIsAvailableInPro')}><InputLabel shrink htmlFor="modification-date">{i18n.t('core:lastModifiedSearchTitle')}</InputLabel><Select@@ -373,40 +505,42 @@ class Search extends React.Component<Props, State> {<FormControlclassName={classes.formControl}disabled={indexing || !Pro}- title={i18n.t('core:thisFunctionalityIsAvailableInPro')}+ title={!Pro && i18n.t('core:thisFunctionalityIsAvailableInPro')}>- <InputLabel shrink htmlFor="file-size">{i18n.t('core:sizeSearchTitle')}</InputLabel>- <Select- value={this.state.fileSize}- onChange={this.handleInputChange}- input={<Input name="fileSize" id="file-size" />}- displayEmpty- >- <MenuItem value="">- {i18n.t('core:sizeAny')}- </MenuItem>- <MenuItem value="sizeEmpty">- {i18n.t('core:sizeEmpty')}- </MenuItem>- <MenuItem value="sizeTiny">- {i18n.t('core:sizeTiny')} (< 10KB)- </MenuItem>- <MenuItem value="sizeVerySmall">- {i18n.t('core:sizeVerySmall')} (< 100KB)- </MenuItem>- <MenuItem value="sizeSmall">- {i18n.t('core:sizeSmall')} (< 1MB)- </MenuItem>- <MenuItem value="sizeMedium">- {i18n.t('core:sizeMedium')} (< 50MB)- </MenuItem>- <MenuItem value="sizeLarge">- {i18n.t('core:sizeLarge')} (< 1GB)- </MenuItem>- <MenuItem value="sizeHuge">- {i18n.t('core:sizeHuge')} (> 1GB)- </MenuItem>- </Select>+ <TextField+ id="tagTimePeriod"+ label={i18n.t('Enter time period')}+ value={this.state.tagTimePeriod}+ onChange={this.handleTimePeriodChange}+ onKeyDown={this.startSearch}+ helperText={this.state.tagTimePeriodHelper}+ InputProps={{+ endAdornment: (+ <InputAdornment position="end" title="201905 for May 2019 / 20190412 for 12th of April 2019 / 20190501~124523 for specific time">+ <IconButton>+ <DateIcon />+ </IconButton>+ </InputAdornment>+ )+ }}+ />+ <TextField+ id="tagPlace"+ label={i18n.t('GPS coordinates or plus code')}+ value={this.state.tagPlace}+ onChange={this.handlePlaceChange}+ onKeyDown={this.startSearch}+ helperText={this.state.tagPlaceHelper}+ InputProps={{+ endAdornment: (+ <InputAdornment position="end" title="GPS: 49.23276,12.43123 PlusCode: 8FRG8Q87+6X">+ <IconButton>+ <PlaceIcon />+ </IconButton>+ </InputAdornment>+ )+ }}+ /></FormControl><FormControl className={classes.formControl}><Buttondiff --git a/app/services/search.js b/app/services/search.jsindex 97b22613b..3f6a3b052 100644--- a/app/services/search.js+++ b/app/services/search.js@@ -18,15 +18,10 @@*/import Fuse from 'fuse.js';-// import ElasticLunr from 'elasticlunr';import jmespath from 'jmespath';-import AppConfig from '../config';+import { type Tag } from '../reducers/taglibrary';import { Pro } from '../pro';-let currentQuery = '';-let nextQuery = '';-let caseSensitiveSearch = false; // TODO implement case sensitive search-// export type FileTypeGroups = 'images' | 'notes' | 'documents' | 'audio' | 'video' | 'archives';export const FileTypeGroups = {@@ -54,6 +49,11 @@ export type SearchQuery = {fileSize: string,searchBoxing: 'location' | 'folder',currentDirectory: string,+ tagTimePeriodFrom?: number | null,+ tagTimePeriodTo?: number | null,+ tagPlaceLat?: number | null,+ tagPlaceLong?: number | null,+ tagPlaceRadius?: number | null,maxSearchResults?: number};@@ -192,22 +192,27 @@ function constructjmespathQuery(searchQuery: SearchQuery): string {return jmespathQuery;}-function transformTagsToLowercase(index: Array<Object>) {- console.time('taglowercase');- const resultIndex = index.map((entry) => {- const tags = [...entry.tags];- if (tags && tags.length) {- tags.map((tag) => {- tag.title = tag.title.toLowerCase();- return tag;- });- }- return {- ...entry,- tags- };- });- console.timeEnd('taglowercase');+function prepareIndex(index: Array<Object>) {+ console.time('PreparingIndex');+ let resultIndex = [];+ if (Pro && Pro.Search && Pro.Search.prepareIndex) {+ resultIndex = Pro.Search.prepareIndex(index);+ } else {+ resultIndex = index.map((entry) => {+ const tags = [...entry.tags];+ if (tags && tags.length) {+ tags.map((tag) => {+ tag.title = tag.title.toLowerCase();+ return tag;+ });+ }+ return {+ ...entry,+ tags+ };+ });+ }+ console.timeEnd('PreparingIndex');return resultIndex;}@@ -215,7 +220,7 @@ export default class Search {static searchLocationIndex = (locationContent: Array<Object>, searchQuery: SearchQuery): Promise<Array<Object> | []> => new Promise((resolve) => {console.time('searchtime');const jmespathQuery = constructjmespathQuery(searchQuery);- let results = transformTagsToLowercase(locationContent);+ let results = prepareIndex(locationContent);let searched = false;// Limiting the search to current folder only (with sub-folders)diff --git a/app/utils/dates.js b/app/utils/dates.jsindex 510c0c70a..f98ccff54 100644--- a/app/utils/dates.js+++ b/app/utils/dates.js@@ -60,9 +60,84 @@ export function isYearMonthDayHourMinSec(tagDate: string): boolean {return /(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789]~[0123456][0123456789][0123456][0123456789][0123456][0123456789])(\s|$)/.test(tagDate);}+/** Returns the number of day in month, January = 1 -> 31 .. December = 12 */+export function getDaysInMonth(year: number, month: number): number {+ return new Date(year, month, 0).getDate();+}++export const msInDay = 1000 * 60 * 60 * 24;++/** Extract the time period from string e.g. 201902 -> fromDate: 2019-01-01 00:00:00, toDate; 2019-01-31 23:59:59 */+export function extractTimePeriod(value: string): { fromDateTime: Date | null, toDateTime: Date | null} {+ let fromDateTime = null;+ let toDateTime = null;+ if (value.length && !/(^|\s)([0123456789])(\s|$)/.test(value.substr(0, 1))) {+ return { // Return if the first char is not a number, ignoring values starting with letters+ fromDateTime,+ toDateTime+ };+ }+ try {+ if (isYear(value)) {+ fromDateTime = new Date(value + '-01-01');+ toDateTime = new Date(value + '-12-31 23:59:59.999');+ } else if (isYearMonth(value)) {+ const year = value.substring(0, 4);+ const month = value.substring(4, 6);+ fromDateTime = new Date(year + '-' + month + '-01');+ toDateTime = new Date(year + '-' + month + '-' + getDaysInMonth(parseInt(year, 10), parseInt(month, 10)) + ' 23:59:59.999');+ } else if (isYearMonthDay(value)) {+ const year = value.substring(0, 4);+ const month = value.substring(4, 6);+ const day = value.substring(6, 8);+ fromDateTime = new Date(year + '-' + month + '-' + day);+ toDateTime = new Date(year + '-' + month + '-' + day + ' 23:59:59.999');+ } else if (isYearMonthDayHour(value)) {+ const year = value.substring(0, 4);+ const month = value.substring(4, 6);+ const day = value.substring(6, 8);+ const hour = value.substring(9, 11);+ fromDateTime = new Date(year + '-' + month + '-' + day + ' ' + hour + ':00:00');+ toDateTime = new Date(year + '-' + month + '-' + day + ' ' + hour + ':59:59.999');+ } else if (isYearMonthDayHourMin(value)) {+ const year = value.substring(0, 4);+ const month = value.substring(4, 6);+ const day = value.substring(6, 8);+ const hour = value.substring(9, 11);+ const min = value.substring(11, 13);+ fromDateTime = new Date(year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':00');+ toDateTime = new Date(year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':59.999');+ } else if (isYearMonthDayHourMinSec(value)) {+ const year = value.substring(0, 4);+ const month = value.substring(4, 6);+ const day = value.substring(6, 8);+ const hour = value.substring(9, 11);+ const min = value.substring(11, 13);+ const sec = value.substring(13, 15);+ fromDateTime = new Date(year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + sec);+ toDateTime = new Date(year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + sec + '.999');+ }+ } catch (err) {+ console.log('Error extracting date ' + err);+ }+ return {+ fromDateTime,+ toDateTime+ };+}++export function pad(number: number) {+ return (number < 10) ? '0' + number : number;+}+/** Convert a date in this 2013-01-02 12:23:58 format */export function formatDateTime(date: Date): string {- return date.toISOString().substring(0, 19).split('T').join(' ');+ return date.getFullYear() ++ '-' + pad(date.getMonth() + 1) ++ '-' + pad(date.getDate()) ++ ' ' + pad(date.getHours()) ++ ':' + pad(date.getMinutes()) ++ ':' + pad(date.getSeconds());}/** Converts 'Sun May 11, 2014' to 2014-05 */