Skip to content

Commit

Permalink
highlight search term in notes list
Browse files Browse the repository at this point in the history
  • Loading branch information
shola committed Mar 15, 2018
1 parent 3552c6a commit c489d77
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/containers/NoteCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export default class NoteCell extends React.PureComponent {
deleting: {
color: GlobalStyles.constants().mainTintColor,
marginBottom: 5,
},

highlight: {
backgroundColor: GlobalStyles.constants().mainTintColor
}
});
}
Expand Down Expand Up @@ -112,6 +116,7 @@ export default class NoteCell extends React.PureComponent {

static ActionSheetCancelIndex = 0;
static ActionSheetDestructiveIndex = 4;
static highlightId = 0;

actionSheetActions() {
var pinAction = this.props.item.pinned ? "Unpin" : "Pin";
Expand Down Expand Up @@ -148,6 +153,33 @@ export default class NoteCell extends React.PureComponent {

}

highlightSearchTerms = (note) => {
const textStyle = this.aggregateStyles(this.styles.noteText, this.styles.noteTextSelected, this.state.selected);

if (!this.props.searchTerm || this.props.searchTerm.length === 0) {
return (<Text numberOfLines={2} style={textStyle}>{note.text}</Text>);
}

const searchTermRe = new RegExp(`(${this.props.searchTerm})`, 'i');
// The number of characters in a note that displays varies by screen width.
// As a rough estimate, assume that the max number of visible characters
// is 280 (twice the twitter limit).
const visibletext = note.text.slice(0, 280);
// Split truncated text by searchterm, and also capture searchTerm matches
const splitText = visibletext.split(searchTermRe);
// Highlight occurences of searchterm in the visibleText.
return (
<Text numberOfLines={2} style={textStyle}>
{splitText.map((text) => {
if (searchTermRe.test(text)) {
return (<Text key={note.uuid + '-highlight-' + NoteCell.highlightId++} style={this.styles.highlight}>{text}</Text>);
} else {
return text;
}
})}
</Text>);
}

render() {
var note = this.props.item;
return (
Expand Down Expand Up @@ -192,9 +224,7 @@ export default class NoteCell extends React.PureComponent {
<Text style={this.aggregateStyles(this.styles.noteTitle, this.styles.noteTitleSelected, this.state.selected)}>{note.title}</Text>
}

{note.safeText().length > 0 &&
<Text numberOfLines={2} style={this.aggregateStyles(this.styles.noteText, this.styles.noteTextSelected, this.state.selected)}>{note.text}</Text>
}
{note.safeText().length > 0 && this.highlightSearchTerms(note)}

<Text
numberOfLines={1}
Expand Down
1 change: 1 addition & 0 deletions src/containers/NoteList.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default class NoteList extends Component {
archived={item.archived}
sortType={this.props.sortType}
renderTags={renderTags}
searchTerm={this.props.searchTerm}
/>
)
}
Expand Down
1 change: 1 addition & 0 deletions src/screens/Notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ export default class Notes extends Abstract {
decrypting={this.state.decrypting}
loading={this.state.loading}
selectedTags={tags}
searchTerm={this.options.searchTerm}
/>
}

Expand Down

0 comments on commit c489d77

Please sign in to comment.