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!
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
onchangeandonKeyDowncallbacks 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:
Note: You will need to download TinyMCE and prettify and make sure the paths to the
.jsand.cssresources are correct for this to work.Hope it helps!