twitter-bootstrapckeditorcolorbox

How to add responsive class to ckeditor image dialog?


I have website baker that has CK Editor. I want to have images automatically have the class "img-responsive" if no classes are inserted. How? Can i Do that

I cant find a dictionary of this http://docs.cksource.com/CKEditor_3.x/Howto/Default_Field_Values what names are every input and so on? and the html source wont show that.


Solution

  • I had the same problem long time ago. I probably copy-pasted most of the code and I did some minor modification by myself. Its a bit long code, which add img-responsive class to all added images.

     CKEDITOR.replace('editor1');
        CKEDITOR.on('instanceReady', function(ev) {
    
            //resp. images for bootstrap 3
            ev.editor.dataProcessor.htmlFilter.addRules(
                    {
                        elements:
                                {
                                    $: function(element) {
                                        // Output dimensions of images as width and height
                                        if (element.name == 'img') {
                                            var style = element.attributes.style;
                                            //responzive images
    
                                            //declare vars
                                            var tclass = "";
                                            var add_class = false;
    
                                            tclass = element.attributes.class;
    
                                            //console.log(tclass);
                                            //console.log(typeof (tclass));
    
                                            if (tclass === "undefined" || typeof (tclass) === "undefined") {
                                                add_class = true;
                                            } else {
                                                //console.log("I am not undefined");
                                                if (tclass.indexOf("img-responsive") == -1) {
                                                    add_class = true;
                                                }
                                            }
    
                                            if (add_class) {
                                                var rclass = (tclass === undefined || typeof (tclass) === "undefined" ? "img-responsive" : tclass + " " + "img-responsive");
                                                element.attributes.class = rclass;
                                            }
    
                                            if (style) {
                                                // Get the width from the style.
                                                var match = /(?:^|\s)width\s*:\s*(\d+)px/i.exec(style),
                                                        width = match && match[1];
    
                                                // Get the height from the style.
                                                match = /(?:^|\s)height\s*:\s*(\d+)px/i.exec(style);
                                                var height = match && match[1];
    
                                                if (width) {
                                                    element.attributes.style = element.attributes.style.replace(/(?:^|\s)width\s*:\s*(\d+)px;?/i, '');
                                                    element.attributes.width = width;
                                                }
    
                                                if (height) {
                                                    element.attributes.style = element.attributes.style.replace(/(?:^|\s)height\s*:\s*(\d+)px;?/i, '');
                                                    element.attributes.height = height;
                                                }
                                            }
                                        }
    
    
    
                                        if (!element.attributes.style)
                                            delete element.attributes.style;
    
                                        return element;
                                    }
                                }
                    });
        });