v1.0
只读模式 group, 分享 评论更多问题 博客标签总是存在一个
This commit is contained in:
44
public/tinymce/plugins/visualchars/plugin.js
Normal file → Executable file
44
public/tinymce/plugins/visualchars/plugin.js
Normal file → Executable file
@ -11,13 +11,49 @@
|
||||
/*global tinymce:true */
|
||||
|
||||
tinymce.PluginManager.add('visualchars', function(editor) {
|
||||
var state;
|
||||
var self = this, state;
|
||||
|
||||
function toggleVisualChars(addBookmark) {
|
||||
var node, nodeList, i, body = editor.getBody(), nodeValue, selection = editor.selection, div, bookmark;
|
||||
var charMap, visualCharsRegExp;
|
||||
|
||||
charMap = {
|
||||
'\u00a0': 'nbsp',
|
||||
'\u00ad': 'shy'
|
||||
};
|
||||
|
||||
function wrapCharWithSpan(value) {
|
||||
return '<span data-mce-bogus="1" class="mce-' + charMap[value] + '">' + value + '</span>';
|
||||
}
|
||||
|
||||
function compileCharMapToRegExp() {
|
||||
var key, regExp = '';
|
||||
|
||||
for (key in charMap) {
|
||||
regExp += key;
|
||||
}
|
||||
|
||||
return new RegExp('[' + regExp + ']', 'g');
|
||||
}
|
||||
|
||||
function compileCharMapToCssSelector() {
|
||||
var key, selector = '';
|
||||
|
||||
for (key in charMap) {
|
||||
if (selector) {
|
||||
selector += ',';
|
||||
}
|
||||
|
||||
selector += 'span.mce-' + charMap[key];
|
||||
}
|
||||
|
||||
return selector;
|
||||
}
|
||||
|
||||
state = !state;
|
||||
self.state = state;
|
||||
editor.fire('VisualChars', {state: state});
|
||||
visualCharsRegExp = compileCharMapToRegExp();
|
||||
|
||||
if (addBookmark) {
|
||||
bookmark = selection.getBookmark();
|
||||
@ -26,14 +62,14 @@ tinymce.PluginManager.add('visualchars', function(editor) {
|
||||
if (state) {
|
||||
nodeList = [];
|
||||
tinymce.walk(body, function(n) {
|
||||
if (n.nodeType == 3 && n.nodeValue && n.nodeValue.indexOf('\u00a0') != -1) {
|
||||
if (n.nodeType == 3 && n.nodeValue && visualCharsRegExp.test(n.nodeValue)) {
|
||||
nodeList.push(n);
|
||||
}
|
||||
}, 'childNodes');
|
||||
|
||||
for (i = 0; i < nodeList.length; i++) {
|
||||
nodeValue = nodeList[i].nodeValue;
|
||||
nodeValue = nodeValue.replace(/(\u00a0)/g, '<span data-mce-bogus="1" class="mce-nbsp">$1</span>');
|
||||
nodeValue = nodeValue.replace(visualCharsRegExp, wrapCharWithSpan);
|
||||
|
||||
div = editor.dom.create('div', null, nodeValue);
|
||||
while ((node = div.lastChild)) {
|
||||
@ -43,7 +79,7 @@ tinymce.PluginManager.add('visualchars', function(editor) {
|
||||
editor.dom.remove(nodeList[i]);
|
||||
}
|
||||
} else {
|
||||
nodeList = editor.dom.select('span.mce-nbsp', body);
|
||||
nodeList = editor.dom.select(compileCharMapToCssSelector(), body);
|
||||
|
||||
for (i = nodeList.length - 1; i >= 0; i--) {
|
||||
editor.dom.remove(nodeList[i], 1);
|
||||
|
2
public/tinymce/plugins/visualchars/plugin.min.js
vendored
Normal file → Executable file
2
public/tinymce/plugins/visualchars/plugin.min.js
vendored
Normal file → Executable file
@ -1 +1 @@
|
||||
tinymce.PluginManager.add("visualchars",function(e){function t(t){var n,i,o,a,s,l,c=e.getBody(),d=e.selection;if(r=!r,e.fire("VisualChars",{state:r}),t&&(l=d.getBookmark()),r)for(i=[],tinymce.walk(c,function(e){3==e.nodeType&&e.nodeValue&&-1!=e.nodeValue.indexOf(" ")&&i.push(e)},"childNodes"),o=0;o<i.length;o++){for(a=i[o].nodeValue,a=a.replace(/(\u00a0)/g,'<span data-mce-bogus="1" class="mce-nbsp">$1</span>'),s=e.dom.create("div",null,a);n=s.lastChild;)e.dom.insertAfter(n,i[o]);e.dom.remove(i[o])}else for(i=e.dom.select("span.mce-nbsp",c),o=i.length-1;o>=0;o--)e.dom.remove(i[o],1);d.moveToBookmark(l)}function n(){var t=this;e.on("VisualChars",function(e){t.active(e.state)})}var r;e.addCommand("mceVisualChars",t),e.addButton("visualchars",{title:"Show invisible characters",cmd:"mceVisualChars",onPostRender:n}),e.addMenuItem("visualchars",{text:"Show invisible characters",cmd:"mceVisualChars",onPostRender:n,selectable:!0,context:"view",prependToContext:!0}),e.on("beforegetcontent",function(e){r&&"raw"!=e.format&&!e.draft&&(r=!0,t(!1))})});
|
||||
tinymce.PluginManager.add("visualchars",function(a){function b(b){function c(a){return'<span data-mce-bogus="1" class="mce-'+n[a]+'">'+a+"</span>"}function f(){var a,b="";for(a in n)b+=a;return new RegExp("["+b+"]","g")}function g(){var a,b="";for(a in n)b&&(b+=","),b+="span.mce-"+n[a];return b}var h,i,j,k,l,m,n,o,p=a.getBody(),q=a.selection;if(n={"\xa0":"nbsp","\xad":"shy"},d=!d,e.state=d,a.fire("VisualChars",{state:d}),o=f(),b&&(m=q.getBookmark()),d)for(i=[],tinymce.walk(p,function(a){3==a.nodeType&&a.nodeValue&&o.test(a.nodeValue)&&i.push(a)},"childNodes"),j=0;j<i.length;j++){for(k=i[j].nodeValue,k=k.replace(o,c),l=a.dom.create("div",null,k);h=l.lastChild;)a.dom.insertAfter(h,i[j]);a.dom.remove(i[j])}else for(i=a.dom.select(g(),p),j=i.length-1;j>=0;j--)a.dom.remove(i[j],1);q.moveToBookmark(m)}function c(){var b=this;a.on("VisualChars",function(a){b.active(a.state)})}var d,e=this;a.addCommand("mceVisualChars",b),a.addButton("visualchars",{title:"Show invisible characters",cmd:"mceVisualChars",onPostRender:c}),a.addMenuItem("visualchars",{text:"Show invisible characters",cmd:"mceVisualChars",onPostRender:c,selectable:!0,context:"view",prependToContext:!0}),a.on("beforegetcontent",function(a){d&&"raw"!=a.format&&!a.draft&&(d=!0,b(!1))})});
|
Reference in New Issue
Block a user