Paragraph wrap callback for TinyMCE

When using TinyMCE if you only insert one line of text the selection will not be wrapped in paragraph tags. As soon as you hit return the selection will be wrapped in paragraphs but when you are handing the editor off to non-technical clients you can’t always be sure that’s going to happen.

We ran into this problem while building our form builder at UVa. Since the entire interface was being built with XSLT in Javascript the results coming out of the TinyMCE based message editor needed to be consistent. In the process I came up with the function that can be called as a save callback from TinyMCE.

This is the function that we will call from the callback. If you are using hak_tinymce make sure you have 0.6.3 and add it to the Callback Functions area.


function mceTagWrap(element_id, html, body) {
 html = trim(html);
 /*check that text starts and ends with tags if not wrap it in <p>s.  
   This only happens for single line text messages*/
  if(html.charAt(0) != "<" || (html.charAt(html.length - 1) != ">" || html.charAt(html.length -2) == "/")) {
	html = "<p>"+html+"</p>";	
  }
 return html;
}
function trim(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

The mceTagWrap() function will check if the results being sent back from TinyMCE start with < and end with > otherwise it will wrap it in <p></p>. It will also check if the second to last character is a / so a block opening with <img /> and ending with <br /> will still be wrapped in paragraph tags. trim() is just used by mceTagWrap() to remove unwanted spaces before and after the text block so that the first and last values are not spaces.

In order to call the function on save add this it to your TinyMCE.init().

save_callback:"mceTagWrap",

hak_tinymce - WYSIWYG article editor

This plugin adds a TinyMCE based WYSIWYG editor to Textpattern.

This is an update for mic_tinymce, originally developed by Michele Campeotto.

Read More »

Textile Quicktags

I implemented a quick alpha of Alex King’s Quicktags that generates textile formated text instead of html. This all got moved into action because of this thread over in the TXP forum.

The original intent was to use this in the admin side but Davidm had a good idea of using it for comments. I think that’s a great thought and was toying with putting in place after I finished this alpha yesterday.

Let me know what you think. Text-Tags Alpha

|