diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2020-02-04 16:44:53 +0100 |
---|---|---|
committer | Rigel Kent <sendmemail@rigelk.eu> | 2020-02-13 16:35:24 +0100 |
commit | 9b8a7aa8ea128f7e197ff38ca9f86ffa53bbe110 (patch) | |
tree | f38e6f83a9d892a99f930c0a25b1a405e679cd4a /client/src/app/shared/angular | |
parent | ece3029bd99a76b3c48a1cc8c58914c5cf61f106 (diff) | |
download | PeerTube-9b8a7aa8ea128f7e197ff38ca9f86ffa53bbe110.tar.gz PeerTube-9b8a7aa8ea128f7e197ff38ca9f86ffa53bbe110.tar.zst PeerTube-9b8a7aa8ea128f7e197ff38ca9f86ffa53bbe110.zip |
Improve search typeahead performance and use native events
Diffstat (limited to 'client/src/app/shared/angular')
-rw-r--r-- | client/src/app/shared/angular/highlight.pipe.ts | 74 |
1 files changed, 38 insertions, 36 deletions
diff --git a/client/src/app/shared/angular/highlight.pipe.ts b/client/src/app/shared/angular/highlight.pipe.ts index 4199d833e..e219b3823 100644 --- a/client/src/app/shared/angular/highlight.pipe.ts +++ b/client/src/app/shared/angular/highlight.pipe.ts | |||
@@ -5,48 +5,50 @@ import { SafeHtml } from '@angular/platform-browser' | |||
5 | @Pipe({ name: 'highlight' }) | 5 | @Pipe({ name: 'highlight' }) |
6 | export class HighlightPipe implements PipeTransform { | 6 | export class HighlightPipe implements PipeTransform { |
7 | /* use this for single match search */ | 7 | /* use this for single match search */ |
8 | static SINGLE_MATCH: string = "Single-Match" | 8 | static SINGLE_MATCH = 'Single-Match' |
9 | /* use this for single match search with a restriction that target should start with search string */ | 9 | /* use this for single match search with a restriction that target should start with search string */ |
10 | static SINGLE_AND_STARTS_WITH_MATCH: string = "Single-And-StartsWith-Match" | 10 | static SINGLE_AND_STARTS_WITH_MATCH = 'Single-And-StartsWith-Match' |
11 | /* use this for global search */ | 11 | /* use this for global search */ |
12 | static MULTI_MATCH: string = "Multi-Match" | 12 | static MULTI_MATCH = 'Multi-Match' |
13 | 13 | ||
14 | constructor() {} | 14 | // tslint:disable-next-line:no-empty |
15 | transform( | 15 | constructor () {} |
16 | |||
17 | transform ( | ||
16 | contentString: string = null, | 18 | contentString: string = null, |
17 | stringToHighlight: string = null, | 19 | stringToHighlight: string = null, |
18 | option: string = "Single-And-StartsWith-Match", | 20 | option = 'Single-And-StartsWith-Match', |
19 | caseSensitive: boolean = false, | 21 | caseSensitive = false, |
20 | highlightStyleName: string = "search-highlight" | 22 | highlightStyleName = 'search-highlight' |
21 | ): SafeHtml { | 23 | ): SafeHtml { |
22 | if (stringToHighlight && contentString && option) { | 24 | if (stringToHighlight && contentString && option) { |
23 | let regex: any = "" | 25 | let regex: any = '' |
24 | let caseFlag: string = !caseSensitive ? "i" : "" | 26 | const caseFlag: string = !caseSensitive ? 'i' : '' |
25 | switch (option) { | 27 | switch (option) { |
26 | case "Single-Match": { | 28 | case 'Single-Match': { |
27 | regex = new RegExp(stringToHighlight, caseFlag) | 29 | regex = new RegExp(stringToHighlight, caseFlag) |
28 | break | 30 | break |
29 | } | 31 | } |
30 | case "Single-And-StartsWith-Match": { | 32 | case 'Single-And-StartsWith-Match': { |
31 | regex = new RegExp("^" + stringToHighlight, caseFlag) | 33 | regex = new RegExp("^" + stringToHighlight, caseFlag) |
32 | break | 34 | break |
33 | } | 35 | } |
34 | case "Multi-Match": { | 36 | case 'Multi-Match': { |
35 | regex = new RegExp(stringToHighlight, "g" + caseFlag) | 37 | regex = new RegExp(stringToHighlight, 'g' + caseFlag) |
36 | break | 38 | break |
37 | } | 39 | } |
38 | default: { | 40 | default: { |
39 | // default will be a global case-insensitive match | 41 | // default will be a global case-insensitive match |
40 | regex = new RegExp(stringToHighlight, "gi") | 42 | regex = new RegExp(stringToHighlight, 'gi') |
41 | } | 43 | } |
42 | } | ||
43 | const replaced = contentString.replace( | ||
44 | regex, | ||
45 | (match) => `<span class="${highlightStyleName}">${match}</span>` | ||
46 | ) | ||
47 | return replaced | ||
48 | } else { | ||
49 | return contentString | ||
50 | } | 44 | } |
45 | const replaced = contentString.replace( | ||
46 | regex, | ||
47 | (match) => `<span class="${highlightStyleName}">${match}</span>` | ||
48 | ) | ||
49 | return replaced | ||
50 | } else { | ||
51 | return contentString | ||
52 | } | ||
51 | } | 53 | } |
52 | } | 54 | } |