How to indent the first line of a paragraph in CKEditor

后端 未结 2 1880
情话喂你
情话喂你 2021-01-23 06:17

I\'m using CKEditor and I want to indent just the first line of the paragraph. What I\'ve done before is click \"Source\" and edit the

style to include

2条回答
  •  故里飘歌
    2021-01-23 06:44

    I've got a way to indent the first line without using style, because I'm using iReport to generate automatic reports. Jasper does not understand styles. So I assign by jQuery an onkeydown method to the main iframe of CKEditor 4.6 and I check the TAB and Shift key to do and undo the first line indentation.

    // TAB
        $(document).ready(function(){
            startTab();
        });
    
        function startTab() {
            setTimeout(function(){
                var $iframe_document;
                var $iframe;
                $iframe_document = $('.cke_wysiwyg_frame').contents();
                $iframe = $iframe_document.find('body');
    
                $iframe.keydown(function(e){
                    event_onkeydown(e);
                });
    
            },300); 
        }
    
        function event_onkeydown(event){
            if(event.keyCode===9) { // key tab
                event.preventDefault();
                setTimeout(function(){
                    var editor = CKEDITOR.instances['editor1'], //get your CKEDITOR instance here
                        range = editor.getSelection().getRanges()[0],
                        startNode = range.startContainer,
                        element = startNode.$,
                        parent;
    
                    if(element.parentNode.tagName != 'BODY') // If you take an inner element of the paragraph, get the parentNode (P)
                        parent = element.parentNode;
                    else // If it takes BODY as parentNode, it updates the inner element
                        parent = element;
    
                    if(event.shiftKey) { // reverse tab
                        var res = parent.innerHTML.toString().split(' ');
                        var aux = [];
                        var count_space = 0;
    
                        for(var i=0;i 8 || res[i] != "") {
                                if(!count_space > 8)
                                    count_space = 9;
                                aux.push(res[i]);
                            }
                        }
                        parent.innerHTML = aux.join(' ');
                    }
                    else { // tab
                        var spaces = "        ";
                        parent.innerHTML = spaces + parent.innerHTML;
                    }
                },200);
            }
        }
    

提交回复
热议问题