Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TinyMCE - Inserting content at cursor position in IE doesn't work

I'm trying to insert some content at the current cursor position or replace the selected content in a TinyMCE editor instance. I'm using this:

tinyMCEPopup.editor.selection.setContent(content);

Where content is a string of HTML that I want to insert.

It works great on Firefox, Opera and Chrome, but won't work in IE. Instead it inserts content at the beginning of the editor, instead of the selection. I've also tried:

tinyMCEPopup.execCommand('mceInsertContent', false, content);

Again, same behavior in IE. The TinyMCE code for inserting special characters uses the function above and that works! I've tried replicating it in my plugin and still no joy...

function insertChar(chr) {
    tinyMCEPopup.execCommand('mceInsertContent', false, '&#' + chr + ';');

    // Refocus in window
    if (tinyMCEPopup.isWindow)
        window.focus();

    tinyMCEPopup.editor.focus();
    tinyMCEPopup.close();
}

UPDATE:

Still not working in IE, Opera doesn't even show my plugin's button in the toolbar (although that's probably a separate issue)! I've now also tried the jQuery plugin, got the editors to load, but couldn't call the mceInsertContent method on it, even using this:

$('#my-editor-id').tinymce().execCommand('mceInsertContent', false, content);

Got an error: "object has no method: tinymce" - maybe I can't call that from a popup?!

like image 825
greg84 Avatar asked Dec 10 '25 13:12

greg84


2 Answers

have you tried calling:

tinyMCEPopup.restoreSelection(); 

before calling

tinyMCEPopup.execCommand('mceInsertContent', false, '&#' + chr + ';');
like image 158
Katherine Unverdi Avatar answered Dec 16 '25 02:12

Katherine Unverdi


The problem is that TinyMCE loses the current cursor position when you click on something else like a button or link outside the editor, that causes it to lose focus.
To solve the problem, use the mousedown event of the button or link. In that way you can perform the insert action before the focus is lost.

$("#insert_link").mousedown(function (e) {
    var editor = tinyMCE.get("editor");
    editor.execCommand('mceInsertContent', false, "Insert Value");
    e.preventDefault();
});
like image 20
Terrence Tan Avatar answered Dec 16 '25 03:12

Terrence Tan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!