|
|
@@ -5,10 +5,17 @@ import { Directive, ElementRef, HostListener } from '@angular/core';
|
|
5
|
5
|
standalone: true
|
|
6
|
6
|
})
|
|
7
|
7
|
export class EnterNavigationDirective {
|
|
|
8
|
+ private isComposing = false;
|
|
|
9
|
+
|
|
8
|
10
|
constructor(private elementRef: ElementRef<HTMLElement>) {}
|
|
9
|
11
|
|
|
10
|
12
|
@HostListener('keydown.enter', ['$event'])
|
|
11
|
13
|
onEnter(event: KeyboardEvent): void {
|
|
|
14
|
+ // 跳过IME组合输入状态下的回车(让IME完成输入确认)
|
|
|
15
|
+ if (event.isComposing || this.isComposing) {
|
|
|
16
|
+ return;
|
|
|
17
|
+ }
|
|
|
18
|
+
|
|
12
|
19
|
// 阻止默认的提交行为,保持现有表单提交逻辑
|
|
13
|
20
|
event.preventDefault();
|
|
14
|
21
|
|
|
|
@@ -33,6 +40,32 @@ export class EnterNavigationDirective {
|
|
33
|
40
|
}
|
|
34
|
41
|
}
|
|
35
|
42
|
|
|
|
43
|
+ @HostListener('focus')
|
|
|
44
|
+ onFocus(): void {
|
|
|
45
|
+ const element = this.elementRef.nativeElement;
|
|
|
46
|
+
|
|
|
47
|
+ // 只处理input元素(排除textarea、select等)
|
|
|
48
|
+ if (element.tagName !== 'INPUT') return;
|
|
|
49
|
+
|
|
|
50
|
+ const inputElement = element as HTMLInputElement;
|
|
|
51
|
+
|
|
|
52
|
+ // 排除只读字段和禁用字段
|
|
|
53
|
+ if (inputElement.readOnly || inputElement.disabled) return;
|
|
|
54
|
+
|
|
|
55
|
+ // 使用setTimeout确保在Angular变更检测后执行
|
|
|
56
|
+ setTimeout(() => inputElement.select(), 0);
|
|
|
57
|
+ }
|
|
|
58
|
+
|
|
|
59
|
+ @HostListener('compositionstart')
|
|
|
60
|
+ onCompositionStart(): void {
|
|
|
61
|
+ this.isComposing = true;
|
|
|
62
|
+ }
|
|
|
63
|
+
|
|
|
64
|
+ @HostListener('compositionend')
|
|
|
65
|
+ onCompositionEnd(): void {
|
|
|
66
|
+ this.isComposing = false;
|
|
|
67
|
+ }
|
|
|
68
|
+
|
|
36
|
69
|
private isTextArea(): boolean {
|
|
37
|
70
|
return this.elementRef.nativeElement.tagName === 'TEXTAREA';
|
|
38
|
71
|
}
|