TagSpaces is an offline, open source, document manager with tagging support
TSN212
Stanimir Marinov165808b56dbc
diff --git a/app/components/Search.js b/app/components/Search.jsindex 8eb2898df..d0f98637b 100644--- a/app/components/Search.js+++ b/app/components/Search.js@@ -33,7 +33,6 @@ import FolderIcon from '@material-ui/icons/Folder';import ClearSearchIcon from '@material-ui/icons/Clear';import Button from '@material-ui/core/Button';import Input from '@material-ui/core/Input';-import FormLabel from '@material-ui/core/FormLabel';import Radio from '@material-ui/core/Radio';import RadioGroup from '@material-ui/core/RadioGroup';import FormControlLabel from '@material-ui/core/FormControlLabel';@@ -43,12 +42,13 @@ import IconButton from '@material-ui/core/IconButton';import Select from '@material-ui/core/Select';import FormControl from '@material-ui/core/FormControl';import FormHelperText from '@material-ui/core/FormHelperText';-// import Switch from '@material-ui/core/Switch';+import TagSelect from './TagsSelect';import { actions as AppActions, getIndexedEntriesCount, isIndexing, getDirectoryPath } from '../reducers/app';import styles from './SidePanels.css';import i18n from '../services/i18n';import { FileTypeGroups, type SearchQuery } from '../services/search';import { Pro } from '../pro';+import type { Tag } from '../reducers/taglibrary';type Props = {classes: Object,@@ -62,7 +62,7 @@ type Props = {type State = {textQuery?: string,- tagQuery?: string,+ tagQuery?: Array<Tag>,tagConjunction?: string,fileTypes?: Array<string>,tags?: Array<string>,@@ -72,7 +72,7 @@ type State = {class Search extends React.Component<Props, State> {state = {textQuery: '',- tagQuery: '',+ tagQuery: [],tagConjunction: 'OR',fileTypes: FileTypeGroups.any,lastModified: ''@@ -98,7 +98,7 @@ class Search extends React.Component<Props, State> {this.setState({textQuery: '',- tagQuery: '',+ tagQuery: [],tagConjunction: 'OR',fileTypes: FileTypeGroups.any},@@ -107,24 +107,31 @@ class Search extends React.Component<Props, State> {};executeSearch = () => {- let tags = [];- let cleanedTagQuery;+ const tags = [];+if (this.state.tagQuery) {- cleanedTagQuery = this.state.tagQuery.split(',').join(' ');- }- if (cleanedTagQuery && cleanedTagQuery.length) {- tags = cleanedTagQuery.split(' ');+ this.state.tagQuery.map((tag) => {+ Object.entries(tag).map(([key, value]) => {+ if (key === 'title') tags.push(value);+ });+ });}const searchQuery: SearchQuery = {textQuery: this.state.textQuery,fileTypes: this.state.fileTypes,tagConjunction: this.state.tagConjunction,- tags+ tags // TODO put this.state.tagQuery Array<Tag> instead};console.log('Search object: ' + JSON.stringify(searchQuery));this.props.searchLocationIndex(searchQuery);};+ handleChange = (name, value) => {+ this.setState({+ [name]: value+ });+ };+render() {const { classes, indexing, indexedEntriesCount } = this.props;return (@@ -166,15 +173,16 @@ class Search extends React.Component<Props, State> {className={classes.formControl}disabled={indexing}>- <InputLabel htmlFor="searchTags">{i18n.t('core:searchTags')}</InputLabel>- <Input+ {/* <InputLabel htmlFor="searchTags">{i18n.t('core:searchTags')}</InputLabel> */}+ <TagSelect tagQuery={this.state.tagQuery} handleChange={this.handleChange} />+ {/* <Inputid="tagQuery"name="tagQuery"placeholder={i18n.t('core:tagsWithInterval')}value={this.state.tagQuery}onKeyDown={this.startSearch}onChange={this.handleInputChange}- />+ /> */}</FormControl><FormControlcomponent="fieldset"diff --git a/app/components/TagsSelect.js b/app/components/TagsSelect.jsnew file mode 100644index 000000000..bbc16e6c3--- /dev/null+++ b/app/components/TagsSelect.js@@ -0,0 +1,277 @@+/**+ * TagSpaces - universal file and folder organizer+ * Copyright (C) 2017-present TagSpaces UG (haftungsbeschraenkt)+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License (version 3) as+ * published by the Free Software Foundation.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * @flow+ */++import React from 'react';+import { connect } from 'react-redux';+import classNames from 'classnames';+import { withStyles } from '@material-ui/core/styles';+import Typography from '@material-ui/core/Typography';+import Paper from '@material-ui/core/Paper';+import CancelIcon from '@material-ui/icons/Cancel';+import MenuItem from '@material-ui/core/MenuItem';+import Chip from '@material-ui/core/Chip';+import CreatableSelect from 'react-select/lib/Creatable';+import NoSsr from '@material-ui/core/NoSsr';+import TextField from '@material-ui/core/TextField';+import { emphasize } from '@material-ui/core/styles/colorManipulator';+import i18n from '../services/i18n';+import { type Tag, getAllTags } from '../reducers/taglibrary';++const styles = theme => ({+ root: {+ flexGrow: 1,+ height: 250,+ },+ input: {+ display: 'flex',+ padding: 0,+ },+ valueContainer: {+ display: 'flex',+ flexWrap: 'wrap',+ flex: 1,+ alignItems: 'center',+ overflow: 'hidden',+ },+ chip: {+ margin: `${theme.spacing.unit / 2}px ${theme.spacing.unit / 4}px`,+ },+ chipFocused: {+ backgroundColor: emphasize(+ theme.palette.type === 'light' ? theme.palette.grey[300] : theme.palette.grey[700],+ 0.08,+ ),+ },+ noOptionsMessage: {+ padding: `${theme.spacing.unit}px ${theme.spacing.unit * 2}px`,+ },+ singleValue: {+ fontSize: 16,+ },+ placeholder: {+ position: 'absolute',+ left: 2,+ fontSize: 16,+ },+ paper: {+ // position: 'absolute',+ zIndex: 1,+ marginTop: theme.spacing.unit,+ left: 0,+ right: 0,+ },+ divider: {+ height: theme.spacing.unit * 2,+ },+});++function NoOptionsMessage(props) {+ return (+ <Typography+ color="textSecondary"+ className={props.selectProps.classes.noOptionsMessage}+ {...props.innerProps}+ >+ {props.children}+ </Typography>+ );+}++function inputComponent({ inputRef, ...props }) {+ return <div ref={inputRef} {...props} />;+}++function Control(props) {+ return (+ <TextField+ fullWidth+ InputProps={{+ inputComponent,+ inputProps: {+ className: props.selectProps.classes.input,+ inputRef: props.innerRef,+ children: props.children,+ ...props.innerProps,+ },+ }}+ {...props.selectProps.textFieldProps}+ />+ );+}++function Option(props) {+ return (+ <MenuItem+ buttonRef={props.innerRef}+ selected={props.isFocused}+ component="div"+ style={{+ fontWeight: props.isSelected ? 500 : 400,+ }}+ {...props.innerProps}+ >+ {props.children}+ </MenuItem>+ );+}++function Placeholder(props) {+ return (+ <Typography+ color="textSecondary"+ className={props.selectProps.classes.placeholder}+ {...props.innerProps}+ >+ {props.children}+ </Typography>+ );+}++function SingleValue(props) {+ return (+ <Typography className={props.selectProps.classes.singleValue} {...props.innerProps}>+ {props.children}+ </Typography>+ );+}++function ValueContainer(props) {+ return <div className={props.selectProps.classes.valueContainer}>{props.children}</div>;+}++function MultiValue(props) {+ return (+ <Chip+ tabIndex={-1}+ label={props.children}+ className={classNames(props.selectProps.classes.chip, {+ [props.selectProps.classes.chipFocused]: props.isFocused,+ })}+ onDelete={props.removeProps.onClick}+ deleteIcon={<CancelIcon {...props.removeProps} />}+ />+ );+}++function Menu(props) {+ return (+ <Paper square className={props.selectProps.classes.paper} {...props.innerProps}>+ {props.children}+ </Paper>+ );+}++const components = {+ Control,+ Menu,+ MultiValue,+ NoOptionsMessage,+ Option,+ Placeholder,+ SingleValue,+ ValueContainer,+};++type Props = {+ classes: Object,+ theme: Object,+ tagQuery: string,+ handleChange: () => void,+ allTags: Array<Tag>+};++class TagsSelect extends React.Component<Props> {+ handleChange = (newValue: any, actionMeta: any) => {+ console.group('Value Changed');+ console.log(newValue);+ console.log(`action: ${actionMeta.action}`);+ console.groupEnd();++ if (actionMeta.action === 'select-option') {+ this.props.handleChange('tagQuery', newValue);+ } else if (actionMeta.action === 'create-option') {+ this.props.allTags.push(newValue);+ this.props.handleChange('tagQuery', newValue);+ } else if (actionMeta.action === 'remove-value') {+ this.props.handleChange('tagQuery', newValue);+ } else if (actionMeta.action === 'clear') {+ this.props.handleChange('tagQuery', []);+ }+ };++ /**+ * temp fix for https://github.com/JedWatson/react-select/issues/2630+ */+ isValidNewOption = (inputValue, selectValue, selectOptions) => !(inputValue.trim().length === 0 ||+ selectOptions.find(option => option.name === inputValue));++ render() {+ const { classes, theme, allTags, tagQuery } = this.props;++ const selectStyles = {+ input: base => ({+ ...base,+ color: theme.palette.text.primary,+ '& input': {+ font: 'inherit',+ },+ }),+ };++ return (+ <NoSsr>+ <CreatableSelect+ isClearable+ classes={classes}+ options={allTags}+ getOptionLabel={(option) => option.title}+ getOptionValue={(option) => option.id}+ isValidNewOption={this.isValidNewOption}+ getNewOptionData={(inputValue, optionLabel) => ({+ id: inputValue,+ title: optionLabel,+ })}+ styles={selectStyles}+ fullWidth={true}+ components={components}+ /* textFieldProps={{+ label: 'title',+ InputLabelProps: {+ shrink: true,+ },+ }} */+ value={tagQuery}+ onChange={this.handleChange}+ // onInputChange={this.handleInputSelectChange}+ // onCreateOption={this.handleCreate}+ placeholder={i18n.t('core:searchTags')}+ isMulti+ // isSearchable+ />+ </NoSsr>+ );+ }+}++const mapStateToProps = state => ({+ allTags: getAllTags(state),+});+++export default connect(mapStateToProps)(withStyles(styles, { withTheme: true })(TagsSelect));diff --git a/app/services/search.js b/app/services/search.jsindex 916446074..e5ce84089 100644--- a/app/services/search.js+++ b/app/services/search.js@@ -46,7 +46,7 @@ export type SearchQuery = {textQuery?: string,fileTypes?: Array<string>,tagConjunction?: 'AND' | 'OR',- tags?: Array<string>,+ tags?: Array<string>, // TODO Array<Tag>lastChanged?: Date};