File

src/lib/contenteditable.directive.ts

Metadata

Index

Properties
Methods
Inputs
Outputs
HostBindings
HostListeners

Constructor

constructor(elementRef: ElementRef)
Parameters :
Name Type Optional
elementRef ElementRef No

Inputs

initial
Type : string
parameters
Type : any
rxapContenteditable
Type : Method<any | ContenteditableEvent>

Outputs

rxapContenteditable
Type : EventEmitter

HostBindings

attr.contenteditable
Type : boolean
Default value : true
attr.spellcheck
Type : boolean
Default value : false

HostListeners

click
Arguments : '$event'
input
Arguments : '$event'

Methods

Public onClick
onClick($event: Event)
Decorators :
@HostListener('click', ['$event'])
Parameters :
Name Type Optional
$event Event No
Returns : void
Public Async onInput
onInput($event: any)
Decorators :
@HostListener('input', ['$event'])
@DebounceCall(1000)
Parameters :
Name Type Optional
$event any No
Returns : any

Properties

Public contenteditable
Default value : true
Decorators :
@HostBinding('attr.contenteditable')
Public spellcheck
Default value : false
Decorators :
@HostBinding('attr.spellcheck')
import {
  Directive,
  ElementRef,
  EventEmitter,
  HostBinding,
  HostListener,
  Input,
  Output,
} from '@angular/core';
import { DebounceCall } from '@rxap/utilities';
import { Method } from '@rxap/pattern';

export interface ContenteditableEvent {
  value: string;
  parameters?: any;
}

@Directive({
  selector: '[rxapContenteditable]',
  standalone: true,
})
export class ContenteditableDirective {

  @HostBinding('attr.contenteditable')
  public contenteditable = true;

  @HostBinding('attr.spellcheck')
  public spellcheck = false;

  @Input('rxapContenteditable')
  public method?: Method<any, ContenteditableEvent>;

  @Output('rxapContenteditable')
  // eslint-disable-next-line @angular-eslint/no-output-native
  public change = new EventEmitter<ContenteditableEvent>();

  @Input()
  public parameters?: any;

  @Input()
  public initial?: string;

  constructor(private readonly elementRef: ElementRef) {
  }

  @HostListener('click', [ '$event' ])
  public onClick($event: Event) {
    $event.stopPropagation();
  }

  @HostListener('input', [ '$event' ])
  @DebounceCall(1000)
  public async onInput($event: any) {
    const value = (($event.target as HTMLElement).textContent)?.trim();
    if (value && value.length >= 2) {
      const event: ContenteditableEvent = {
        value,
        parameters: this.parameters,
      };
      this.change.emit(event);
      const result = await this.method?.call(event);
      if (result && typeof result === 'string') {
        this.initial = this.elementRef.nativeElement.innerText = result;
      } else if (this.initial) {
        this.elementRef.nativeElement.innerText = this.initial;
      }
    }
  }

}



results matching ""

    No results matching ""