(core) Fixing ACIndex highlightMatches functions

Summary:
Highlighting wasn't working correctly for the new
normalized search for autocomplate widgets.

Test Plan: Existing tests

Reviewers: alexmojaki

Reviewed By: alexmojaki

Differential Revision: https://phab.getgrist.com/D3602
This commit is contained in:
Jarosław Sadziński
2022-08-26 08:05:05 +02:00
parent 2cb783ea7b
commit b6f5718ad0
3 changed files with 55 additions and 28 deletions

View File

@@ -11,6 +11,7 @@ import {localeCompare, nativeCompare, sortedIndex} from 'app/common/gutil';
import {DomContents} from 'grainjs';
import escapeRegExp = require("lodash/escapeRegExp");
import deburr = require("lodash/deburr");
import split = require("lodash/split");
export interface ACItem {
// This should be a trimmed lowercase version of the item's text. It may be an accessor.
@@ -243,11 +244,17 @@ function highlightMatches(searchWords: string[], text: string): string[] {
for (let i = 0; i < textParts.length; i += 2) {
const word = textParts[i];
const separator = textParts[i + 1] || '';
const prefixLen = findLongestPrefixLen(word.toLowerCase(), searchWords);
// deburr (remove diacritics) was used to produce searchWords, so `word` needs to match that.
const prefixLen = findLongestPrefixLen(deburr(word).toLowerCase(), searchWords);
if (prefixLen === 0) {
outputs[outputs.length - 1] += word + separator;
} else {
outputs.push(word.slice(0, prefixLen), word.slice(prefixLen) + separator);
// Split into unicode 'characters' that keep diacritics combined
const chars = split(word, '');
outputs.push(
chars.slice(0, prefixLen).join(''),
chars.slice(prefixLen).join('') + separator
);
}
}
return outputs;