Search code examples
javascripthtmldialogckeditorcodemirror

Replace Element in CKEditor Dialog after onShow Event


I try to write an own plugin for CKEditor. The goal is to click a button in the toolbar that opens a dialog. In the dialog there is a textArea element that should be replaced by a CodeMirror instance.

I came so far that the dialog opens and I can grab the textArea and replace it. But it looks crappy and has no functionality. Furthermore there is no error in the console.

I can't figure out why that doesn't work.

plugin.js that starts when hitting my plugin button

CKEDITOR.plugins.add( 'abbr', {
    icons: 'abbr',
    init: function( editor ) {
        editor.addCommand( 'abbr',new CKEDITOR.dialogCommand( 'abbrDialog' ) );

        // Create a toolbar button that executes the plugin command. 
        editor.ui.addButton( 'Abbr', {
            label: 'Insert SourceCode',
            command: 'abbr',
            toolbar: 'insert'
        } );

        CKEDITOR.dialog.add( 'abbrDialog', function ( editor ) {
            return {
                title : 'Insert SourceCode',
                minWidth : 700,
                minHeight : 300,
                contents : [{
                        id : 'tab1',
                        label : 'label1',
                        elements :
                        [{
                            type : 'html',
                            html: '<textarea id="codeEditor"></textarea>',
                            id : 'codeEditor',
                            label : 'CodeEditor',
                        }]
                }],
                onShow : replacaByCodeMirror,
                onOk : saveActionFunc
            };
        });
    }
});

function replacaByCodeMirror() {
    var codeEditor = CodeMirror.fromTextArea(document.getElementById("codeEditor"), {
        mode : "mixedMode",
        theme : "default"
    });
}

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <script src="../ckeditor.js"></script>
        <script src="../plugins/codemirror/js/codemirror.js"></script></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>

        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css" type="text/css"/>
        <link rel="stylesheet" href="../plugins/codemirror/css/codemirror.css"/>
        <link rel="stylesheet" href="../plugins/codemirror/css/myStyle.css">
    </head>
    <body>
        <form>
            <textarea name="editor1" id="editor1" rows="10" cols="80">
                StartWort
            </textarea>
            <script>

                var editor = CKEDITOR.replace( 'editor1' );

            </script>
        </form>
    </body>
</html>

Screenshot: Dialog Screenshot


Solution

  • You can't run javascript code inside a ckeditor window (or within the ckeditor dialog box). The work around is to use an iframe inside the dialog box (there is an iframedialog plugin you have to add).