search: Split out the description highlighter into its own class
No functional change yet, only preparation to allow adding a unit test later on. Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2033>
This commit is contained in:
parent
9069183cec
commit
5e0c842429
@ -2,7 +2,7 @@
|
|||||||
/* exported findUrls, spawn, spawnCommandLine, spawnApp, trySpawnCommandLine,
|
/* exported findUrls, spawn, spawnCommandLine, spawnApp, trySpawnCommandLine,
|
||||||
formatTime, formatTimeSpan, createTimeLabel, insertSorted,
|
formatTime, formatTimeSpan, createTimeLabel, insertSorted,
|
||||||
ensureActorVisibleInScrollView, wiggle, lerp, GNOMEversionCompare,
|
ensureActorVisibleInScrollView, wiggle, lerp, GNOMEversionCompare,
|
||||||
DBusSenderChecker */
|
DBusSenderChecker, Highlighter */
|
||||||
|
|
||||||
const { Clutter, Gio, GLib, Shell, St, GnomeDesktop } = imports.gi;
|
const { Clutter, Gio, GLib, Shell, St, GnomeDesktop } = imports.gi;
|
||||||
const Gettext = imports.gettext;
|
const Gettext = imports.gettext;
|
||||||
@ -555,3 +555,38 @@ var DBusSenderChecker = class {
|
|||||||
this._watchList = [];
|
this._watchList = [];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* @class Highlighter Highlight given terms in text using markup. */
|
||||||
|
var Highlighter = class {
|
||||||
|
/**
|
||||||
|
* @param {?string[]} terms - list of terms to highlight
|
||||||
|
*/
|
||||||
|
constructor(terms) {
|
||||||
|
if (!terms)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const escapedTerms = terms
|
||||||
|
.map(term => Shell.util_regex_escape(term))
|
||||||
|
.filter(term => term.length > 0);
|
||||||
|
|
||||||
|
if (escapedTerms.length === 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
this._highlightRegex = new RegExp('(%s)'.format(
|
||||||
|
escapedTerms.join('|')), 'gi');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Highlight all occurences of the terms defined for this
|
||||||
|
* highlighter in the provided text using markup.
|
||||||
|
*
|
||||||
|
* @param {string} text - text to highlight the defined terms in
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
highlight(text) {
|
||||||
|
if (!this._highlightRegex)
|
||||||
|
return text;
|
||||||
|
|
||||||
|
return text.replace(this._highlightRegex, '<b>$1</b>');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
@ -10,6 +10,8 @@ const ParentalControlsManager = imports.misc.parentalControlsManager;
|
|||||||
const RemoteSearch = imports.ui.remoteSearch;
|
const RemoteSearch = imports.ui.remoteSearch;
|
||||||
const Util = imports.misc.util;
|
const Util = imports.misc.util;
|
||||||
|
|
||||||
|
const { Highlighter } = imports.misc.util;
|
||||||
|
|
||||||
const SEARCH_PROVIDERS_SCHEMA = 'org.gnome.desktop.search-providers';
|
const SEARCH_PROVIDERS_SCHEMA = 'org.gnome.desktop.search-providers';
|
||||||
|
|
||||||
var MAX_LIST_SEARCH_RESULTS_ROWS = 5;
|
var MAX_LIST_SEARCH_RESULTS_ROWS = 5;
|
||||||
@ -596,7 +598,7 @@ var SearchResultsView = GObject.registerClass({
|
|||||||
|
|
||||||
this._providers = [];
|
this._providers = [];
|
||||||
|
|
||||||
this._highlightRegex = null;
|
this._highlighter = new Highlighter();
|
||||||
|
|
||||||
this._searchSettings = new Gio.Settings({ schema_id: SEARCH_PROVIDERS_SCHEMA });
|
this._searchSettings = new Gio.Settings({ schema_id: SEARCH_PROVIDERS_SCHEMA });
|
||||||
this._searchSettings.connect('changed::disabled', this._reloadRemoteProviders.bind(this));
|
this._searchSettings.connect('changed::disabled', this._reloadRemoteProviders.bind(this));
|
||||||
@ -739,8 +741,7 @@ var SearchResultsView = GObject.registerClass({
|
|||||||
if (this._searchTimeoutId == 0)
|
if (this._searchTimeoutId == 0)
|
||||||
this._searchTimeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 150, this._onSearchTimeout.bind(this));
|
this._searchTimeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 150, this._onSearchTimeout.bind(this));
|
||||||
|
|
||||||
let escapedTerms = this._terms.map(term => Shell.util_regex_escape(term));
|
this._highlighter = new Highlighter(this._terms);
|
||||||
this._highlightRegex = new RegExp('(%s)'.format(escapedTerms.join('|')), 'gi');
|
|
||||||
|
|
||||||
this.emit('terms-changed');
|
this.emit('terms-changed');
|
||||||
}
|
}
|
||||||
@ -894,10 +895,7 @@ var SearchResultsView = GObject.registerClass({
|
|||||||
if (!description)
|
if (!description)
|
||||||
return '';
|
return '';
|
||||||
|
|
||||||
if (!this._highlightRegex)
|
return this._highlighter.highlight(description);
|
||||||
return description;
|
|
||||||
|
|
||||||
return description.replace(this._highlightRegex, '<b>$1</b>');
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user