2020-09-15 15:01:07 +02:00
|
|
|
import * as React from 'react';
|
2020-11-09 14:07:37 +02:00
|
|
|
import { useState, useCallback, useEffect, useRef } from 'react';
|
2021-01-07 18:30:53 +02:00
|
|
|
import SearchInput from '../lib/SearchInput/SearchInput';
|
2020-11-09 14:07:37 +02:00
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
|
|
|
import { stateUtils } from '@joplin/lib/reducer';
|
|
|
|
import BaseModel from '@joplin/lib/BaseModel';
|
|
|
|
import uuid from '@joplin/lib/uuid';
|
2020-10-09 19:35:46 +02:00
|
|
|
const { connect } = require('react-redux');
|
2021-01-22 19:41:11 +02:00
|
|
|
import Note from '@joplin/lib/models/Note';
|
2021-12-21 19:29:47 +02:00
|
|
|
import { AppState } from '../../app.reducer';
|
2024-04-01 16:34:22 +02:00
|
|
|
import { blur, focus } from '@joplin/lib/utils/focusHandler';
|
2020-11-09 14:07:37 +02:00
|
|
|
const debounce = require('debounce');
|
2021-01-07 18:30:53 +02:00
|
|
|
const styled = require('styled-components').default;
|
|
|
|
|
|
|
|
export const Root = styled.div`
|
|
|
|
position: relative;
|
|
|
|
display: flex;
|
|
|
|
width: 100%;
|
2022-01-08 21:12:18 +02:00
|
|
|
min-width: 30px;
|
2021-01-07 18:30:53 +02:00
|
|
|
`;
|
2020-09-15 15:01:07 +02:00
|
|
|
|
|
|
|
interface Props {
|
2020-11-12 21:29:22 +02:00
|
|
|
inputRef?: any;
|
|
|
|
notesParentType: string;
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
dispatch?: Function;
|
|
|
|
selectedNoteId: string;
|
2021-12-21 19:29:47 +02:00
|
|
|
isFocused?: boolean;
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
function SearchBar(props: Props) {
|
2020-09-15 15:01:07 +02:00
|
|
|
const [query, setQuery] = useState('');
|
2020-11-09 14:07:37 +02:00
|
|
|
const [searchStarted, setSearchStarted] = useState(false);
|
|
|
|
const searchId = useRef(uuid.create());
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
function search(searchId: string, query: string, dispatch: Function) {
|
2020-11-09 14:07:37 +02:00
|
|
|
dispatch({
|
|
|
|
type: 'SEARCH_UPDATE',
|
|
|
|
search: {
|
|
|
|
id: searchId,
|
|
|
|
title: query,
|
|
|
|
query_pattern: query,
|
|
|
|
query_folder_id: null,
|
|
|
|
type_: BaseModel.TYPE_SEARCH,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'SEARCH_SELECT',
|
|
|
|
id: searchId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const debouncedSearch = debounce(search, 500);
|
|
|
|
if (searchStarted) debouncedSearch(searchId.current, query, props.dispatch);
|
|
|
|
return () => {
|
|
|
|
debouncedSearch.clear();
|
|
|
|
};
|
2022-08-19 13:10:04 +02:00
|
|
|
// eslint-disable-next-line @seiyab/react-hooks/exhaustive-deps -- Old code before rule was applied
|
2020-11-09 14:07:37 +02:00
|
|
|
}, [query, searchStarted]);
|
|
|
|
|
|
|
|
const onExitSearch = useCallback(async (navigateAway = true) => {
|
|
|
|
setQuery('');
|
|
|
|
setSearchStarted(false);
|
|
|
|
|
|
|
|
if (navigateAway) {
|
|
|
|
const note = props.selectedNoteId ? await Note.load(props.selectedNoteId) : null;
|
|
|
|
|
|
|
|
if (note) {
|
|
|
|
props.dispatch({
|
|
|
|
type: 'FOLDER_AND_NOTE_SELECT',
|
|
|
|
folderId: note.parent_id,
|
|
|
|
noteId: note.id,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const folderId = Setting.value('activeFolderId');
|
|
|
|
if (folderId) {
|
|
|
|
props.dispatch({
|
|
|
|
type: 'FOLDER_SELECT',
|
|
|
|
id: folderId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-19 13:10:04 +02:00
|
|
|
// eslint-disable-next-line @seiyab/react-hooks/exhaustive-deps -- Old code before rule was applied
|
2020-11-09 14:07:37 +02:00
|
|
|
}, [props.selectedNoteId]);
|
2020-09-15 15:01:07 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
function onChange(event: any) {
|
2021-04-24 18:30:07 +02:00
|
|
|
if (event.value.length === 0) {
|
|
|
|
// Revert to previous state if query string becomes empty
|
|
|
|
void onExitSearch();
|
|
|
|
return;
|
|
|
|
}
|
2020-11-09 14:07:37 +02:00
|
|
|
setSearchStarted(true);
|
2021-01-07 18:30:53 +02:00
|
|
|
setQuery(event.value);
|
2020-09-29 13:31:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function onFocus() {
|
|
|
|
props.dispatch({
|
|
|
|
type: 'FOCUS_SET',
|
|
|
|
field: 'globalSearch',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function onBlur() {
|
|
|
|
// Do it after a delay so that the "Clear" button
|
|
|
|
// can be clicked on (otherwise the field loses focus
|
|
|
|
// and is resized before the click event has been processed)
|
|
|
|
setTimeout(() => {
|
|
|
|
props.dispatch({
|
|
|
|
type: 'FOCUS_CLEAR',
|
|
|
|
field: 'globalSearch',
|
|
|
|
});
|
|
|
|
}, 300);
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
const onKeyDown = useCallback((event: any) => {
|
2020-09-29 13:31:19 +02:00
|
|
|
if (event.key === 'Escape') {
|
2024-04-01 16:34:22 +02:00
|
|
|
if (document.activeElement) blur('SearchBar::onKeyDown', document.activeElement as any);
|
2020-11-25 16:40:25 +02:00
|
|
|
void onExitSearch();
|
2020-09-29 13:31:19 +02:00
|
|
|
}
|
2020-11-09 14:07:37 +02:00
|
|
|
}, [onExitSearch]);
|
2020-09-15 15:01:07 +02:00
|
|
|
|
|
|
|
const onSearchButtonClick = useCallback(() => {
|
2022-06-14 15:24:51 +02:00
|
|
|
if (props.isFocused || searchStarted) {
|
2021-12-21 19:29:47 +02:00
|
|
|
void onExitSearch();
|
|
|
|
} else {
|
|
|
|
setSearchStarted(true);
|
2024-04-01 16:34:22 +02:00
|
|
|
focus('SearchBar::onSearchButtonClick', props.inputRef.current);
|
2021-12-21 19:29:47 +02:00
|
|
|
props.dispatch({
|
|
|
|
type: 'FOCUS_SET',
|
|
|
|
field: 'globalSearch',
|
|
|
|
});
|
|
|
|
}
|
2022-08-19 13:10:04 +02:00
|
|
|
// eslint-disable-next-line @seiyab/react-hooks/exhaustive-deps -- Old code before rule was applied
|
2022-06-14 15:24:51 +02:00
|
|
|
}, [onExitSearch, props.isFocused, searchStarted]);
|
2020-09-15 15:01:07 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (props.notesParentType !== 'Search') {
|
2020-11-25 16:40:25 +02:00
|
|
|
void onExitSearch(false);
|
2020-09-15 15:01:07 +02:00
|
|
|
}
|
2020-11-09 14:07:37 +02:00
|
|
|
}, [props.notesParentType, onExitSearch]);
|
2020-09-15 15:01:07 +02:00
|
|
|
|
2022-01-15 19:05:31 +02:00
|
|
|
// When the searchbar is remounted, exit the search if it was previously open
|
|
|
|
// or else other buttons stay hidden (e.g. when opening Layout Editor and closing it)
|
|
|
|
// https://github.com/laurent22/joplin/issues/5953
|
|
|
|
useEffect(() => {
|
|
|
|
if (props.notesParentType === 'Search' || props.isFocused) {
|
|
|
|
if (props.isFocused) {
|
|
|
|
props.dispatch({
|
|
|
|
type: 'FOCUS_CLEAR',
|
|
|
|
field: 'globalSearch',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
void onExitSearch(true);
|
|
|
|
}
|
2022-08-19 13:10:04 +02:00
|
|
|
// eslint-disable-next-line @seiyab/react-hooks/exhaustive-deps -- Old code before rule was applied
|
2022-01-15 19:05:31 +02:00
|
|
|
}, []);
|
|
|
|
|
2020-09-15 15:01:07 +02:00
|
|
|
return (
|
2021-05-17 20:33:44 +02:00
|
|
|
<Root className="search-bar">
|
2020-09-29 13:31:19 +02:00
|
|
|
<SearchInput
|
2021-01-07 18:30:53 +02:00
|
|
|
inputRef={props.inputRef}
|
2020-09-29 13:31:19 +02:00
|
|
|
value={query}
|
|
|
|
onChange={onChange}
|
|
|
|
onFocus={onFocus}
|
|
|
|
onBlur={onBlur}
|
|
|
|
onKeyDown={onKeyDown}
|
2021-01-07 18:30:53 +02:00
|
|
|
onSearchButtonClick={onSearchButtonClick}
|
|
|
|
searchStarted={searchStarted}
|
2020-09-29 13:31:19 +02:00
|
|
|
/>
|
2020-09-15 15:01:07 +02:00
|
|
|
</Root>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-21 19:29:47 +02:00
|
|
|
const mapStateToProps = (state: AppState) => {
|
2020-09-15 15:01:07 +02:00
|
|
|
return {
|
|
|
|
notesParentType: state.notesParentType,
|
2020-11-09 14:07:37 +02:00
|
|
|
selectedNoteId: stateUtils.selectedNoteId(state),
|
2021-12-21 19:29:47 +02:00
|
|
|
isFocused: state.focusedField === 'globalSearch',
|
2020-09-15 15:01:07 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(SearchBar);
|