-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
OlgaLarina
committed
Jan 24, 2024
1 parent
9dc7660
commit d5fb9f0
Showing
13 changed files
with
529 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { InputMaskBase } from "./mask_base"; | ||
import { ITextMaskInputArgs } from "./mask_manager"; | ||
|
||
export class InputElementAdapter { | ||
constructor(private mask: InputMaskBase, private inputElement: HTMLInputElement, value: string = "") { | ||
this.inputElement.value = mask.getMaskedValue(value, true); | ||
this.addInputEventListener(); | ||
} | ||
|
||
beforeInputHandler = (event: any) => { | ||
const args = this.createArgs(event); | ||
const result = this.mask.processInput(args); | ||
this.inputElement.value = result.text; | ||
this.inputElement.setSelectionRange(result.cursorPosition, result.cursorPosition); | ||
if(!result.cancelPreventDefault) { | ||
event.preventDefault(); | ||
} | ||
}; | ||
|
||
public createArgs(event: any): ITextMaskInputArgs { | ||
const args: ITextMaskInputArgs = { | ||
insertedCharacters: event.data, | ||
selectionStart: event.target.selectionStart, | ||
selectionEnd: event.target.selectionEnd, | ||
prevValue: event.target.value, | ||
inputDirection: "leftToRight" | ||
}; | ||
switch (event.inputType) { | ||
case "deleteContentBackward": { | ||
args.selectionStart = Math.max(args.selectionStart - 1, 0); | ||
args.inputDirection = "rightToLeft"; | ||
break; | ||
} | ||
case "deleteContentForward": { | ||
args.selectionEnd += 1; | ||
break; | ||
} | ||
} | ||
return args; | ||
} | ||
public addInputEventListener(): void { | ||
if (!!this.inputElement) { | ||
this.inputElement.addEventListener("beforeinput", this.beforeInputHandler); | ||
} | ||
} | ||
public removeInputEventListener(): void { | ||
if (!!this.inputElement) { | ||
this.inputElement.removeEventListener("beforeinput", this.beforeInputHandler); | ||
} | ||
} | ||
public dispose(): void { | ||
this.removeInputEventListener(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { IInputMaskType, IMaskOption, IMaskedValue, ITextMaskInputArgs } from "./mask_manager"; | ||
|
||
export class InputMaskBase implements IInputMaskType { | ||
protected prevSelectionStart: number; | ||
protected prevInputValue: string; | ||
|
||
constructor(protected maskOptions: IMaskOption) { } | ||
|
||
// public get maskedInputValue(): string { | ||
// return this.input.value; | ||
// } | ||
// public get unmaskedInputValue(): string { | ||
// return this.getUnmaskedValue(this.input.value, true); | ||
// } | ||
|
||
public processInput(args: ITextMaskInputArgs): IMaskedValue { | ||
const result = { text: args.prevValue, cursorPosition: args.selectionEnd, cancelPreventDefault: false }; | ||
|
||
const leftPart = args.prevValue.slice(0, args.selectionStart) + (args.insertedCharacters || ""); | ||
if(!args.insertedCharacters && args.inputDirection === "rightToLeft") { | ||
result.cursorPosition = args.selectionStart; | ||
} else { | ||
result.cursorPosition = this.getMaskedValue(leftPart).length; | ||
} | ||
|
||
const src = leftPart + args.prevValue.slice(args.selectionEnd); | ||
result.text = this.getMaskedValue(src, true); | ||
return result; | ||
} | ||
|
||
public getUnmaskedValue(src: string, matchWholeMask: boolean = false): string { return src; } | ||
public getMaskedValue(src: string, matchWholeMask: boolean = false): string { return src; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { HashTable } from "../helpers"; | ||
import { InputMaskBase } from "./mask_base"; | ||
|
||
export interface IMaskOption { | ||
type: string; | ||
mask: string; | ||
} | ||
|
||
export interface IMaskedValue { | ||
text: string; | ||
cursorPosition: number; | ||
cancelPreventDefault: boolean; | ||
} | ||
|
||
export interface ITextMaskInputArgs { | ||
prevValue: string; | ||
selectionStart: number; | ||
selectionEnd: number; | ||
insertedCharacters: string | null; | ||
inputDirection?: "leftToRight" | "rightToLeft"; | ||
} | ||
|
||
export interface IInputMaskType { | ||
getMaskedValue(src: string, matchWholeMask?: boolean): string; | ||
getUnmaskedValue(src: string, matchWholeMask?: boolean): string; | ||
processInput(args: ITextMaskInputArgs): IMaskedValue; | ||
} | ||
|
||
export class MaskManagerType { | ||
public static Instance: MaskManagerType = new MaskManagerType(); | ||
private creatorHash: HashTable<(maskOption: IMaskOption) => IInputMaskType> = {}; | ||
|
||
public registerMaskManagerType(maskType: string, maskCreator: (maskOption: IMaskOption) => IInputMaskType): void { | ||
this.creatorHash[maskType] = maskCreator; | ||
} | ||
|
||
public createInputMask(maskOption: IMaskOption): IInputMaskType { | ||
const creator = MaskManagerType.Instance.creatorHash[maskOption.type]; | ||
if (!!creator) return creator(maskOption); | ||
|
||
return new InputMaskBase(maskOption); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.