Hi,
I've been using CKEditor as my WYSIWYG of choice but have noticed only recently the "preview" button is very different to the Markitup equivalent. I've just skimmed through the CKEditor developers guide and could find no mention of editing the preview functionality to make it similar to the Markitup one (ie insert WYSIWYG content into the fuel preview template).
I don't imagine this discrepancy in functionality is an accident - but I thought I'd ask anyway!
I've seen a solution in the making though with this thread:
http://stackoverflow.com/questions/4401693/ckeditor-preview-method
Comments
if($ckEditor) ckAddPreview();
and then tacked on to the same script I added
function injectCKContents(w) { var wysiwyg = $ckEditor.val(); var target = $('#'+w).contents().find('body'); target.append(wysiwyg); } function ckAddPreview() { iframePreview = document.createElement("IFRAME"); var iframeId = "ckPreview"; iframePreview.setAttribute("id", iframeId); $('#vars--body_viewsource').parent().append(' <a class="btn_field editor_viewsource" onclick="injectCKContents(\''+iframeId+'\');">Preview</a>'); $('#vars--body_viewsource').parent().append(iframePreview); }
That achieves a button (Preview) after the "View Source" and then the iframe to inject the CK Editor content into when the new button is pressed. Rudimentary, and for proof of concept only!
An iframe mimics the preview of markItUp (and guarantees a complete DOM) - on balance better than a targeted DIV.
I'm not sure what would be the best next step, that is how to put the wysiwyg into the preview frame as per the main layout. DOM injection is quick, but targeting the correct parent element would be tricky given that this is up to the end user's whim. The other way is to call the main layout (or specified layout) and perform the same functionality as a controller, adding the wysiwyg as vars to the view.
But how is it achieved on the markItUp variant? Is the "fuel_preview" view used for this?
A nice option would be to enable a preview by width, so that the admin would know what would happen to the content in a mobile view, tablet or destkop.
previewParserPath: __FUEL_PATH__ + "/preview",
This points to the fuel/modules/fuel/controllers/preview.php controller file.
We'll put down the preview widths on our future wish list.
if($ckEditor) ckAddPreview();
in the add_edit function (as before), but altered the rest to:
function ckAddPreview() { // add a link, form & iframe // link submits form, posts to iframe target var formContainer = $("#main_content_inner"); var container = $('#vars--body_viewsource').parent(); var previewUrl = __FUEL_PATH__ + "/preview"; var iframeId = "ckPreview"; var iframePreview = document.createElement("IFRAME"); iframePreview.setAttribute("src", ""); iframePreview.setAttribute("id", iframeId); iframePreview.setAttribute("name", iframeId); var previewForm = document.createElement("FORM"); previewForm.setAttribute("action", previewUrl); previewForm.setAttribute("target", iframeId); previewForm.setAttribute("method", "post"); var dataId = "ck-data"; var dataInput = document.createElement("INPUT"); dataInput.setAttribute("type", "hidden"); dataInput.setAttribute("name", "data"); dataInput.setAttribute("id", dataId); previewForm.appendChild(dataInput); var linkId = "preview-link"; var previewLink = document.createElement("A"); previewLink.setAttribute("class", "btn_field editor_viewsource"); previewLink.setAttribute("id", linkId) var linkName = document.createTextNode("Preview"); previewLink.appendChild(linkName); // append new elements formContainer.append(previewForm); container.append(previewLink); container.append(iframePreview); $("#"+linkId).click(function() { var input = $("#"+dataId); input.val($ckEditor.val()); input.parent().submit(); }); }
This works nearly perfectly. The "nearly" being the currency of what is copied from the editor to the data input - it's not always the most recent edit from the form, often it's everything but the very last thing typed into CK editor. Perhaps there is a better way to capture the content than $ckEditor.val(). $ckEditor is defined in BaseFuelController.js, and seemed a handy shortcut. The toolbar preview in CKE achieves it though, so it can be done. I'll investigate further.
The new window is the better way.