This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
highlight.ts
207 lines (184 loc) · 6.25 KB
/
highlight.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* @license
* Copyright 2020 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { DOCUMENT } from '@angular/common';
import {
AfterContentChecked,
AfterViewInit,
ChangeDetectionStrategy,
Component,
ElementRef,
Inject,
Input,
NgZone,
OnChanges,
OnDestroy,
Optional,
ViewChild,
ViewEncapsulation,
} from '@angular/core';
import { Subscription } from 'rxjs';
import { take } from 'rxjs/operators';
import {
_addCssClass,
createInViewportStream,
} from '@dynatrace/barista-components/core';
const HIGHLIGHTED_CLASS = 'dt-highlight-mark';
const HIGHLIGHTED_ELEMENT = 'mark';
const HTML_CHARS_OVERRIDES: [string, string][] = [
['<', '<'],
['>', '>'],
['&', '&'],
[' ', ' '],
];
/**
* Helper function to remove all children of a parent node.
* This can be every dom element.
*/
function removeNodeChildren(parent: Element): void {
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
}
/** Escapes all characters that could be dangerous for a regular expression */
function escapeRegExp(text: string): string {
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
@Component({
selector: 'dt-highlight, [dt-highlight]',
exportAs: 'dtHighlight',
host: {
class: 'dt-highlight',
},
templateUrl: 'highlight.html',
styleUrls: ['highlight.scss'],
preserveWhitespaces: false,
encapsulation: ViewEncapsulation.Emulated,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DtHighlight
implements AfterContentChecked, AfterViewInit, OnChanges, OnDestroy {
/**
* The caseSensitive input can be set to search for case sensitive occurrences.
* Per default the search is case insensitive.
*/
@Input()
get caseSensitive(): boolean {
return this._caseSensitive;
}
set caseSensitive(sensitive: boolean) {
this._caseSensitive = coerceBooleanProperty(sensitive);
}
private _caseSensitive = false;
/**
* The term is the string that should be highlighted in the projected content.
* Every occurrence of the string is going to be highlighted.
*/
@Input() term = '';
/** @internal */
@ViewChild('source', { static: true })
_sourceElement: ElementRef<HTMLElement>;
/** @internal */
@ViewChild('transformed', { static: true })
_transformedElement: ElementRef<HTMLElement>;
private _textContent: string | null = null;
private _isInViewport = false;
private _isInViewportSubscription = Subscription.EMPTY;
constructor(
private _zone: NgZone,
private _elementRef: ElementRef,
// tslint:disable-next-line: no-any
@Optional() @Inject(DOCUMENT) private _document?: any,
) {}
/** Highlight if either the term or the caseSensitive input changes. */
ngOnChanges(): void {
this._highlight();
}
/** Highlight if the content of the highlight component changes. */
ngAfterContentChecked(): void {
this._zone.onStable.pipe(take(1)).subscribe(() => {
// check if the text has actually changed, if not skip the highlight.
const textContent = this._getTextContent();
if (textContent !== this._textContent) {
this._textContent = textContent;
this._highlight();
}
});
}
ngAfterViewInit(): void {
// Observable whether the component is in the viewport.
this._isInViewportSubscription = createInViewportStream(
this._elementRef,
0,
).subscribe((value) => {
this._isInViewport = value;
if (value) {
this._highlight();
}
});
}
ngOnDestroy(): void {
this._isInViewportSubscription.unsubscribe();
}
/** Get the textContent of the highlight component. */
private _getTextContent(): string | null {
// check if we are in a browser context
if (this._document) {
let text = this._sourceElement.nativeElement.innerHTML.trim();
for (const [needle, replacement] of HTML_CHARS_OVERRIDES) {
text = text.replace(new RegExp(needle, 'g'), replacement);
}
return text.length ? text : null;
}
return null;
}
/** The highlight function triggers the highlighting process if we are in a browser context. */
private _highlight(): void {
// Make sure to only update the highlight if it is in the viewport.
// This is needed because the highlight component can possibly be
// rendered a lot if it is used for example in a big data table or autocomplete.
if (!this._isInViewport || !this._document) {
return;
}
// As we create the highlight nodes with browser native functions we do not depend on Angular's CD.
// So we can run this code outside the zone to boost performance.
this._zone.runOutsideAngular(() => {
const textContent = this._textContent;
const term = this.term;
const transformedEl = this._transformedElement.nativeElement;
// Remove the old nodes.
removeNodeChildren(transformedEl);
if (textContent !== null && Boolean(term && term.length)) {
const flags = this._caseSensitive ? 'gm' : 'gmi';
const regExp = new RegExp(`(${escapeRegExp(term)})`, flags);
const textTokens = textContent.split(regExp).filter((s) => s.length);
for (const token of textTokens) {
const text = this._document.createTextNode(token);
if (token.toLowerCase() === term.toLowerCase()) {
const span = this._document.createElement(HIGHLIGHTED_ELEMENT);
_addCssClass(span, HIGHLIGHTED_CLASS);
span.appendChild(text);
transformedEl.appendChild(span);
} else {
transformedEl.appendChild(text);
}
}
} else if (textContent !== null) {
transformedEl.appendChild(this._document.createTextNode(textContent));
}
});
}
}