settings highlight optimized
This commit is contained in:
@@ -249,7 +249,7 @@ VerticalFadeFlickable {
|
|||||||
elide: Text.ElideRight
|
elide: Text.ElideRight
|
||||||
font.pointSize: Appearance.font.size.medium
|
font.pointSize: Appearance.font.size.medium
|
||||||
text: SettingsSearcher.highlight(result.modelData.title, root.search, DynamicColors.palette.m3primary)
|
text: SettingsSearcher.highlight(result.modelData.title, root.search, DynamicColors.palette.m3primary)
|
||||||
textFormat: Text.StyledText
|
textFormat: text.includes("<font") ? Text.StyledText : Text.PlainText
|
||||||
}
|
}
|
||||||
|
|
||||||
CustomText {
|
CustomText {
|
||||||
@@ -258,7 +258,7 @@ VerticalFadeFlickable {
|
|||||||
elide: Text.ElideRight
|
elide: Text.ElideRight
|
||||||
font.pointSize: Appearance.font.size.small
|
font.pointSize: Appearance.font.size.small
|
||||||
text: SettingsSearcher.highlight(result.modelData.subtext, root.search, DynamicColors.palette.m3primary)
|
text: SettingsSearcher.highlight(result.modelData.subtext, root.search, DynamicColors.palette.m3primary)
|
||||||
textFormat: Text.StyledText
|
textFormat: text.includes("<font") ? Text.StyledText : Text.PlainText
|
||||||
visible: result.modelData.subtext.length > 0
|
visible: result.modelData.subtext.length > 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,16 +10,39 @@ Singleton {
|
|||||||
id: root
|
id: root
|
||||||
|
|
||||||
property var fzfFinder: null
|
property var fzfFinder: null
|
||||||
|
readonly property var highlightCache: ({
|
||||||
|
"search": "",
|
||||||
|
"pattern": null
|
||||||
|
})
|
||||||
property var inverted: ({})
|
property var inverted: ({})
|
||||||
property var ranking: ({})
|
property var ranking: ({})
|
||||||
|
|
||||||
function highlight(text: string, search: string, colour: color): string {
|
function highlight(text: string, search: string, colour: color): string {
|
||||||
const escaped = text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
const escaped = text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
||||||
const tokens = tokenize(search);
|
if (search.length === 0)
|
||||||
if (tokens.length === 0)
|
|
||||||
return escaped;
|
return escaped;
|
||||||
const escapedTokens = tokens.map(t => t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
|
|
||||||
const pattern = new RegExp("\\b(" + escapedTokens.join("|") + ")", "gi");
|
const cache = root.highlightCache;
|
||||||
|
if (search !== cache.search) {
|
||||||
|
const tokens = tokenize(search);
|
||||||
|
cache.search = search;
|
||||||
|
if (tokens.length === 0)
|
||||||
|
cache.pattern = null;
|
||||||
|
else {
|
||||||
|
const escapedTokens = tokens.map(t => t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
|
||||||
|
cache.pattern = new RegExp("\\b(" + escapedTokens.join("|") + ")", "gi");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const pattern = cache.pattern;
|
||||||
|
if (!pattern)
|
||||||
|
return escaped;
|
||||||
|
|
||||||
|
pattern.lastIndex = 0;
|
||||||
|
if (!pattern.test(escaped))
|
||||||
|
return escaped;
|
||||||
|
|
||||||
|
pattern.lastIndex = 0;
|
||||||
return escaped.replace(pattern, `<font color="${colour}">$1</font>`);
|
return escaped.replace(pattern, `<font color="${colour}">$1</font>`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user