[ 5 posts ]

Ventzy Kunev

  • Username: Ventzy
  • Joined: Thu Nov 05, 2009 2:41 am
  • Posts: 35
  • Offline
  • Profile

Rich Text Editor - how to insert text at cursor?

Post Posted: Thu Jun 14, 2012 3:28 pm
+0-
How to insert text at cursor position in the editor? I looked at EditorSelection, but I cannot find any example how to use it.

Ventzy Kunev

  • Username: Ventzy
  • Joined: Thu Nov 05, 2009 2:41 am
  • Posts: 35
  • Offline
  • Profile
Tags:

Re: Rich Text Editor - how to insert text at cursor?

Post Posted: Fri Jun 15, 2012 12:10 am
+0-
This works in FF

Code:
editor.execCommand('insertHTML', 'bla');


Found it here https://developer.mozilla.org/en/Rich-Text_Editing_in_Mozilla. It will not work in IE and I have an impression that EditorSelection intends to deal with browser differences, so simple example will be appreciated.

Marco Asbreuk

YUI Contributor

  • Username: itsasbreuk
  • Joined: Mon Nov 16, 2009 5:14 am
  • Posts: 485
  • Location: Netherlands
  • Twitter: itsasbreuk
  • GitHub: ItsAsbreuk
  • Gists: ItsAsbreuk
  • IRC: Marco Asbreuk
  • Offline
  • Profile

Re: Rich Text Editor - how to insert text at cursor?

Post Posted: Mon Jun 18, 2012 12:52 pm
+0-
Hi Ventzy,

execCommand indeed seems to differentiate between browsers: http://www.quirksmode.org/dom/execCommand.html. So this is not a good thing...
I'm suffering the same at this moment trying to write my own toolbar.

I'm looking at something like manipulate the bodycontent of the editor-instance like this:

var editorY, editorBody, bodycontents, newbodycontents, insertcontent, cursorposition;
editorY = editor.getInstance();
editorBody = editorY.one('body');
bodycontents = editorBody.getHTML();
cursorposition = 25;
insertcontent = 'my text to be inserted';
newbodycontents = bodycontents.substring(0, cursorposition) + insertcontent + bodycontents.substring(cursorposition);
editorBody.setHTML(newbodycontents);

Or with less code:

editorBody = editor.getInstance().one('body');
bodycontents = editorBody.getHTML();
cursorposition = 25;
insertcontent = 'my text to be inserted';
editorBody.setHTML(bodycontents.substring(0, cursorposition) + insertcontent + bodycontents.substring(cursorposition));

The only thing I need to know now is: at what position is the cursor in the editorinstance?
I didn't figure that one out yet. Let you know if I have.

Kind Regards,
Marco.

Marc

YUI Contributor

  • Offline
  • Profile

Re: Rich Text Editor - how to insert text at cursor?

Post Posted: Mon Jun 18, 2012 2:18 pm
+0-
If you want to insert text at the cursor, the two methods that seem most effective in our company are
1. start typing
2. paste something you copied before with CTRL + V

Marco Asbreuk

YUI Contributor

  • Username: itsasbreuk
  • Joined: Mon Nov 16, 2009 5:14 am
  • Posts: 485
  • Location: Netherlands
  • Twitter: itsasbreuk
  • GitHub: ItsAsbreuk
  • Gists: ItsAsbreuk
  • IRC: Marco Asbreuk
  • Offline
  • Profile
Tags:

Re: Rich Text Editor - how to insert text at cursor?

Post Posted: Tue Jun 19, 2012 4:07 am
+0-
Hey Ventzy,

Forget my last post: it will bring up problems like scroll-postion and extra workload.
If you just want to insert at the cursorposition, this might work:

var HTMLtoInsert = 'blah blah blah';
var editorY = editor.getInstance();
if ((Y.UA.ie>0) && editorY.config.win.getSelection) {
editorY.config.win.getSelection().getRangeAt(0).pasteHTML(HTMLtoInsert);
}
else {
editor.execCommand("inserthtml", HTMLtoInsert);
}

Keep in mind that the user can select text before you insert: this will be overwritten.
I must say that execCommand really sucks browser wide. If you want to overcome this, than have a deeper look into how Dav Glass made its YUI2-editor: https://github.com/yui/yui2/blob/master ... -editor.js, especially its _createCurrentElement method.

Good luck,
Marco.
  [ 5 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum