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
5d364f9
commit 54abc1c
Showing
29 changed files
with
416 additions
and
30 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
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 type {MaskitoElementPredicate} from '../types'; | ||
import {maskitoAdaptContentEditable} from '../utils'; | ||
|
||
export const MASKITO_DEFAULT_ELEMENT_PREDICATE: MaskitoElementPredicate = e => | ||
e.querySelector<HTMLInputElement | HTMLTextAreaElement>('input,textarea') || | ||
(e as HTMLInputElement | HTMLTextAreaElement); | ||
e.isContentEditable | ||
? 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
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 type {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 type {MaskitoOptions} from './mask-options'; | ||
import type {MaskitoElement} from './maskito-element'; | ||
|
||
export type MaskitoPlugin = ( | ||
element: HTMLInputElement | HTMLTextAreaElement, | ||
element: 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
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,51 @@ | ||
import type {MaskitoElement, TextfieldLike} from '../types'; | ||
import {getContentEditableSelection} from './dom/get-content-editable-selection'; | ||
import {setContentEditableSelection} from './dom/set-content-editable-selection'; | ||
|
||
class ContentEditableAdapter implements TextfieldLike { | ||
public maxLength = Infinity; | ||
|
||
constructor(private readonly element: HTMLElement) {} | ||
|
||
public get value(): string { | ||
return this.element.innerText.replace(/\n\n$/, '\n'); | ||
} | ||
|
||
public set value(value) { | ||
// Setting into innerHTML of element with `white-space: pre;` style | ||
this.element.innerHTML = value.replace(/\n$/, '\n\n'); | ||
} | ||
|
||
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; | ||
} |
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 type {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 type {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
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
50 changes: 50 additions & 0 deletions
50
projects/demo/src/pages/recipes/content-editable/content-editable-doc.component.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,50 @@ | ||
import {ChangeDetectionStrategy, Component} from '@angular/core'; | ||
import {RouterLink} from '@angular/router'; | ||
import {DemoPath, DocExamplePrimaryTab} from '@demo/constants'; | ||
import type {TuiDocExample} from '@taiga-ui/addon-doc'; | ||
import {TuiAddonDocModule} from '@taiga-ui/addon-doc'; | ||
import {TuiLinkModule, TuiNotificationModule} from '@taiga-ui/core'; | ||
|
||
import {ContentEditableDocExample1} from './examples/1-time/component'; | ||
import {ContentEditableDocExample2} from './examples/2-multi-line/component'; | ||
|
||
@Component({ | ||
standalone: true, | ||
selector: 'content-editable-doc', | ||
imports: [ | ||
TuiAddonDocModule, | ||
TuiLinkModule, | ||
RouterLink, | ||
ContentEditableDocExample1, | ||
ContentEditableDocExample2, | ||
TuiNotificationModule, | ||
], | ||
templateUrl: './content-editable-doc.template.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export default class ContentEditableDocComponent { | ||
protected readonly coreConceptsOverviewDocPage = `/${DemoPath.CoreConceptsOverview}`; | ||
protected readonly timeMaskDocPage = `/${DemoPath.Time}`; | ||
protected readonly angularDocPage = `/${DemoPath.Angular}`; | ||
protected readonly reactDocPage = `/${DemoPath.React}`; | ||
protected readonly vueDocPage = `/${DemoPath.Vue}`; | ||
protected readonly maskitoWithContentEditableDemo = import( | ||
'./examples/maskito-with-content-editable.md?raw' | ||
); | ||
|
||
protected readonly contentEditableExample1: TuiDocExample = { | ||
[DocExamplePrimaryTab.MaskitoOptions]: import('./examples/1-time/mask.ts?raw'), | ||
[DocExamplePrimaryTab.JavaScript]: import('./examples/vanilla-js-tab.md?raw'), | ||
[DocExamplePrimaryTab.Angular]: import('./examples/1-time/component.ts?raw'), | ||
}; | ||
|
||
protected readonly contentEditableExample2: TuiDocExample = { | ||
[DocExamplePrimaryTab.MaskitoOptions]: import( | ||
'./examples/2-multi-line/mask.ts?raw' | ||
), | ||
[DocExamplePrimaryTab.JavaScript]: import('./examples/vanilla-js-tab.md?raw'), | ||
[DocExamplePrimaryTab.Angular]: import( | ||
'./examples/2-multi-line/component.ts?raw' | ||
), | ||
}; | ||
} |
Oops, something went wrong.