| Page 1 of 1 | [ 5 posts ] |
|
How to insert text at cursor position in the editor? I looked at EditorSelection, but I cannot find any example how to use it.
|
|
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 AsbreukYUI Contributor
|
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. |
MarcYUI Contributor
|
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 AsbreukYUI Contributor
|
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. |
| Page 1 of 1 | [ 5 posts ] |
| 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 |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group