generated from Tinkoff/angular-open-source-starter
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): turn
MaskitoContentEditable
class into `maskitoAdap…
…tContentEditable` utility
- Loading branch information
1 parent
0f6783f
commit e682dba
Showing
6 changed files
with
54 additions
and
63 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 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 |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export {MaskitoContentEditable} from './content-editable'; | ||
export {MaskHistory} from './mask-history'; | ||
export {MaskModel} from './mask-model/mask-model'; |
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import {MaskitoContentEditable} from '../classes'; | ||
import type {MaskitoElement, MaskitoElementPredicate} from '../types'; | ||
import type {MaskitoElementPredicate} from '../types'; | ||
import {maskitoAdaptContentEditable} from '../utils'; | ||
|
||
export const MASKITO_DEFAULT_ELEMENT_PREDICATE: MaskitoElementPredicate = e => | ||
e.isContentEditable | ||
? (new MaskitoContentEditable(e) as unknown as MaskitoElement) | ||
? maskitoAdaptContentEditable(e) | ||
: e.querySelector<HTMLInputElement | HTMLTextAreaElement>('input,textarea') || | ||
(e as HTMLInputElement | HTMLTextAreaElement); |
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,49 @@ | ||
import {MaskitoElement, TextfieldLike} from '../types'; | ||
import {getContentEditableSelection, setContentEditableSelection} from './index'; | ||
|
||
class ContentEditableAdapter implements TextfieldLike { | ||
public maxLength = Infinity; | ||
|
||
constructor(private readonly element: HTMLElement) {} | ||
|
||
public get value(): string { | ||
return this.element.textContent || ''; | ||
} | ||
|
||
public set value(value) { | ||
this.element.textContent = value; | ||
} | ||
|
||
public get selectionStart(): number | null { | ||
return getContentEditableSelection(this.element)[0]; | ||
} | ||
|
||
public get selectionEnd(): number | null { | ||
return getContentEditableSelection(this.element)[1]; | ||
} | ||
|
||
public setSelectionRange(from: number | null, to: number | null): void { | ||
setContentEditableSelection(this.element, [from || 0, to || 0]); | ||
} | ||
} | ||
|
||
export function maskitoAdaptContentEditable(element: HTMLElement): MaskitoElement { | ||
const adapter = new ContentEditableAdapter(element); | ||
|
||
return new Proxy(element, { | ||
get(target, prop: keyof HTMLElement) { | ||
if (prop in adapter) { | ||
return adapter[prop as keyof ContentEditableAdapter]; | ||
} | ||
|
||
const nativeProperty = target[prop]; | ||
|
||
return typeof nativeProperty === 'function' | ||
? nativeProperty.bind(target) | ||
: nativeProperty; | ||
}, | ||
set(target, prop: keyof HTMLElement, val, receiver) { | ||
return Reflect.set(prop in adapter ? adapter : target, prop, val, receiver); | ||
}, | ||
}) as MaskitoElement; | ||
} |
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