fix(core): unexpected line breaks (#6019)

This commit is contained in:
JimmFly
2024-03-05 10:02:21 +00:00
parent ceec54b423
commit c1afdb9bd6

View File

@@ -1,43 +1,44 @@
import { useMemo } from 'react'; import { useMemo } from 'react';
function* highlightTextFragmentsGenerator(text: string, query: string) { function* highlightTextFragmentsGenerator(text: string, query: string) {
const lowerCaseText = text.replace(/\r?\n|\r/g, '').toLowerCase(); const cleanedText = text.replace(/\r?\n|\r|\t/g, '');
const lowerCaseText = cleanedText.toLowerCase();
query = query.toLowerCase(); query = query.toLowerCase();
let startIndex = lowerCaseText.indexOf(query); let startIndex = lowerCaseText.indexOf(query);
if (startIndex !== -1) { if (startIndex !== -1) {
if (startIndex > 0) { if (startIndex > 0) {
yield { text: text.substring(0, startIndex), highlight: false }; yield { text: cleanedText.substring(0, startIndex), highlight: false };
} }
yield { yield {
text: text.substring(startIndex, startIndex + query.length), text: cleanedText.substring(startIndex, startIndex + query.length),
highlight: true, highlight: true,
}; };
if (startIndex + query.length < text.length) { if (startIndex + query.length < cleanedText.length) {
yield { yield {
text: text.substring(startIndex + query.length), text: cleanedText.substring(startIndex + query.length),
highlight: false, highlight: false,
}; };
} }
} else { } else {
startIndex = 0; startIndex = 0;
for (const char of query) { for (const char of query) {
const pos = text.toLowerCase().indexOf(char, startIndex); const pos = cleanedText.toLowerCase().indexOf(char, startIndex);
if (pos !== -1) { if (pos !== -1) {
if (pos > startIndex) { if (pos > startIndex) {
yield { yield {
text: text.substring(startIndex, pos), text: cleanedText.substring(startIndex, pos),
highlight: false, highlight: false,
}; };
} }
yield { text: text.substring(pos, pos + 1), highlight: true }; yield { text: cleanedText.substring(pos, pos + 1), highlight: true };
startIndex = pos + 1; startIndex = pos + 1;
} }
} }
if (startIndex < text.length) { if (startIndex < cleanedText.length) {
yield { text: text.substring(startIndex), highlight: false }; yield { text: cleanedText.substring(startIndex), highlight: false };
} }
} }
} }