4

We recently moved away from tinymce, and a custom function we created there was a toolbar button that, once clicked, slid up a custom div right above the toolbar, which was appended to the toolbar on page load.

The problem now is doing the same for CKEditor when using the Inline version. Since there are a dozen of instances active at any one time, how can I fetch the instance of the current popped up inline Ckeditor so that I can append to it with jQuery, after the custom plugin button has been clicked?

Using the latest version 4.x of CKEditor.

4 Answers 4

8

It's hard to imagine what you're trying to accomplish. Anyway, you can observe which editor instance is focused (you can eventually store a reference in some variable):

CKEDITOR.on( 'instanceReady', function( event ) {
    event.editor.on( 'focus', function() {
        console.log( 'focused', this );
    });
});

After all, you can also browse editor instances since they are stored in CKEDITOR.instances object in global namespace. With this, you can find your instance by name, id, anything (i.e. previously associated with your button).

Sign up to request clarification or add additional context in comments.

3 Comments

This is exactly what I needed, thanks! (the focusedInstance variable is undefined though). I'm trying to avoid using the CKEditor dialog, so the edited text doesn't get covered by it while using the plugin to insert certain text. I think this should work, so I'll accept as soon as possible.
Fixed missing variable. Replied in hurry ;-)
I was in need of the exact same thing, so that I could make the save button appear only when the focus is on the editor.
3

I'd do it like this

var ck_instance_name = false;
for ( var ck_instance in CKEDITOR.instances ){
    if (CKEDITOR.instances[ck_instance].focusManager.hasFocus){
        ck_instance_name = ck_instance;
        return ck_instance_name;
    }
}

1 Comment

This should be the accepted answer: tracking the focus on the instances like other answers suggest is really useless, given the fact that Ckeditor itself knows which editor has the focus. Of course after ten years the code can be revanoed, but that's beyond the point.
1

Get current instance of ck editor. Here is simple code

var currentEditor;    
 for(var id in CKEDITOR.instances) {
  CKEDITOR.instances[id].on('focus', function(e) {
    // Fill some global var here
    currentEditor = e.editor.name;
});
}

Comments

0

Using the accepted answer above I found that the editor "name" (as it's known) of the editor instance that triggered the given plugin is accessible via this._.editor.name (using CKEditor v4.3)

As such you can retrieve the editor content in this manner.

CKEDITOR.instances[this._.editor.name].getData();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.