- {this.renderLanguageIcon(codeSnippet.language)}
- {this.boldNameOnSearch(this.state.searchValue, language, name)}
+
+
+ {this.renderLanguageIcon(language)}
+ {this.boldNameOnSearch(id, language, name)}
-
+
{actionButtons.map(btn => {
return (
-
-
{`${codeSnippet.description}`}
+
+
{`${codeSnippet.description}`}
@@ -1171,24 +1196,27 @@ export class CodeSnippetDisplay extends React.Component<
if (state.searchValue === '' && state.filterTags.length === 0) {
return {
codeSnippets: props.codeSnippets,
+ matchIndices: [],
searchValue: '',
filterTags: []
};
}
if (state.searchValue !== '' || state.filterTags.length !== 0) {
- const newSnippets = props.codeSnippets.filter(codeSnippet => {
- return (
- (state.searchValue !== '' &&
- codeSnippet.name.toLowerCase().includes(state.searchValue)) ||
- (state.searchValue !== '' &&
- codeSnippet.language.toLowerCase().includes(state.searchValue)) ||
- (codeSnippet.tags &&
- codeSnippet.tags.some(tag => state.filterTags.includes(tag)))
- );
- });
+ // const newSnippets = props.codeSnippets.filter(codeSnippet => {
+ // return (
+ // state.matchIndices[codeSnippet.id] !== null ||
+ // // (state.searchValue !== '' &&
+ // // codeSnippet.name.toLowerCase().includes(state.searchValue)) ||
+ // // (state.searchValue !== '' &&
+ // // codeSnippet.language.toLowerCase().includes(state.searchValue)) ||
+ // (codeSnippet.tags &&
+ // codeSnippet.tags.some(tag => state.filterTags.includes(tag)))
+ // );
+ // });
return {
- codeSnippets: newSnippets,
+ codeSnippets: state.codeSnippets,
+ matchIndices: state.matchIndices,
searchValue: state.searchValue,
filterTags: state.filterTags
};
@@ -1198,29 +1226,73 @@ export class CodeSnippetDisplay extends React.Component<
filterSnippets = (searchValue: string, filterTags: string[]): void => {
// filter with search
- let filteredSnippets = this.props.codeSnippets.filter(
- codeSnippet =>
- codeSnippet.name.toLowerCase().includes(searchValue.toLowerCase()) ||
- codeSnippet.language.toLowerCase().includes(searchValue.toLowerCase())
- );
+ let matchIndices: number[][] = [];
+ const matchResults: StringExt.IMatchResult[] = [];
+ let filteredSnippets = this.props.codeSnippets;
+ const filteredSnippetsScore: {
+ score: number;
+ snippet: ICodeSnippet;
+ }[] = [];
+ if (searchValue !== '') {
+ filteredSnippets.forEach(snippet => {
+ const matchResult = StringExt.matchSumOfSquares(
+ (snippet.language + snippet.name).toLowerCase(),
+ searchValue.replace(' ', '').toLowerCase()
+ );
+
+ if (matchResult) {
+ matchResults.push(matchResult);
+ filteredSnippetsScore.push({
+ score: matchResult.score,
+ snippet: snippet
+ });
+ }
+ });
+
+ // sort snippets by its score
+ filteredSnippetsScore.sort((a, b) => a.score - b.score);
+ const newFilteredSnippets: ICodeSnippet[] = [];
+ filteredSnippetsScore.forEach(snippetScore =>
+ newFilteredSnippets.push(snippetScore.snippet)
+ );
+ filteredSnippets = newFilteredSnippets;
+
+ // sort the matchResults by its score
+ matchResults.sort((a, b) => a.score - b.score);
+ matchResults.forEach(res => matchIndices.push(res.indices));
+ }
// filter with tags
if (filterTags.length !== 0) {
- filteredSnippets = filteredSnippets.filter(codeSnippet => {
+ const newMatchIndices = matchIndices.slice();
+ filteredSnippets = filteredSnippets.filter((codeSnippet, id) => {
return filterTags.some(tag => {
if (codeSnippet.tags) {
- return codeSnippet.tags.includes(tag);
+ if (codeSnippet.tags.includes(tag)) {
+ return true;
+ }
}
+ // if the snippet does not have the tag, remove its mathed index
+ const matchedIndex = matchIndices[id];
+ const indexToDelete = newMatchIndices.indexOf(matchedIndex);
+ newMatchIndices.splice(indexToDelete, 1);
return false;
});
});
+ matchIndices = newMatchIndices;
}
- this.setState({
- codeSnippets: filteredSnippets,
- searchValue: searchValue,
- filterTags: filterTags
- });
+ this.setState(
+ {
+ codeSnippets: filteredSnippets,
+ matchIndices: matchIndices,
+ searchValue: searchValue,
+ filterTags: filterTags
+ },
+ () => {
+ console.log('snippets filtered');
+ }
+ );
};
getActiveTags(): string[] {
@@ -1370,7 +1442,7 @@ export class CodeSnippetDisplay extends React.Component<
{this.state.codeSnippets.map((codeSnippet, id) =>
- this.renderCodeSnippet(codeSnippet, id.toString())
+ this.renderCodeSnippet(codeSnippet, id)
)}