Fixing FCKeditor To Work With BlogCFC, And Vice-Versa

In a previous article, we had configured FCKeditor to work with BlogCFC. After a couple of posts, however, we realized that FCKeditor was having problems with both "tags" we could use: <more/> and <code></code>. When switching from WYSIWYG to Source mode, FCKeditor would break all of our formatting, breaking the <more/> tag and destroying the <code> tag and it's content. After a little googling, here is what I was able to put together.

To achieve a workable result, the first thing we have to do is to obfuscate our tags. We want to maintain the functionality of our <more/> and <code> tags, but we don't want FCKeditor to think they're HTML tags. To do so, we simply replace the chevrons ( < > ) with square brackets ( [ ] ).

I'm writing a blog post![more/]
This is what people see when they click to read more!

If we want to get this to work, however, we first have to make a few changes to how BlogCFC saves and retrieves entries for editing. Open up /admin/entry.cfm, and towards line 17 you should find this line:

<cfset entry.body = entry.body & "<more/>" & entry.morebody>

Change it to this:

<cfset entry.body = entry.body & "[more/]" & entry.morebody>

This change makes sure that we send the bracketed version for editing and to FCKeditor. The next step is making sure BlogCFC recognizes the new format and saves our post accordingly. Towards line 156, make the following change:

<cfset strMoreTag = "<more/>">

Becomes:

<cfset strMoreTag = "[more/]">

Now BlogCFC recognizes our modified more tag and can react accordingly. Next we'll need to make similar changes for the code tag. Always in the entry.cfm file, add this at line 19 right before all those cfparams:

<CFset entry.body=REReplaceNoCase(entry.body, "<(/)*code>", "[\1code]", "all") />

Again we ensure that FCKeditor gets a bracketed tag. We need to make sure we convert it back to chevrons before saving it to the database. Towards line 150 (right after the smart quote fix) add this line:

<CFset form.body=REReplaceNoCase(form.body, "\[(/)*code\]", "<\1code>", "all") />

With this line added BlogCFC is ready to work with our new changes. However, we now want to make sure that FCKeditor will not break the text within code tags. To do so, we'll use FCKeditor's ProtectedSource object, and it's Add method:

FCKConfig.ProtectedSource.Add( /\[code[\s\S]*?\/code\]/gi );
This line protects all text with code tags. We can now switch happily between WYSIWYG and Source mode without affecting our code examples.

And that's all there is to it! Enjoy your unbroken FCKeditor implementation .

I lik-a do da cha-cha.

Related Blog Entries

 
Comments
Justin's Gravatar
Are you able to get the image stuff to work below the editor. For instance, can you use the img browser to insert an image? I seem to be able to get the images to add to the cookie that BlogCFC stores, but the images don't actually become visible in the textarea until I do a page refresh.

It's like the FCKeditor needs to refresh after the insert. Below is the function in camdens code that works, but not until I refresh the page and BlogCFC pulls the stored textarea value from the cookie (which is actually there in case the browser crashes mid-post).

function newImage(str) {
   var imgstr = '<img src="/img/blog/' + str + '">';
   var textbox = Spry.$("body");
   textbox.value = textbox.value + '\n' + imgstr;
}
# Posted By Justin | 2/14/08 12:15 PM
 
Francois Levesque's Gravatar
Hi Justin,

I haven't had the chance to play with those links, so I haven't tested this. Try modifying Ray's function with this:

function newImage(str) {
   var imgstr = '<img src="/img/blog/' + str + '">';
   var textbox = Spry.$("body");
   textbox.value = textbox.value + '\n' + imgstr;
   
   // add these 2 lines
   var oEditor = FCKeditorAPI.GetInstance('InstanceName');
   oEditor.InsertElement(imgstr);

}

In theory, this should retrieve the editor's instance and insert the IMG element at the cursor's position, without affecting Ray's working code.

Let me know if this works out.

hth
# Posted By Francois Levesque | 2/14/08 12:44 PM
 
Pissedlobster's Gravatar
Francois, this couldn't be any more timely! Both this and your previous post on implementing FCKeditor with BlogCFC are great! Thank you!!

I got the [more] tag to work, but still cannot get the [code] tag to work - it displays &lt;cfoutput&gt;#variable#&lt;/cfouput&gt;
# Posted By Pissedlobster | 2/14/08 3:40 PM
 
Francois Levesque's Gravatar
@Pissedlobster,

You want to find the line in entry.cfm where it calls the textarea custom tag. Here's what Scott P posted on the original article:

begin quote
change line 302 of entry.cfm
<cfmodule template="../tags/textarea.cfm" fieldname="body" value="#htmlEditFormat(form.body)#" class="txtArea">

Take out the htmlEditFormat(form.body) and just use form.body.

The line numbers maybe off since this is old but it should get you close.
/quote
# Posted By Francois Levesque | 2/14/08 5:28 PM
 
Justin's Gravatar
Thanks Francois.
Unfortunately, the below produces this error:

String contains an invalid character" code: "5


function newImage(str) {
   var imgstr = '<img src="/img/blog/' + str + '">';
   var textbox = Spry.$("body");
   textbox.value = textbox.value + '\n' + imgstr;
   var oEditor = FCKeditorAPI.GetInstance('body');
oEditor.InsertElement(imgstr);
}
# Posted By Justin | 2/14/08 7:58 PM
 
Francois Levesque's Gravatar
@Justin,

I found the cause for the trouble. Replace the InsertElement method with InsertHtml, like so:

function newImage(str) {
   var imgstr = '<img src="/img/blog/' + str + '">';
   var textbox = Spry.$("body");
   textbox.value = textbox.value + '\n' + imgstr;
   var oEditor = FCKeditorAPI.GetInstance('body');
   oEditor.InsertHtml(imgstr);
}

I've tested it on my end and it works, let me know if it solves the problem on your side.
# Posted By Francois Levesque | 2/14/08 9:09 PM
 
Justin's Gravatar
Tada! It works, awesome! I've pretty much forgotten javascript over the years, so I felt like a newb trying to debug.

On to the next stage of BlogCFC customization!

p.s. I'm using blog CFC integrated into another application. I've basically changed everything from application.x to application.blog.x or application.blog.theBlog.getX()

I've integrated the admin part into the existing application admin and have all the blog code stored elsewhere. It works pretty nicely this way. Admin and the blog share the base code which is stored above wwwroot.

I'm doing the same thing with Galleon.
# Posted By Justin | 2/14/08 9:29 PM
 
Francois Levesque's Gravatar
Congrats and good luck with that integration! It looks like a lot of work.
# Posted By Francois Levesque | 2/15/08 5:56 AM

BlogCFC was created by Raymond Camden. This blog is running version 5.9.001. Contact Blog Owner