Angular 管道高亮
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Pipe({
name: 'matTextlight'
})
export class MatTextLightPipe implements PipeTransform {
constructor(public _sanitizer: DomSanitizer) {}
/** 通过正则匹配替换方式。(如果采用这种方式请先处理特殊字符) **/
_stringToHtml(val: string, regStr: string, color?: string, bgcolor?: string): string {
if (!val || !regStr) {
return val;
}
return val.replace(new RegExp(regStr, 'gi'), `<mark class="mat-text-light" style="color: ${color || ''};background-color: ${bgcolor || ''}">${regStr}</mark>`);
}
/** 通过字符串分割替换方式。 **/
_stringToHtml(val: string, regStr: string, color?: string, bgcolor?: string): string {
if (!val || !regStr) {
return val;
}
return val.split(regStr.trim())
.join(`<mark class="mat-text-light" style="color: ${color || ''};background-color: ${bgcolor || ''}">${regStr}</mark>`);
}
transform(val: string, regStr: string, color?: string, bgcolor?: string): any {
return this._sanitizer
.bypassSecurityTrustHtml(this._stringToHtml(val, regStr, color, bgcolor));
}
}
使用管道:
<!-- text : 文本 -->.
<!-- pipeSearchText : 要搜索的关键字 -->
<!-- color : 自定义高亮文字颜色 -->
<!-- pipeSearchText : 自定义高亮文字背景颜色 -->
<p [innerHtml]="text | matTextlight: pipeSearchText:color:bgcolor"> </p>
Angular 指令
import { Directive, Input, ElementRef, OnDestroy, OnInit } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { MatTextLightPipe } from './textlight-pipe';
import { Subject, Subscription } from 'rxjs';
import { debounceTime, distinctUntilChanged } from 'rxjs/operators';
export const MAT_TEXT_LIGHT_DEFAULT_DEBOUNCETIME = 300;
@Directive({
selector: '[matTextlight]',
exportAs: 'matTextlight',
})
export class MatTextLightDirective extends MatTextLightPipe implements OnDestroy, OnInit {
/** Search change subject */
private _search$ = new Subject();
/** Search subscription */
private _subscription: Subscription;
constructor(public _sanitizer: DomSanitizer, private _el: ElementRef) {
super(_sanitizer);
}
private _color: string;
@Input('matTextlightColor')
set color(value: string) {
this._color = value;
}
get color(): string {
return this._color;
}
private _bgcolor: string;
@Input('matTextlightBgcolor')
set bgcolor(value: string) {
this._bgcolor = value;
}
get bgcolor(): string {
return this._bgcolor;
}
private _content: string;
@Input('matTextlight')
set content(value: string) {
this._content = value;
}
get content(): string {
return this._content;
}
private _search: string;
@Input('matTextlightSearch')
set search(value: string) {
this._search = value;
this._search$.next(this._search);
}
get search(): string {
return this._search;
}
private _highlight(value: string) {
this._el.nativeElement.innerHTML = this._stringToHtml(this.content, value, this.color, this.bgcolor);
}
ngOnInit() {
this._subscription = this._search$.pipe(
debounceTime(MAT_TEXT_LIGHT_DEFAULT_DEBOUNCETIME),
distinctUntilChanged()
).subscribe((value: string) => this._highlight(value));
this._search$.next('');
}
ngOnDestroy() {
if (this._subscription) {
this._subscription.unsubscribe();
}
}
}
使用指令:
<!-- matTextlight : 文本 -->.
<!-- matTextlightSearch : 要搜索的关键字 -->
<!-- matTextlightColor : 自定义高亮文字颜色 -->
<!-- matTextlightBgcolor : 自定义高亮文字背景颜色 -->
<p [matTextlight]="text" [matTextlightSearch]="directiveSearch"> </p>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。