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.
feat(core): add
contenteditable
support
- Loading branch information
1 parent
78bcebe
commit bd0cfdf
Showing
16 changed files
with
128 additions
and
21 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 {MaskitoElement} from '../types'; | ||
import {getContentEditableSelection, setContentEditableSelection} from '../utils'; | ||
|
||
// @ts-ignore | ||
export class MaskitoContentEditable implements MaskitoElement { | ||
maxLength = Infinity; | ||
|
||
constructor(private readonly element: HTMLElement) { | ||
const proxyHost = this; | ||
|
||
return new Proxy(element as any, { | ||
get(target, prop: keyof HTMLElement) { | ||
if (prop in proxyHost) { | ||
// @ts-ignore | ||
return proxyHost[prop]; | ||
} | ||
|
||
const nativeProperty = target[prop]; | ||
|
||
return typeof nativeProperty === 'function' | ||
? nativeProperty.bind(target) | ||
: target[prop]; | ||
}, | ||
set(target, prop: keyof HTMLElement, val, receiver) { | ||
return Reflect.set( | ||
prop in proxyHost ? proxyHost : target, | ||
prop, | ||
val, | ||
receiver, | ||
); | ||
}, | ||
}); | ||
} | ||
|
||
get value(): string { | ||
return this.element.textContent || ''; | ||
} | ||
|
||
set value(value) { | ||
this.element.textContent = value; | ||
} | ||
|
||
get selectionStart(): number | null { | ||
return getContentEditableSelection(this.element)[0]; | ||
} | ||
|
||
get selectionEnd(): number | null { | ||
return getContentEditableSelection(this.element)[1]; | ||
} | ||
|
||
setSelectionRange(from: number | null, to: number | null): void { | ||
setContentEditableSelection(this.element, [from || 0, to || 0]); | ||
} | ||
} |
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,2 +1,3 @@ | ||
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,5 +1,8 @@ | ||
import {MaskitoElementPredicate} from '../types'; | ||
import {MaskitoContentEditable} from '../classes'; | ||
import {MaskitoElement, MaskitoElementPredicate} from '../types'; | ||
|
||
export const MASKITO_DEFAULT_ELEMENT_PREDICATE: MaskitoElementPredicate = e => | ||
e.querySelector<HTMLInputElement | HTMLTextAreaElement>('input,textarea') || | ||
(e as HTMLInputElement | HTMLTextAreaElement); | ||
e.isContentEditable | ||
? (new MaskitoContentEditable(e) as unknown as MaskitoElement) | ||
: 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
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,6 +1,5 @@ | ||
import {MaskitoElement} from './maskito-element'; | ||
|
||
export type MaskitoElementPredicate = ( | ||
element: HTMLElement, | ||
) => | ||
| HTMLInputElement | ||
| HTMLTextAreaElement | ||
| Promise<HTMLInputElement | HTMLTextAreaElement>; | ||
) => MaskitoElement | Promise<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
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,5 @@ | ||
export type TextfieldLike = Pick< | ||
HTMLInputElement, | ||
'maxLength' | 'selectionEnd' | 'selectionStart' | 'setSelectionRange' | 'value' | ||
>; | ||
export type MaskitoElement = HTMLElement & TextfieldLike; |
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,6 +1,7 @@ | ||
import {MaskitoOptions} from './mask-options'; | ||
import {MaskitoElement} from './maskito-element'; | ||
|
||
export type MaskitoPlugin = ( | ||
element: HTMLInputElement | HTMLTextAreaElement, | ||
element: HTMLInputElement | HTMLTextAreaElement | MaskitoElement, | ||
options: Required<MaskitoOptions>, | ||
) => (() => void) | void; |
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
11 changes: 11 additions & 0 deletions
11
projects/core/src/lib/utils/dom/get-content-editable-selection.ts
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,11 @@ | ||
import {SelectionRange} from '../../types'; | ||
|
||
export function getContentEditableSelection(element: HTMLElement): SelectionRange { | ||
const {anchorOffset = 0, focusOffset = 0} = | ||
element.ownerDocument.getSelection() || {}; | ||
|
||
const from = Math.min(anchorOffset, focusOffset); | ||
const to = Math.max(anchorOffset, focusOffset); | ||
|
||
return [from, to]; | ||
} |
24 changes: 24 additions & 0 deletions
24
projects/core/src/lib/utils/dom/set-content-editable-selection.ts
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,24 @@ | ||
import {SelectionRange} from '../../types'; | ||
|
||
export function setContentEditableSelection( | ||
element: HTMLElement, | ||
[from, to]: SelectionRange, | ||
): void { | ||
const document = element.ownerDocument; | ||
const range = document.createRange(); | ||
|
||
range.setStart( | ||
element.firstChild || element, | ||
Math.min(from, element.textContent?.length || 0), | ||
); | ||
range.setEnd( | ||
element.lastChild || element, | ||
Math.min(to, element.textContent?.length || 0), | ||
); | ||
const selection = document.getSelection(); | ||
|
||
if (selection) { | ||
selection.removeAllRanges(); | ||
selection.addRange(range); | ||
} | ||
} |
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
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