@hmcts/opal-frontend-common 0.0.111 → 0.0.112
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DOCUMENT } from "@angular/common";
|
|
1
2
|
import * as i0 from "@angular/core";
|
|
2
3
|
import { Directive, Input, inject } from "@angular/core";
|
|
3
4
|
import { Subject, takeUntil } from "rxjs";
|
|
@@ -5,15 +6,34 @@ import { UtilsService } from "@hmcts/opal-frontend-common/services/utils-service
|
|
|
5
6
|
var CapitalisationDirective = class CapitalisationDirective {
|
|
6
7
|
destroy$ = new Subject();
|
|
7
8
|
utilsService = inject(UtilsService);
|
|
9
|
+
document = inject(DOCUMENT);
|
|
8
10
|
control;
|
|
11
|
+
captureSelection(value) {
|
|
12
|
+
const activeElement = this.document.activeElement;
|
|
13
|
+
if (activeElement?.tagName !== "INPUT" && activeElement?.tagName !== "TEXTAREA") return null;
|
|
14
|
+
const input = activeElement;
|
|
15
|
+
const { selectionStart, selectionEnd, selectionDirection } = input;
|
|
16
|
+
if (input.value !== value || typeof selectionStart !== "number" || typeof selectionEnd !== "number") return null;
|
|
17
|
+
return {
|
|
18
|
+
input,
|
|
19
|
+
start: this.utilsService.upperCaseAllLetters(value.slice(0, selectionStart)).length,
|
|
20
|
+
end: this.utilsService.upperCaseAllLetters(value.slice(0, selectionEnd)).length,
|
|
21
|
+
direction: selectionDirection ?? "none"
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
capitaliseControlValue(value) {
|
|
25
|
+
if (typeof value === "string" && value.length > 0) {
|
|
26
|
+
const upper = this.utilsService.upperCaseAllLetters(value);
|
|
27
|
+
if (value !== upper) {
|
|
28
|
+
const selection = this.captureSelection(value);
|
|
29
|
+
this.control.setValue(upper, { emitEvent: false });
|
|
30
|
+
if (selection?.input === this.document.activeElement && selection.input.value === upper) selection.input.setSelectionRange(selection.start, selection.end, selection.direction);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
9
34
|
ngOnInit() {
|
|
10
35
|
if (!this.control) return;
|
|
11
|
-
this.control.valueChanges.pipe(takeUntil(this.destroy$)).subscribe((value) =>
|
|
12
|
-
if (typeof value === "string" && value.length > 0) {
|
|
13
|
-
const upper = this.utilsService.upperCaseAllLetters(value);
|
|
14
|
-
if (value !== upper) this.control.setValue(upper, { emitEvent: false });
|
|
15
|
-
}
|
|
16
|
-
});
|
|
36
|
+
this.control.valueChanges.pipe(takeUntil(this.destroy$)).subscribe((value) => this.capitaliseControlValue(value));
|
|
17
37
|
}
|
|
18
38
|
ngOnDestroy() {
|
|
19
39
|
this.destroy$.next();
|
package/package.json
CHANGED
|
@@ -4,7 +4,23 @@ import { AbstractControl } from "@angular/forms";
|
|
|
4
4
|
export declare class CapitalisationDirective implements OnInit, OnDestroy {
|
|
5
5
|
private readonly destroy$;
|
|
6
6
|
private readonly utilsService;
|
|
7
|
+
private readonly document;
|
|
7
8
|
control: AbstractControl;
|
|
9
|
+
/**
|
|
10
|
+
* Captures the focused input or textarea selection before its value is uppercased.
|
|
11
|
+
* Maps offsets through uppercase prefixes to account for expanding characters, such as ß becoming SS.
|
|
12
|
+
* Defaults missing selection direction to 'none' for document implementations with incomplete selection support.
|
|
13
|
+
*
|
|
14
|
+
* @param value The current control value, which must match the focused input's value.
|
|
15
|
+
* @returns The input and its mapped selection, or null when the value differs or selection is unsupported.
|
|
16
|
+
*/
|
|
17
|
+
private captureSelection;
|
|
18
|
+
/**
|
|
19
|
+
* Uppercases a changed control value while preserving the focused input's selection.
|
|
20
|
+
*
|
|
21
|
+
* @param value The latest value emitted by the form control.
|
|
22
|
+
*/
|
|
23
|
+
private capitaliseControlValue;
|
|
8
24
|
ngOnInit(): void;
|
|
9
25
|
ngOnDestroy(): void;
|
|
10
26
|
static ɵfac: i0.ɵɵFactoryDeclaration<CapitalisationDirective, never>;
|