includes/clientside/tinymce/plugins/visualchars/editor_plugin_src.js
changeset 335 67bd3121a12e
parent 1 fe660c52c48f
child 395 fa4c5ecb7c9a
equal deleted inserted replaced
334:c72b545f1304 335:67bd3121a12e
     1 /**
     1 /**
     2  * $Id: editor_plugin_src.js 42 2006-08-08 14:32:24Z spocke $
     2  * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
     3  *
     3  *
     4  * @author Moxiecode
     4  * @author Moxiecode
     5  * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
     5  * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
     6  */
     6  */
     7 
     7 
     8 /* Import plugin specific language pack */
     8 (function() {
     9 tinyMCE.importPluginLanguagePack('visualchars');
     9 	tinymce.create('tinymce.plugins.VisualChars', {
       
    10 		init : function(ed, url) {
       
    11 			var t = this;
    10 
    12 
    11 var TinyMCE_VisualCharsPlugin = {
    13 			t.editor = ed;
    12 	getInfo : function() {
       
    13 		return {
       
    14 			longname : 'Visual characters',
       
    15 			author : 'Moxiecode Systems AB',
       
    16 			authorurl : 'http://tinymce.moxiecode.com',
       
    17 			infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/visualchars',
       
    18 			version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
       
    19 		};
       
    20 	},
       
    21 
    14 
    22 	initInstance : function(inst) {
    15 			// Register commands
    23 		inst.visualChars = {
    16 			ed.addCommand('mceVisualChars', t._toggleVisualChars, t);
    24 			state : false
       
    25 		};
       
    26 	},
       
    27 
    17 
    28 	getControlHTML : function(cn) {
    18 			// Register buttons
    29 		switch (cn) {
    19 			ed.addButton('visualchars', {title : 'visualchars.desc', cmd : 'mceVisualChars'});
    30 			case "visualchars":
    20 
    31 				return tinyMCE.getButtonHTML(cn, 'lang_visualchars_desc', '{$pluginurl}/images/visualchars.gif', 'mceVisualChars', false);
    21 			ed.onBeforeGetContent.add(function(ed, o) {
       
    22 				if (t.state) {
       
    23 					t.state = true;
       
    24 					t._toggleVisualChars();
       
    25 				}
       
    26 			});
       
    27 		},
       
    28 
       
    29 		getInfo : function() {
       
    30 			return {
       
    31 				longname : 'Visual characters',
       
    32 				author : 'Moxiecode Systems AB',
       
    33 				authorurl : 'http://tinymce.moxiecode.com',
       
    34 				infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/visualchars',
       
    35 				version : tinymce.majorVersion + "." + tinymce.minorVersion
       
    36 			};
       
    37 		},
       
    38 
       
    39 		// Private methods
       
    40 
       
    41 		_toggleVisualChars : function() {
       
    42 			var t = this, ed = t.editor, nl, i, h, d = ed.getDoc(), b = ed.getBody(), nv, s = ed.selection, bo;
       
    43 
       
    44 			t.state = !t.state;
       
    45 			ed.controlManager.setActive('visualchars', t.state);
       
    46 
       
    47 			if (t.state) {
       
    48 				nl = [];
       
    49 				tinymce.walk(b, function(n) {
       
    50 					if (n.nodeType == 3 && n.nodeValue && n.nodeValue.indexOf('\u00a0') != -1)
       
    51 						nl.push(n);
       
    52 				}, 'childNodes');
       
    53 
       
    54 				for (i=0; i<nl.length; i++) {
       
    55 					nv = nl[i].nodeValue;
       
    56 					nv = nv.replace(/(\u00a0+)/g, '<span class="mceItemHiddenVisualChar">$1</span>');
       
    57 					nv = nv.replace(/\u00a0/g, '\u00b7');
       
    58 					ed.dom.setOuterHTML(nl[i], nv, d);
       
    59 				}
       
    60 			} else {
       
    61 				nl = tinymce.grep(ed.dom.select('span', b), function(n) {
       
    62 					return ed.dom.hasClass(n, 'mceItemHiddenVisualChar');
       
    63 				});
       
    64 
       
    65 				for (i=0; i<nl.length; i++)
       
    66 					ed.dom.setOuterHTML(nl[i], nl[i].innerHTML.replace(/(&middot;|\u00b7)/g, '&nbsp;'), d);
       
    67 			}
    32 		}
    68 		}
       
    69 	});
    33 
    70 
    34 		return "";
    71 	// Register plugin
    35 	},
    72 	tinymce.PluginManager.add('visualchars', tinymce.plugins.VisualChars);
    36 
    73 })();
    37 	execCommand : function(editor_id, element, command, user_interface, value) {
       
    38 		var inst = tinyMCE.getInstanceById(editor_id);
       
    39 
       
    40 		switch (command) {
       
    41 			case "mceVisualChars":
       
    42 				this._toggleVisualChars(editor_id, inst);
       
    43 				return true;
       
    44 		}
       
    45 
       
    46 		return false;
       
    47 	},
       
    48 
       
    49 	cleanup : function(type, content, inst) {
       
    50 		if (type == "insert_to_editor_dom" || type == "get_from_editor_dom") {
       
    51 			inst.visualChars.state = true;
       
    52 			this._toggleVisualChars(inst.editorId, inst);
       
    53 		}
       
    54 
       
    55 		return content;
       
    56 	},
       
    57 
       
    58 	// Private plugin internal methods
       
    59 
       
    60 	_toggleVisualChars : function(editor_id, inst) {
       
    61 		var nl, i, h, d = inst.getDoc(), b = inst.getBody(), nv, s = inst.selection, bo;
       
    62 
       
    63 		inst.visualChars.state = !inst.visualChars.state;
       
    64 
       
    65 		bo = s.getBookmark(true);
       
    66 
       
    67 		tinyMCE.switchClass(editor_id + '_visualchars', inst.visualChars.state ? 'mceButtonSelected' : 'mceButtonNormal');
       
    68 
       
    69 		if (inst.visualChars.state) {
       
    70 			nl = tinyMCE.selectNodes(b, function(n) {return n.nodeType == 3 && n.nodeValue && n.nodeValue.indexOf('\u00a0') != -1;});
       
    71 
       
    72 			for (i=0; i<nl.length; i++) {
       
    73 				nv = nl[i].nodeValue;
       
    74 				nv = nv.replace(/(\u00a0+)/g, '<span class="mceItemHiddenVisualChar">$1</span>');
       
    75 				nv = nv.replace(/\u00a0/g, '\u00b7');
       
    76 				tinyMCE.setOuterHTML(nl[i], nv, d);
       
    77 			}
       
    78 		} else {
       
    79 			nl = tinyMCE.selectNodes(b, function(n) {return n.nodeType == 1 && n.nodeName == 'SPAN' && n.className == 'mceItemHiddenVisualChar';});
       
    80 
       
    81 			for (i=0; i<nl.length; i++)
       
    82 				tinyMCE.setOuterHTML(nl[i], nl[i].innerHTML.replace(/(&middot;|\u00b7)/g, '&nbsp;'), d);
       
    83 		}
       
    84 
       
    85 		//s.moveToBookmark(bo);
       
    86 	}
       
    87 };
       
    88 
       
    89 tinyMCE.addPlugin("visualchars", TinyMCE_VisualCharsPlugin);