Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4546588
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:58:29+00:00 2026-05-21T15:58:29+00:00

I’m trying to add a custom style in the style menu. style_formats : [

  • 0

I’m trying to add a custom style in the style menu.

style_formats : [
                {title:'Estilos'},
                    {title : 'Bold text', inline : 'b'},
                    {title : 'Blue text', inline : 'span', styles : {color : '#006'}},
                    {title : 'Blue header', block : 'h1', styles : {color : '#006'}},
   /*this one*/     {title : 'Codigo fuente', inline : 'code', classes : 'prettyprint', exact: true}
                 ],

Basically i want selected text to turn in:

<code class="prettyprint"> 
 codeline1
 codeline2
 codeline3 
</code>

But i get:

<code class="prettyprint"> codeline1</code>
<code class="prettyprint"> codeline2</code>
<code class="prettyprint"> codeline3 </code>

How can i make to all selection to be inserted in same <code></code> ??

tried also: {title : 'Codigo fuente', block : 'code', classes : 'prettyprint', exact: true}
And i get same result but just with no blank spaces or \n

If you want to see why i’m asking this

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:58:30+00:00Added an answer on May 21, 2026 at 3:58 pm

    I’ve been working on this on and off for a few days and still cannot get a fully working solution. I think it’s close but I just don’t have any more time to spend on it. It also seems to be rather hacky, so I have to ask if there is not just a more simple way to do this, that is, does it really need TinyMCE? Might be easier to just use a <textarea> and prettify.

    I also used a bit of advice from this other question: Remove styles when pasting from Word or other source

    The idea I was experimenting with was to manipulate the TinyMCE content if the code was “prettified” so that editing occurs on the raw text rather than the prettified version. So I have hooked into the TinyMCE onchange and onKeyDown callbacks to switch the content back to an unpretty version. The only problem is that the first keypress will not register as it is swallowed by the act of replacing the content. There is a way to Programmatically sending keys to input field? but it is not supported in Webkit!

    Also, there seems to be some bugs, as it still adds multiples <code> elements if code is typed directly into the TinyMCE. However, pasting code and then editing seems to work OK.
    So if you type code and then apply the “Source Code” style. all the carriage returns are removed (probably same issue as @András identified).

    So here is my partial solution:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Title</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <style type="text/css">
            pre {
                width:500px;
            }
    
            code {
                white-space:pre;
                line-height:1;
                margin:0;
                padding:0;
            }
    
            #pretty {
                display:block;
            }
        </style>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
        <script type="text/javascript" src="tiny_mce/tiny_mce.js"></script>
        <link type="text/css" rel="stylesheet" href="prettify/prettify.css" />
        <script type="text/javascript" src="prettify/prettify.js"></script> <!-- from http://code.google.com/p/google-code-prettify/ -->
        <script type="text/javascript">
            var plainContent = null;
    
            function applyPrettyPrint(inst) {
                if (tinyMCE.activeEditor.isDirty()) {
                    var content = tinyMCE.activeEditor.getContent({format : 'raw'});
                    if (content.indexOf('prettyprint') > 0) {
                        $('#pretty').html(content);
                        prettyPrint();
                        tinyMCE.activeEditor.setContent($('#pretty').html(), {format : 'raw'});
                    } else {
                        plainContent = content;
                    }
               }
            }
    
            tinyMCE.init({
                // General options
                mode:'textareas',
                theme:'advanced',
                forced_root_block:false,
                force_br_newlines:true,
                force_p_newlines:false,
                content_css:'prettify/prettify.css',
                // http://tinymce.moxiecode.com/wiki.php/Plugin:paste
                plugins:'paste',
                onchange_callback:'applyPrettyPrint',
                setup:function(ed) {
                    ed.onKeyDown.add(function(ed, e) {
                        if (ed.getContent().indexOf('prettyprint') > 0) {
                            ed.setContent(plainContent, {format : 'raw'});
                        }
                    });
                },
                style_formats:[
                    {title : 'Source Code', block : 'code', classes : 'prettyprint', exact: true}
                ]
            });
        </script>
    </head>
    <body>
        <form method="post" action="somepage">
            <div><textarea name="content" cols="50" rows="15"></textarea></div>
        </form>
        <p id="pretty" ></p>
        <p>Plain code to copy inside textarea</p>
        <pre>
    class Foo {
        private int bar = 0;
        public doSomething() {
            return bar;
        }
    }
    
    class Foo { private int bar = 0; public doSomething() { return bar; } }
    </pre>
    </body>
    </html>
    

    Note: You will need to download TinyMCE and prettify and make sure the paths to the .js and .css resources are correct for this to work.

    Hope it helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.