/
mention-trigger.ts
139 lines (122 loc) · 4.35 KB
/
mention-trigger.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
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
import {
AfterViewInit,
ChangeDetectorRef,
Directive,
ElementRef,
EventEmitter,
ExistingProvider,
forwardRef,
NgZone,
OnDestroy,
Output
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { fromEvent } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { NzDestroyService } from 'ng-zorro-antd/core/services';
import { OnChangeType, OnTouchedType } from 'ng-zorro-antd/core/types';
import { NZ_MENTION_CONFIG } from './config';
import { Mention } from './mention.component';
import { NzMentionService } from './mention.service';
export const NZ_MENTION_TRIGGER_ACCESSOR: ExistingProvider = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => NzMentionTriggerDirective),
multi: true
};
@Directive({
selector: 'input[nzMentionTrigger], textarea[nzMentionTrigger]',
exportAs: 'nzMentionTrigger',
providers: [NzDestroyService, NZ_MENTION_TRIGGER_ACCESSOR],
host: {
autocomplete: 'off'
}
})
export class NzMentionTriggerDirective implements ControlValueAccessor, OnDestroy, AfterViewInit {
onChange: OnChangeType = () => {};
onTouched: OnTouchedType = () => {};
// eslint-disable-next-line @angular-eslint/no-output-on-prefix
@Output() readonly onFocusin: EventEmitter<void> = new EventEmitter();
// eslint-disable-next-line @angular-eslint/no-output-on-prefix
@Output() readonly onBlur: EventEmitter<void> = new EventEmitter();
// eslint-disable-next-line @angular-eslint/no-output-on-prefix
@Output() readonly onInput: EventEmitter<KeyboardEvent> = new EventEmitter();
// eslint-disable-next-line @angular-eslint/no-output-on-prefix
@Output() readonly onKeydown: EventEmitter<KeyboardEvent> = new EventEmitter();
// eslint-disable-next-line @angular-eslint/no-output-on-prefix
@Output() readonly onClick: EventEmitter<MouseEvent> = new EventEmitter();
value?: string;
constructor(
public el: ElementRef<HTMLInputElement | HTMLTextAreaElement>,
private ngZone: NgZone,
private ref: ChangeDetectorRef,
private destroy$: NzDestroyService,
private nzMentionService: NzMentionService
) {}
completeEvents(): void {
this.onFocusin.complete();
this.onBlur.complete();
this.onInput.complete();
this.onKeydown.complete();
this.onClick.complete();
}
focus(caretPos: number | null = null): void {
this.el.nativeElement.focus();
this.el.nativeElement.setSelectionRange(caretPos, caretPos);
}
insertMention(mention: Mention): void {
const value: string = this.el.nativeElement.value;
const insertValue = `${mention.mention}${NZ_MENTION_CONFIG.split}`;
const newValue = [
value.slice(0, mention.startPos + 1),
insertValue,
value.slice(mention.endPos, value.length)
].join('');
this.el.nativeElement.value = newValue;
this.focus(mention.startPos + insertValue.length + 1);
this.onChange(newValue);
this.value = newValue;
}
writeValue(value: string): void {
this.value = value;
if (typeof value === 'string') {
this.el.nativeElement.value = value;
} else {
this.el.nativeElement.value = '';
}
}
registerOnChange(fn: (value: string) => void): void {
this.onChange = fn;
}
registerOnTouched(fn: () => void): void {
this.onTouched = fn;
}
ngAfterViewInit(): void {
this.nzMentionService.registerTrigger(this);
this.setupEventListener('blur', this.onBlur);
this.setupEventListener('focusin', this.onFocusin);
this.setupEventListener('input', this.onInput, true);
this.setupEventListener('click', this.onClick, true);
this.setupEventListener('keydown', this.onKeydown, true);
}
ngOnDestroy(): void {
this.completeEvents();
}
private setupEventListener<T>(eventName: string, eventEmitter: EventEmitter<T>, shouldPassEvent = false): void {
this.ngZone.runOutsideAngular(() => {
fromEvent<T>(this.el.nativeElement, eventName)
.pipe(takeUntil(this.destroy$))
.subscribe(event => {
if (eventEmitter.observers.length) {
this.ngZone.run(() => {
eventEmitter.emit(shouldPassEvent ? event : undefined);
this.ref.markForCheck();
});
}
});
});
}
}