2019-07-29 15:43:53 +02:00
|
|
|
const React = require('react');
|
2019-07-29 15:58:33 +02:00
|
|
|
|
2022-12-30 16:12:07 +02:00
|
|
|
import { StyleSheet, View, TextInput, FlatList, TouchableHighlight } from 'react-native';
|
2017-11-03 02:09:34 +02:00
|
|
|
const { connect } = require('react-redux');
|
2022-12-30 16:12:07 +02:00
|
|
|
import ScreenHeader from '../ScreenHeader';
|
2017-11-03 02:09:34 +02:00
|
|
|
const Icon = require('react-native-vector-icons/Ionicons').default;
|
2022-12-30 16:12:07 +02:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
|
|
|
import Note from '@joplin/lib/models/Note';
|
|
|
|
import gotoAnythingStyleQuery from '@joplin/lib/services/searchengine/gotoAnythingStyleQuery';
|
2020-11-05 18:58:23 +02:00
|
|
|
const { NoteItem } = require('../note-item.js');
|
|
|
|
const { BaseScreenComponent } = require('../base-screen.js');
|
|
|
|
const { themeStyle } = require('../global-style.js');
|
2017-11-23 20:41:35 +02:00
|
|
|
const DialogBox = require('react-native-dialogbox').default;
|
2022-12-30 16:12:07 +02:00
|
|
|
import SearchEngineUtils from '@joplin/lib/services/searchengine/SearchEngineUtils';
|
|
|
|
import SearchEngine from '@joplin/lib/services/searchengine/SearchEngine';
|
|
|
|
import { AppState } from '../../utils/types';
|
2017-07-23 00:52:24 +02:00
|
|
|
|
2023-01-04 14:54:13 +02:00
|
|
|
// We need this to suppress the useless warning
|
|
|
|
// https://github.com/oblador/react-native-vector-icons/issues/1465
|
2023-01-04 22:25:09 +02:00
|
|
|
Icon.loadFont().catch((error: any) => { console.info(error); });
|
2020-02-09 16:51:12 +02:00
|
|
|
|
2017-07-23 00:52:24 +02:00
|
|
|
class SearchScreenComponent extends BaseScreenComponent {
|
2022-12-30 16:12:07 +02:00
|
|
|
|
|
|
|
private state: any = null;
|
|
|
|
private isMounted_ = false;
|
|
|
|
private styles_: any = {};
|
|
|
|
private scheduleSearchTimer_: any = null;
|
|
|
|
|
2019-09-13 00:16:42 +02:00
|
|
|
static navigationOptions() {
|
2022-12-30 16:12:07 +02:00
|
|
|
return { header: null } as any;
|
2017-07-23 00:52:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.state = {
|
|
|
|
query: '',
|
|
|
|
notes: [],
|
|
|
|
};
|
2017-08-01 19:59:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
styles() {
|
2020-09-15 15:01:07 +02:00
|
|
|
const theme = themeStyle(this.props.themeId);
|
2017-08-01 19:59:01 +02:00
|
|
|
|
2020-09-15 15:01:07 +02:00
|
|
|
if (this.styles_[this.props.themeId]) return this.styles_[this.props.themeId];
|
2017-08-01 19:59:01 +02:00
|
|
|
this.styles_ = {};
|
|
|
|
|
2022-12-30 16:12:07 +02:00
|
|
|
const styles: any = {
|
2017-08-01 19:59:01 +02:00
|
|
|
body: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
searchContainer: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
borderWidth: 1,
|
|
|
|
borderColor: theme.dividerColor,
|
2019-07-29 15:43:53 +02:00
|
|
|
},
|
|
|
|
};
|
2017-08-01 19:59:01 +02:00
|
|
|
|
|
|
|
styles.searchTextInput = Object.assign({}, theme.lineInput);
|
|
|
|
styles.searchTextInput.paddingLeft = theme.marginLeft;
|
|
|
|
styles.searchTextInput.flex = 1;
|
|
|
|
styles.searchTextInput.backgroundColor = theme.backgroundColor;
|
|
|
|
styles.searchTextInput.color = theme.color;
|
|
|
|
|
|
|
|
styles.clearIcon = Object.assign({}, theme.icon);
|
|
|
|
styles.clearIcon.color = theme.colorFaded;
|
|
|
|
styles.clearIcon.paddingRight = theme.marginRight;
|
|
|
|
styles.clearIcon.backgroundColor = theme.backgroundColor;
|
|
|
|
|
2020-09-15 15:01:07 +02:00
|
|
|
this.styles_[this.props.themeId] = StyleSheet.create(styles);
|
|
|
|
return this.styles_[this.props.themeId];
|
2017-07-23 00:52:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.setState({ query: this.props.query });
|
2022-12-30 16:12:07 +02:00
|
|
|
void this.refreshSearch(this.props.query);
|
2017-07-23 00:52:24 +02:00
|
|
|
this.isMounted_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.isMounted_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearButton_press() {
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'SEARCH_QUERY',
|
|
|
|
query: '',
|
|
|
|
});
|
2019-02-03 18:57:29 +02:00
|
|
|
|
|
|
|
this.setState({ query: '' });
|
2022-12-30 16:12:07 +02:00
|
|
|
void this.refreshSearch('');
|
2017-07-23 00:52:24 +02:00
|
|
|
}
|
|
|
|
|
2022-12-30 16:12:07 +02:00
|
|
|
async refreshSearch(query: string = null) {
|
2017-08-01 21:08:38 +02:00
|
|
|
if (!this.props.visible) return;
|
|
|
|
|
2022-12-30 16:12:07 +02:00
|
|
|
query = gotoAnythingStyleQuery(query);
|
2017-07-23 00:52:24 +02:00
|
|
|
|
2022-09-30 12:46:26 +02:00
|
|
|
let notes = [];
|
|
|
|
|
|
|
|
if (query) {
|
|
|
|
if (this.props.settings['db.ftsEnabled']) {
|
|
|
|
notes = await SearchEngineUtils.notesForQuery(query, true);
|
|
|
|
} else {
|
|
|
|
const p = query.split(' ');
|
|
|
|
const temp = [];
|
|
|
|
for (let i = 0; i < p.length; i++) {
|
|
|
|
const t = p[i].trim();
|
|
|
|
if (!t) continue;
|
|
|
|
temp.push(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
notes = await Note.previews(null, {
|
|
|
|
anywherePattern: `*${temp.join('*')}*`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-07-23 00:52:24 +02:00
|
|
|
|
|
|
|
if (!this.isMounted_) return;
|
|
|
|
|
2020-09-28 20:19:21 +02:00
|
|
|
const parsedQuery = await SearchEngine.instance().parseQuery(query);
|
|
|
|
const highlightedWords = SearchEngine.instance().allParsedQueryTerms(parsedQuery);
|
|
|
|
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'SET_HIGHLIGHTED',
|
|
|
|
words: highlightedWords,
|
|
|
|
});
|
|
|
|
|
2017-07-23 00:52:24 +02:00
|
|
|
this.setState({ notes: notes });
|
|
|
|
}
|
|
|
|
|
2022-12-30 16:12:07 +02:00
|
|
|
scheduleSearch() {
|
|
|
|
if (this.scheduleSearchTimer_) clearTimeout(this.scheduleSearchTimer_);
|
|
|
|
|
|
|
|
this.scheduleSearchTimer_ = setTimeout(() => {
|
|
|
|
this.scheduleSearchTimer_ = null;
|
|
|
|
void this.refreshSearch(this.state.query);
|
|
|
|
}, 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
searchTextInput_changeText(text: string) {
|
2017-07-23 00:52:24 +02:00
|
|
|
this.setState({ query: text });
|
2022-12-30 16:12:07 +02:00
|
|
|
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'SEARCH_QUERY',
|
|
|
|
query: text,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.scheduleSearch();
|
2017-07-23 00:52:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-07-25 22:24:30 +02:00
|
|
|
if (!this.isMounted_) return null;
|
|
|
|
|
2020-09-15 15:01:07 +02:00
|
|
|
const theme = themeStyle(this.props.themeId);
|
2017-08-01 21:08:38 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const rootStyle = {
|
2017-08-01 21:08:38 +02:00
|
|
|
flex: 1,
|
|
|
|
backgroundColor: theme.backgroundColor,
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-08-01 21:08:38 +02:00
|
|
|
|
|
|
|
if (!this.props.visible) {
|
|
|
|
rootStyle.flex = 0.001; // This is a bit of a hack but it seems to work fine - it makes the component invisible but without unmounting it
|
|
|
|
}
|
|
|
|
|
2017-11-23 20:41:35 +02:00
|
|
|
const thisComponent = this;
|
|
|
|
|
2017-07-23 00:52:24 +02:00
|
|
|
return (
|
2017-08-01 21:08:38 +02:00
|
|
|
<View style={rootStyle}>
|
2017-11-23 20:41:35 +02:00
|
|
|
<ScreenHeader
|
|
|
|
title={_('Search')}
|
|
|
|
parentComponent={thisComponent}
|
|
|
|
folderPickerOptions={{
|
|
|
|
enabled: this.props.noteSelectionEnabled,
|
|
|
|
mustSelect: true,
|
|
|
|
}}
|
2019-06-28 01:51:02 +02:00
|
|
|
showSideMenuButton={false}
|
|
|
|
showSearchButton={false}
|
2017-11-23 20:41:35 +02:00
|
|
|
/>
|
2017-08-01 19:59:01 +02:00
|
|
|
<View style={this.styles().body}>
|
|
|
|
<View style={this.styles().searchContainer}>
|
2017-07-23 00:52:24 +02:00
|
|
|
<TextInput
|
2017-08-01 19:59:01 +02:00
|
|
|
style={this.styles().searchTextInput}
|
2017-08-01 21:08:38 +02:00
|
|
|
autoFocus={this.props.visible}
|
2019-07-29 15:43:53 +02:00
|
|
|
underlineColorAndroid="#ffffff00"
|
2020-05-21 10:14:33 +02:00
|
|
|
onChangeText={text => this.searchTextInput_changeText(text)}
|
2017-07-23 00:52:24 +02:00
|
|
|
value={this.state.query}
|
2018-04-11 19:25:07 +02:00
|
|
|
selectionColor={theme.textSelectionColor}
|
2020-04-17 00:35:31 +02:00
|
|
|
keyboardAppearance={theme.keyboardAppearance}
|
2017-07-23 00:52:24 +02:00
|
|
|
/>
|
2019-07-29 15:43:53 +02:00
|
|
|
<TouchableHighlight onPress={() => this.clearButton_press()}>
|
|
|
|
<Icon name="md-close-circle" style={this.styles().clearIcon} />
|
2017-07-23 00:52:24 +02:00
|
|
|
</TouchableHighlight>
|
|
|
|
</View>
|
|
|
|
|
2020-05-21 10:14:33 +02:00
|
|
|
<FlatList data={this.state.notes} keyExtractor={(item) => item.id} renderItem={event => <NoteItem note={event.item} />} />
|
2017-07-23 00:52:24 +02:00
|
|
|
</View>
|
2019-07-29 15:43:53 +02:00
|
|
|
<DialogBox
|
2022-12-30 16:12:07 +02:00
|
|
|
ref={(dialogbox: any) => {
|
2019-07-29 15:43:53 +02:00
|
|
|
this.dialogbox = dialogbox;
|
|
|
|
}}
|
|
|
|
/>
|
2017-07-23 00:52:24 +02:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 16:12:07 +02:00
|
|
|
const SearchScreen = connect((state: AppState) => {
|
2019-07-29 15:43:53 +02:00
|
|
|
return {
|
|
|
|
query: state.searchQuery,
|
2020-09-15 15:01:07 +02:00
|
|
|
themeId: state.settings.theme,
|
2019-07-29 15:43:53 +02:00
|
|
|
settings: state.settings,
|
|
|
|
noteSelectionEnabled: state.noteSelectionEnabled,
|
|
|
|
};
|
|
|
|
})(SearchScreenComponent);
|
2017-07-23 00:52:24 +02:00
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = { SearchScreen };
|