Skip to content

Commit

Permalink
refactor(core): turn MaskitoContentEditable class into `maskitoAdap…
Browse files Browse the repository at this point in the history
…tContentEditable` utility
  • Loading branch information
nsbarsukov committed Mar 7, 2024
1 parent 0f6783f commit e682dba
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 63 deletions.
2 changes: 1 addition & 1 deletion projects/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export {MaskitoContentEditable} from './lib/classes';
export {
MASKITO_DEFAULT_ELEMENT_PREDICATE,
MASKITO_DEFAULT_OPTIONS,
Expand All @@ -15,6 +14,7 @@ export {
MaskitoPreprocessor,
} from './lib/types';
export {
maskitoAdaptContentEditable,
maskitoInitialCalibrationPlugin,
maskitoPipe,
maskitoStrictCompositionPlugin,
Expand Down
58 changes: 0 additions & 58 deletions projects/core/src/lib/classes/content-editable.ts

This file was deleted.

1 change: 0 additions & 1 deletion projects/core/src/lib/classes/index.ts
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';
6 changes: 3 additions & 3 deletions projects/core/src/lib/constants/default-element-predicate.ts
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);
49 changes: 49 additions & 0 deletions projects/core/src/lib/utils/content-editable.ts
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;
}
1 change: 1 addition & 0 deletions projects/core/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './content-editable';
export * from './dom/event-listener';
export * from './dom/get-content-editable-selection';
export * from './dom/history-events';
Expand Down

0 comments on commit e682dba

Please sign in to comment.