|
(function ($, w) { |
|
|
|
var editorSrc = '//cdn.ckeditor.com/4.4.7/full-all/ckeditor.js'; |
|
|
|
var ids = { |
|
summary: 'edit-body-und-0-value', |
|
comment: 'edit-nodechanges-comment-comment-body-und-0-value', |
|
commentEdit: 'edit-comment-body-und-0-value' |
|
}; |
|
|
|
/** |
|
* |
|
*/ |
|
function config() { |
|
|
|
// Buttons list: http://ckeditor.com/comment/123266#comment-123266 |
|
// Plugin presets: https://github.com/ckeditor/ckeditor-presets/tree/release/4.4.x/presets |
|
|
|
var allowedContent = "" |
|
+ "pre blockquote p;" |
|
+ "h2 h3 h4 h5 h6[name,id];" |
|
+ "ul ol li dl dt dd;" |
|
+ "table caption thead tbody tfoot tr;" |
|
+ "th td[colspan,rowspan];" |
|
+ "colgroup[span];" |
|
+ "em strong code del q cite sup sub br a b u i;" |
|
+ "a[!href,title];" |
|
+ "img[src,alt,title,width,height];" |
|
; |
|
|
|
var options = { |
|
enterMode: CKEDITOR.ENTER_BR, |
|
autoParagraph: true, |
|
fillEmptyBlocks: false |
|
// contentsCss: contentsCss() |
|
}; |
|
CKEDITOR.config.allowedContent = allowedContent; |
|
CKEDITOR.addCss('code { background-color: #f6f6f2; }'); |
|
CKEDITOR.addCss('pre { padding:5px; margin-bottom: .5em; background-color: #f6f6f2; border: 1px solid #CCC; }'); |
|
return options; |
|
} |
|
/** |
|
* |
|
*/ |
|
function searchBlocks(str, prefix, suffix, callback) { |
|
var match, depth = 0, from, ranges = [], i, r; |
|
var pattern = new RegExp('(' + escapeRegExp(prefix) + ')|(' + escapeRegExp(suffix) + ')', 'g'); |
|
while(match = pattern.exec(str)) { |
|
if(match[1] && !depth++) { |
|
from = match.index + prefix.length; |
|
} |
|
else if(match[2] && !--depth) { |
|
ranges.push({ from: from, to: match.index}) |
|
} |
|
} |
|
i = ranges.length; |
|
while(r = ranges[--i]) { |
|
str = str.slice(0, r.from) + callback(str.slice(r.from, r.to)) + str.slice(r.to); |
|
} |
|
return str; |
|
} |
|
/** |
|
* |
|
*/ |
|
function setOutputRules(writer) { |
|
var rules = {}; |
|
rules.breakAll = { |
|
indent: true, |
|
breakBeforeOpen: true, |
|
breakAfterOpen: true, |
|
breakBeforeClose: true, |
|
breakAfterClose: true |
|
}; |
|
rules.breakOuter = { |
|
indent: true, |
|
breakBeforeOpen: true, |
|
breakAfterOpen: false, |
|
breakBeforeClose: false, |
|
breakAfterClose: true |
|
}; |
|
writer.indentationChars = " "; |
|
|
|
writer.setRules("code", rules.blockOuter); |
|
} |
|
/** |
|
* |
|
*/ |
|
function load(callback) { |
|
if (w.CKEDITOR) { |
|
return callback(); |
|
} |
|
$.getScript(editorSrc, function () { |
|
CKEDITOR.on('instanceReady', function(e) { |
|
setOutputRules(e.editor.dataProcessor.writer); |
|
}); |
|
return callback(); |
|
}); |
|
} |
|
/** |
|
* |
|
*/ |
|
function contentsCss() { |
|
var elements = $('link[rel=stylesheet]').toArray(); |
|
var list = [], i = 0, el; |
|
while(el = elements[i++]) { |
|
list.push(el.href); |
|
} |
|
return list; |
|
} |
|
/** |
|
* |
|
*/ |
|
function escapeEntities(str) { |
|
return $('<div>').text(str).html(); |
|
} |
|
function unescapeEntities(str) { |
|
return $('<div>').html(str).text(); |
|
} |
|
function escapeRegExp(str) { |
|
return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); |
|
} |
|
/** |
|
* |
|
*/ |
|
function replace(name) { |
|
var id = ids[name]; |
|
var $area = $('#' + id); |
|
if(!$area.length) { |
|
return; |
|
} |
|
|
|
if(w.CKEDITOR && $area.data('ckeditor')) { |
|
$area.data('ckeditor').destroy(); |
|
$area.data('ckeditor', null); |
|
$area.val($('<div>').html($area.val()).find('pre > code').unwrap().end().html()); |
|
$area.val(searchBlocks($area.val(), '<code>', '</code>', unescapeEntities)); |
|
$area.prev('.bue-ui').show(); |
|
} |
|
else { |
|
load(function () { |
|
$area.prev('.bue-ui').hide(); |
|
$area.val(searchBlocks($area.val(), '<code>', '</code>', function(str) { |
|
return '<pre>' + escapeEntities(str) + '</pre>'; |
|
})); |
|
$area.data('ckeditor', CKEDITOR.replace(id, config())); |
|
}); |
|
} |
|
|
|
} |
|
|
|
D_ORG_CKEDITOR = { |
|
replace: replace |
|
}; |
|
|
|
}(jQuery, window)); |
|
//D_ORG_CKEDITOR.replace('summary'); |