angular-editor icon indicating copy to clipboard operation
angular-editor copied to clipboard

Get pointer position in textarea

Open iam-sanjay opened this issue 5 years ago • 28 comments

Hello Team, I want to know pointer position in textarea and requirement is like to insert predefined text in that position. Please help to me!

Thanks, San

iam-sanjay avatar Jun 11 '19 10:06 iam-sanjay

Hi! What do you mean by pointer position?

kolkov avatar Jun 11 '19 11:06 kolkov

Hi Kolkov, Thanks for reply.

Example: hello world!

Cursor has on 'w' and I want to know the position and will add new text 'my ' on that position.

Output: hello my world!

Thanks, San

iam-sanjay avatar Jun 11 '19 12:06 iam-sanjay

Those. Do you want to programmatically insert arbitrary text into the position of the cursor?

kolkov avatar Jun 28 '19 08:06 kolkov

You can insert the node at the position using a click event on the editor..! Call this function on click insertNode() { let selection = window.getSelection().getRangeAt(0); let parentNode = document.createElement("a"); //create a custom node to insert selection.insertNode(parentNode); parentNode.insertAdjacentHTML("beforebegin", " "); parentNode.insertAdjacentHTML("afterend", " "); //add space after and before node to bring cursor outof the node }

yashm19 avatar Jul 03 '19 09:07 yashm19

Those. Do you want to programmatically insert arbitrary text into the position of the cursor?

Yes. How can I do that?

thinkdj avatar Sep 13 '19 08:09 thinkdj

This is something which I need too.

ankit1329 avatar Nov 20 '19 05:11 ankit1329

How can we insert text at cursor position (which can be somewhere in between the entered html) programmatically.

ankit1329 avatar Nov 20 '19 05:11 ankit1329

Has anyone succeeded?

newmesiss avatar Dec 16 '19 13:12 newmesiss

I don't understand how you want to insert some text at the position. We have node tree, not plain text in editor.

kolkov avatar Dec 16 '19 16:12 kolkov

@kolkov I will tell what I ran into: We wanted to insert variables like %firstname%, %account% etc into wherever the cursor was at in the text editor.

thinkdj avatar Dec 16 '19 16:12 thinkdj

@thinkdj what should be used as a trigger for insertion in your case?

kolkov avatar Dec 16 '19 18:12 kolkov

I had the same task. I need to insert some text in current cursor position when user clicks on the list of values in my component. I tried to do like this:

this.editor.focus();
this.editorService.restoreSelection();
this.editorService.insertHtml('text to insert here');

But it inserts always at the beginnig of text. Any suggestions?

ilacomp avatar Jan 24 '20 10:01 ilacomp

I think it would be very useful to add w possibility of inserting a string at last cursor position (or at the end of the string is the area was not yet touched). I tried to fullfill my use case (which is very similar to described above) using patchValue (I am using editor as form control) but unfortunately then the undo/redo possibility stops working.

Svitra avatar Feb 10 '20 14:02 Svitra

Hello there,

I just signed the petition "San Jose State University : Refund SJSU Student’s Tuition" and wanted to see if you could help by adding your name.

Our goal is to reach 5,000 signatures and we need more support. You can read more and sign the petition here:

http://chng.it/b4rJ8Tyx6x

Thanks! Yash

yashm19 avatar Mar 21 '20 12:03 yashm19

@thinkdj what should be used as a trigger for insertion in your case?

In my case, I am going to have a list of buttons and each will insert different text at the position of the cursor, is this possible using your HTML editor? If it isn't possible with cursor, is there another way to do it? Appreciate the great tool you made.

For now I am just appending to the end, which should work fine for most cases, but in the case where someone wants to insert text before the end is where that issue would arise.

Emilio-K avatar May 29 '20 15:05 Emilio-K

I had the same task. I need to insert some text in current cursor position when user clicks on the list of values in my component. I tried to do like this:

this.editor.focus();
this.editorService.restoreSelection();
this.editorService.insertHtml('text to insert here');

But it inserts always at the beginnig of text. Any suggestions?

  1. make template variable of any type
@ViewChild('editor') editor: any;
  1. your method will be updated as follows
insertHTML(data: string): void {
    this.editor.focus();
    this.editor.editorService.restoreSelection();
    this.editor.editorService.insertHtml(data);
}

sagar-agola avatar May 11 '21 16:05 sagar-agola

<angular-editor #editor [(ngModel)]="htmlContent" [config]="config">

<button (click)="editor.editorService.insertHtml('enter-your-text-here')">ADD TEXT

LuigiRibeiro avatar Feb 17 '22 23:02 LuigiRibeiro

<angular-editor (keyup)="txtEditorPosition(txtEditor)" (click)="txtEditorPosition(txtEditor)"> txtEditorPosition(position) { console.log(position?.editorService?.savedSelection?.startOffset); }

That is starting position returned as number. There is also endOffset also returned as number. There is also selectedText if user selected some part of text. Cheers :)

lilke1992 avatar Feb 28 '22 23:02 lilke1992

You are very good Thanks., you have saved my time

SeyedZia avatar May 09 '22 14:05 SeyedZia

I found only one way to implement text dynamically to kolkov/angular-editor package.

in the html file: <angular-editor #editor [(ngModel)]="htmlContent" [config]="config" (ngModelChange)="onContentChange($event)"></angular-editor>

in the ts file:

addTextDynamically() {
    let cursor = this.editor.editorService.savedSelection.startOffset;
    this.editor.editorService.savedSelection.startContainer.data =
        this.editor.editorService.savedSelection.startContainer.data.slice(0, cursor)
        + ["implemented string"] + this.editor.editorService.savedSelection.startContainer.data.slice(cursor);
    setTimeout(() => {
        this.editor.editorService.insertHtml(" ");
    }, 0);
}

*the insertHtml in the end is for the change to take effect also in the htmlContent property.

ShemiNechmad avatar Jun 21 '22 06:06 ShemiNechmad

I think the feature asked had been implemented, though not as abstract as some would expect. Here's the codes that should work:

			navigator.clipboard.readText().then(
				s => {
						this.htmlEditorService.insertHtml(s);
	
				}
			);

s from readText is already plain text with line breaks transformed from HTML. To get HTML, you may need to use clipboard.read().

In your component or module, you need:

	providers: [
		AngularEditorService
	]

The AngularEditorComponent is also using this service though you cannot directly reference it because it is a private memeber. However, because AngularEditorService is a singleton service at least in some contexts, so calling this.htmlEditorService.insertHtml(s) will insert context to the cursor/selection of the editor. I guess the presumption was that you have only one editor at a time, otherwise, with 2 editors, the service may behave strangely though I had never tested such scenario.

I think this issue may be closed.

zijianhuang avatar Jun 28 '22 01:06 zijianhuang

@zijianhuang Thanks a lot! It's now working :) Here is what I did for those who want:

` onPaste(event: ClipboardEvent) {

      this.consoleLoggerService.log('onPaste');
      event.stopPropagation();
      event.stopImmediatePropagation();
      event.preventDefault();
      const htmlMode = this.myEditor.editorToolbar.htmlMode;
      if (htmlMode) {
          this.consoleLoggerService.log('do not modify text pasted when in htmlMode');
          return;
      }
      let pastedText = event.clipboardData.getData('text/html');
      pastedText = this.modifyTextPasted(pastedText);
      this.angularEditorService.insertHtml(pastedText);

}

`

Kenji94 avatar Nov 02 '22 11:11 Kenji94

@zijianhuang Thanks a lot! It's now working :) Here is what I did for those who want:

` onPaste(event: ClipboardEvent) {

      this.consoleLoggerService.log('onPaste');
      event.stopPropagation();
      event.stopImmediatePropagation();
      event.preventDefault();
      const htmlMode = this.myEditor.editorToolbar.htmlMode;
      if (htmlMode) {
          this.consoleLoggerService.log('do not modify text pasted when in htmlMode');
          return;
      }
      let pastedText = event.clipboardData.getData('text/html');
      pastedText = this.modifyTextPasted(pastedText);
      this.angularEditorService.insertHtml(pastedText);

}

`

But does this solution add the content at the cursor position? I tried all the solutions here but none worked. I have the same need as the thread creator: insert %keywords% at the cursor position

ganholete avatar Dec 29 '22 11:12 ganholete

But does this solution add the content at the cursor position? I tried all the solutions here but none worked. I have the same need as the thread creator: insert %keywords% at the cursor position

Yes it does, you can find it working on https://neopload.neovel.io/ Create an book and add a chapter, you will find the text editor on the chapter page.

Kenji94 avatar Dec 30 '22 16:12 Kenji94

If I could understand, I didn't find in your editor the desire that the author describes: adding keywords to the cursor after clicking on an item. This would be the scenario: image

ganholete avatar Dec 30 '22 18:12 ganholete

I found the problem. I need to add a href="#" in the link, otherwise the editor shows me the error "ERROR Error: Unable to perform the operation". Here is the code working as I need it. If I remove href="#" from the link it stops working: https://stackblitz.com/edit/angular-ivy-kgudlp Is this a bug?

ganholete avatar Jan 02 '23 15:01 ganholete

I had problem when trying to add HTML string in editor. But i found the form to add asynchronous data with the custom buttons.

image

First button for add asynchronous html with my logic business and use the custom buttons you can add that item.

Second button to paste htmlString

Third button to delete reference

You need to add a property in your component.ts for save that htmlString

<ng-template #customButtons let-executeCommandFn="executeCommandFn"> <ae-toolbar-set> <ae-button iconClass="fa-solid fa-book" title="Agregar referencia a una evidencia" (buttonClick)="addEvidenceLink()" > </ae-button> <ae-button iconClass="fa-solid fa-clipboard" title="Pegar referencia" (buttonClick)=" executeCommandFn('insertHtml', linkToAdd ?? '') " > </ae-button> <ae-button iconClass="fa-solid fa-trash-o" title="Borrar referencia" (buttonClick)="deleteLink()" > </ae-button> </ae-toolbar-set> <ae-toolbar-set> <ae-button iconClass="fa-solid fa-table-cells" title="Agregar tabla" (buttonClick)="openAddTable()" > </ae-button> <ae-button iconClass="fa-solid fa-clipboard" title="Pegar tabla" (buttonClick)=" executeCommandFn('insertHtml', tableToAdd ?? '') " > </ae-button> <ae-button iconClass="fa-solid fa-trash-o" title="Borrar tabla" (buttonClick)="deleteTable()" > </ae-button> </ae-toolbar-set> </ng-template>

JorgeMdn avatar May 15 '23 16:05 JorgeMdn