var jcommunicator = {
childwindow : null,
populatechange : function (ed , l ) {
var jcommunicator = jQuery(this.childwindow.document);
var elementpostcontainer = this.getpostelement(jcommunicator);
elementpostcontainer.html(ed.getBody().innerHTML);
},
populatehtmlchange : function (html) {
var jcommunicator = jQuery(this.childwindow.document);
var elementpostcontainer = this.getpostelement(jcommunicator);
//An empty line = line with only white space
//First replace all one or more empty lines with one br.
var regex = /^\s*$/gm;
var updatedhtml = html.replace(regex,"~%activepreview%~");
//Replace any line break at any line that is no
//line ending with a br
regex = /^\s*((<((span)|(a)|(b)|(i)|(sup)|(u)|(s)|(em)|(label)|(sub)|(strong)|(strike)).*)|((^<^\/)&(^~^%).*)|(\w.*))$/gmi;
updatedhtml = updatedhtml.replace(regex,"$1
\n");
//Remove the active preview place holders now to avoid double spacing.
regex = /(~%activepreview%~)+/g;
updatedhtml = updatedhtml.replace(regex,"
");
elementpostcontainer.html(updatedhtml);
},
getpostelement : function (jcommunicator) {
//This div is actually always found because we wrap it with a filter in PHP.
return jcommunicator.find('#wpactivepreview');
}
};
jQuery(document).ready(function () {
//Insert the new preview link.
//id for the preview action div is - preview-action
var previewdiv = jQuery('#preview-action');
//Only do preview logic if you have a valid preview link.
if(previewdiv && previewdiv.length != 0) {
//Figure out the what the preview button text is for internationalization this text could vary.
var previewbutton = jQuery('#post-preview');
var previewbuttontext = previewbutton.html();
var additionalbuttonhtml = previewdiv.html().replace("post-preview","post-preview2");
//Chop out any text no associated with the anchor tag.
additionalbuttonhtml = additionalbuttonhtml.substring(additionalbuttonhtml.toLowerCase().indexOf('')+4);
additionalbuttonhtml = additionalbuttonhtml.replace(previewbuttontext,"Active Preview");
var actiondiv = jQuery('#minor-publishing-actions');
actiondiv.append(""+additionalbuttonhtml+"
");
//Because tinyMCE doesn't get initialized all the way when they have the
//html editor selected on first page visit. SO lets just hook into the
// visual link on the page and initialize if nothing was initialized yet.
var previeweditorlink = jQuery('#edButtonPreview');
previeweditorlink.click(function (e) {
switchEditors.go('content', 'tinymce');
if(!window.tinyalreadyinit){
//Hook a listener onto the iframe for changes and send the changes to the content div for the post which wordpress puts into the page.
//We need to hook into onKeyUp to catch key strokes.
tinyMCE.activeEditor.onKeyUp.add(function (ed, l) {
jcommunicator.populatechange(ed,l);
});
//We need to hook into onNodeChange to get changes that aren't necessarily related to key strokes (like changing text color etc)
tinyMCE.activeEditor.onNodeChange.add(function (ed, l) {
jcommunicator.populatechange(ed,l);
});
window.tinyalreadyinit = true;
}
});
//Hook in the onclick to do an open but keep a reference to the window that was opened for update.
var activelink = jQuery('#post-preview2');
activelink.click(function (e) {
e.preventDefault();
//Don't allow this functionality in IE. It just doesn't work well there.
if(jQuery.browser.msie){
//For now do an old school alert.
alert("Active Preview does not support IE browsers, to use functionality please use with another browser");
} else {
jcommunicator.childwindow = window.open(this.href,'activepreview','menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
jQuery('#content').keyup(function() {
jcommunicator.populatehtmlchange(this.value);
});
if(!window.tinyalreadyinit && tinyMCE.activeEditor){
//Hook a listener onto the iframe for changes and send the changes to the content div for the post which wordpress puts into the page.
//We need to hook into onKeyUp to catch key strokes.
tinyMCE.activeEditor.onKeyUp.add(function (ed, l) {
jcommunicator.populatechange(ed,l);
});
//We need to hook into onNodeChange to get changes that aren't necessarily related to key strokes (like changing text color etc)
tinyMCE.activeEditor.onNodeChange.add(function (ed, l) {
jcommunicator.populatechange(ed,l);
});
window.tinyalreadyinit = true;
}
}
return false;
});
}
});