0

I was checking this documentation...

https://material.angular.io/cdk/clipboard/examples

Is there some example explaining how to paste inside some text of the last current (maybe starting, or middle) position inside of TextArea (after of lost focus event).

I was trying with this simple response: https://stackoverflow.com/a/58356806/5113188

function insertAtCaret(text) {
  const textarea = document.querySelector('textarea')
  textarea.setRangeText(
    text,
    textarea.selectionStart,
    textarea.selectionEnd,
    'end'
  )
}

setInterval(() => insertAtCaret('Hello'), 3000)

Adapting to my code:

In .html file:

    <button mat-icon-button type="button" (click)="onInsertSomethingText(entry)" >
      <mat-icon>play_for_work</mat-icon>
    </button>

  <div>
    <mat-form-field>
      <textarea
        [id]="BODY_TEXTAREA"
        matInput
        rows="5"
        formControlName="body"
      ></textarea>
    </mat-form-field>
  </div>

In my .ts file

  public readonly BODY_TEXTAREA = 'BODY_TEXTAREA';
  
  private buildMyFormGroup() {
    return internalFormGroup = this.formBuilder.group({
      body: ['', [Validators.required, Validators.minLength(1)]],
    });
  }
   
  //Event in ordfer to insert the text in the position!!!!
  public onInsertSomethingText(myText: string) {
    console.log(myText);
    const myTA = document.querySelector(this.BODY_TEXTAREA);
    myTA.setRangeText(myText, myTA.selectionStart, myTA.selectionEnd, 'end');
  }
 

In this line myTA.setRangeText(myText, myTA.selectionStart, myTA.selectionEnd, 'end'); I'm obtaining these:

  • Property 'setRangeText' does not exist on type 'Element'.ts(2339)
  • Property 'selectionStart' does not exist on type 'Element'.ts(2339)
  • Property 'selectionEnd' does not exist on type 'Element'.ts(2339)

I know that possibly, the myTA is not interpreted as a real TextArea

How to do it correctly?

Thanks in Advance

1 Answers1

2

When you're dealing with Angular you're in TypeScript world.

document.querySelector method is a generic method where by default it returns Element:

querySelector<E extends Element = Element>(selectors: string): E | null;

Simply choose the proper type and you're done:

const myTA = document.querySelector<HTMLTextAreaElement>(`#${this.BODY_TEXTAREA}`);
                                    ^^^^^^^^^^^^^^^^^^^          
yurzui
  • 205,937
  • 32
  • 433
  • 399
  • Thanks, unfortunately I getting null in `myTA` variable then I can't test it. I know that is a change in the question. I'm so sorry, :( –  Nov 08 '21 at 14:36
  • @Anita Can you please prepare a stackblitz example so I can help? – yurzui Nov 08 '21 at 14:39
  • Sure!!! Here my attempt https://stackblitz.com/edit/angular-ivy-eenyyo –  Nov 08 '21 at 15:41
  • Since you're selecting by id you should be adding `#` before selector `document.querySelector('#${this.BODY_TEXTAREA}')` https://stackblitz.com/edit/angular-ivy-nr6ngv?file=src%2Fapp%2Fapp.component.ts – yurzui Nov 08 '21 at 15:48
  • `const myTA = document.querySelector('#'+ this.BODY_TEXTAREA);` –  Nov 08 '21 at 15:52
  • @Anita Yes, that's the same – yurzui Nov 08 '21 at 15:52
  • 1
    The single quote doesn't work I need to use backticK! –  Nov 08 '21 at 15:55
  • @Anita They should work. You're doing smth wrong. Please check here https://stackblitz.com/edit/angular-ivy-qmnksm?file=src%2Fapp%2Fapp.component.ts – yurzui Nov 08 '21 at 15:56