Skip to content

Commit b7866cb

Browse files
committed
Fix bug with highlighting the matched characters
1 parent fa9c43d commit b7866cb

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

src/CodeSnippetDisplay.tsx

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ interface ICodeSnippetDisplayProps {
144144
*/
145145
interface ICodeSnippetDisplayState {
146146
codeSnippets: ICodeSnippet[];
147-
matchIndices: { [key: number]: number[] };
147+
matchIndices: number[][];
148148
searchValue: string;
149149
filterTags: string[];
150150
}
@@ -162,7 +162,7 @@ export class CodeSnippetDisplay extends React.Component<
162162
super(props);
163163
this.state = {
164164
codeSnippets: this.props.codeSnippets,
165-
matchIndices: {},
165+
matchIndices: [],
166166
searchValue: '',
167167
filterTags: []
168168
};
@@ -295,9 +295,15 @@ export class CodeSnippetDisplay extends React.Component<
295295
name: string
296296
): JSX.Element => {
297297
const displayName = language + name;
298-
299-
// check if this snippet is one of the filtered snippets
300-
if (this.state.searchValue !== '' && this.state.matchIndices[id] !== null) {
298+
console.log(displayName);
299+
console.log(this.state.searchValue);
300+
console.log(this.state.codeSnippets.length);
301+
console.log(id);
302+
console.log(this.state.matchIndices);
303+
// console.log(this.state.matchIndices[id]);
304+
305+
// check if the searchValue is not ''
306+
if (this.state.searchValue !== '') {
301307
const elements = [];
302308
const boldIndices = this.state.matchIndices[id].slice();
303309

@@ -335,8 +341,8 @@ export class CodeSnippetDisplay extends React.Component<
335341
// add the regular string until we reach the next bold index
336342
elements.push(displayName.substring(currIndex + 1, nextIndex));
337343
currIndex = nextIndex;
338-
i++;
339344
if (i < boldIndices.length - 1) {
345+
i++;
340346
nextIndex = boldIndices[i];
341347
} else {
342348
nextIndex = null;
@@ -352,6 +358,7 @@ export class CodeSnippetDisplay extends React.Component<
352358
displayName.substring(currIndex + 1, displayName.length)
353359
);
354360
}
361+
console.log(elements);
355362
return <span>{elements}</span>;
356363
}
357364
}
@@ -1181,26 +1188,26 @@ export class CodeSnippetDisplay extends React.Component<
11811188
if (state.searchValue === '' && state.filterTags.length === 0) {
11821189
return {
11831190
codeSnippets: props.codeSnippets,
1184-
matchIndices: {},
1191+
matchIndices: [],
11851192
searchValue: '',
11861193
filterTags: []
11871194
};
11881195
}
11891196

11901197
if (state.searchValue !== '' || state.filterTags.length !== 0) {
1191-
const newSnippets = props.codeSnippets.filter(codeSnippet => {
1192-
return (
1193-
state.matchIndices[codeSnippet.id] !== null ||
1194-
// (state.searchValue !== '' &&
1195-
// codeSnippet.name.toLowerCase().includes(state.searchValue)) ||
1196-
// (state.searchValue !== '' &&
1197-
// codeSnippet.language.toLowerCase().includes(state.searchValue)) ||
1198-
(codeSnippet.tags &&
1199-
codeSnippet.tags.some(tag => state.filterTags.includes(tag)))
1200-
);
1201-
});
1198+
// const newSnippets = props.codeSnippets.filter(codeSnippet => {
1199+
// return (
1200+
// state.matchIndices[codeSnippet.id] !== null ||
1201+
// // (state.searchValue !== '' &&
1202+
// // codeSnippet.name.toLowerCase().includes(state.searchValue)) ||
1203+
// // (state.searchValue !== '' &&
1204+
// // codeSnippet.language.toLowerCase().includes(state.searchValue)) ||
1205+
// (codeSnippet.tags &&
1206+
// codeSnippet.tags.some(tag => state.filterTags.includes(tag)))
1207+
// );
1208+
// });
12021209
return {
1203-
codeSnippets: newSnippets,
1210+
codeSnippets: state.codeSnippets,
12041211
matchIndices: state.matchIndices,
12051212
searchValue: state.searchValue,
12061213
filterTags: state.filterTags
@@ -1268,16 +1275,18 @@ export class CodeSnippetDisplay extends React.Component<
12681275

12691276
// TODO: filter codes nippet with regex!
12701277
// filter with search
1271-
const matchIndices: { [key: number]: number[] } = {};
1278+
const matchIndices: number[][] = [];
12721279
let filteredSnippets = this.props.codeSnippets;
12731280
if (searchValue !== '') {
12741281
filteredSnippets = filteredSnippets.filter(snippet => {
12751282
const indices = StringExt.findIndices(
12761283
(snippet.language + snippet.name).toLowerCase(),
12771284
searchValue.toLowerCase()
12781285
);
1279-
matchIndices[snippet.id] = indices;
1280-
return indices;
1286+
if (indices) {
1287+
matchIndices.push(indices);
1288+
}
1289+
return indices !== null;
12811290
});
12821291

12831292
// remove space
@@ -1342,7 +1351,6 @@ export class CodeSnippetDisplay extends React.Component<
13421351
console.log('snippets filtered');
13431352
}
13441353
);
1345-
console.log(this.state.codeSnippets);
13461354
};
13471355

13481356
getActiveTags(): string[] {

0 commit comments

Comments
 (0)