Search code examples
sitecoresitecore7rich-text-editor

Customise Sitecore RichTextEditor to add default wrapper


The front end (html and css) is set up such a way that for the description text from a Sitecore content field needs to have a <p> tag wrapped around it.

So by default the RTE wraps texts in a <p> tag = TRUE. BUT the catch is you will need to hit Enter or copy/paste multiple paragraphs.

How can we force Sitecore to add a P tag if it's just one line?


Solution

  • Fortunately, From the dll, one particular function caught my eye:

    protected virtual void SetupScripts()
    {
        foreach (XmlNode node in Factory.GetConfigNodes("clientscripts/htmleditor/script"))
        this.Result.Scripts.AppendFormat("<script src=\"{0}\" language=\"{1}\"></script>\n", (object) XmlUtil.GetAttribute("src", node), (object) XmlUtil.GetAttribute("language", node));
    }
    

    NICE, eh? The developers of SITECORE are clever after all. So I did this in the web.config,

    <!— CLIENT SCRIPTS
    These script files are included in the client, e.g. '<script src="/myscript.js" language="JavaScript"/>'
    —>
    <clientscripts>
        <everypage />
        <htmleditor>
            <script src=”/assets/js/CustomRTE.js” language=”javascript”/>
        </htmleditor>
    </clientscripts>
    

    And overrode scSendRequest function from EditorWindow.aspx.

    window.scSendRequest = function(evt, command) {
        var editor = scRichText.getEditor();
        if (editor.get_mode() == 2) { //If in HTML edit mode
            editor.set_mode(1); //Set mode to Design
        }
        var htmls = editor.get_html(true);
        var regex = /<p[^>]*>.*?<\/p>/i;
        var match = regex.exec(htmls);
        if(match == null && htmls != null) {
            htmls = "<p>" + htmls + "</p>";
        }
    
        //$("EditorValue").value = editor.get_html(true);
        $("EditorValue").value = htmls;
    
        scForm.browser.clearEvent(evt);
    
        scForm.postRequest("", "", "", command);
    
        return false;
    }
    

    AND YAY .. double rainbow and unicorn.