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
b543858
commit 2100589
Showing
17 changed files
with
138 additions
and
25 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,58 @@ | ||
import {MaskitoElement} from '../types'; | ||
import {getContentEditableSelection, setContentEditableSelection} from '../utils'; | ||
|
||
// @ts-ignore Type MaskitoContentEditable is missing the following properties from type HTMLElement: accessKey, accessKeyLabel, autocapitalize, dir etc. | ||
export class MaskitoContentEditable implements MaskitoElement { | ||
maxLength = Infinity; | ||
|
||
constructor(private readonly element: HTMLElement) { | ||
const proxyHost = this; | ||
|
||
/** | ||
* We cannot just write `export class MaskitoContentEditable extends Proxy`. | ||
* > The Proxy constructor does not have a prototype property because | ||
* > proxy exotic objects do not have a [[Prototype]] internal slot that requires initialization. | ||
*/ | ||
return new Proxy(element as any, { | ||
get(target, prop: keyof HTMLElement) { | ||
if (prop in proxyHost) { | ||
return proxyHost[prop as keyof MaskitoContentEditable]; | ||
} | ||
|
||
const nativeProperty = target[prop]; | ||
|
||
return typeof nativeProperty === 'function' | ||
? nativeProperty.bind(target) | ||
: nativeProperty; | ||
}, | ||
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
12 changes: 12 additions & 0 deletions
12
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,12 @@ | ||
import {SelectionRange} from '../../types'; | ||
|
||
// TODO: add multi-line support later | ||
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]; | ||
} |
25 changes: 25 additions & 0 deletions
25
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,25 @@ | ||
import {SelectionRange} from '../../types'; | ||
|
||
// TODO: add multi-line support later | ||
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
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