webfashionist / RichText

WYSIWYG editor developed as jQuery plugin
GNU Affero General Public License v3.0
112 stars 62 forks source link

How can I insert text at current position #105

Open Richard-Dufour opened 1 year ago

Richard-Dufour commented 1 year ago

How can I insert text at the current cursor/caret position?

Richard-Dufour commented 1 year ago

This seems to work but is there a webfashionist/RichText way that would be more appropriate?

function InsertAtCursor(textToAdd) { console.log("textToAdd: ", textToAdd);

// These 4 lines is what actually inserts the text into the editor
let selection = window.getSelection();
let range = selection.getRangeAt(0);
let newNode = document.createTextNode(textToAdd);
range.insertNode(newNode);

}

webfashionist commented 10 months ago

Hi @Richard-Dufour

thank you for your question and code snippet.

There are similar functions within the code as of now, but none to add a custom text at the current caret position. What exactly is your use case for it?

Richard-Dufour commented 10 months ago

Just that, I need to be able to insert some text at the current caret position (where the mouse is currently located).

That code snippet work, but I wanted to know if there was a better way to do this.