plugin.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * plugin.js
  3. *
  4. * Copyright, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /*global tinymce:true */
  11. tinymce.PluginManager.add('visualchars', function(editor) {
  12. var self = this, state;
  13. function toggleVisualChars(addBookmark) {
  14. var node, nodeList, i, body = editor.getBody(), nodeValue, selection = editor.selection, div, bookmark;
  15. state = !state;
  16. self.state = state;
  17. editor.fire('VisualChars', {state: state});
  18. if (addBookmark) {
  19. bookmark = selection.getBookmark();
  20. }
  21. if (state) {
  22. nodeList = [];
  23. tinymce.walk(body, function(n) {
  24. if (n.nodeType == 3 && n.nodeValue && n.nodeValue.indexOf('\u00a0') != -1) {
  25. nodeList.push(n);
  26. }
  27. }, 'childNodes');
  28. for (i = 0; i < nodeList.length; i++) {
  29. nodeValue = nodeList[i].nodeValue;
  30. nodeValue = nodeValue.replace(/(\u00a0)/g, '<span data-mce-bogus="1" class="mce-nbsp">$1</span>');
  31. div = editor.dom.create('div', null, nodeValue);
  32. while ((node = div.lastChild)) {
  33. editor.dom.insertAfter(node, nodeList[i]);
  34. }
  35. editor.dom.remove(nodeList[i]);
  36. }
  37. } else {
  38. nodeList = editor.dom.select('span.mce-nbsp', body);
  39. for (i = nodeList.length - 1; i >= 0; i--) {
  40. editor.dom.remove(nodeList[i], 1);
  41. }
  42. }
  43. selection.moveToBookmark(bookmark);
  44. }
  45. function toggleActiveState() {
  46. var self = this;
  47. editor.on('VisualChars', function(e) {
  48. self.active(e.state);
  49. });
  50. }
  51. editor.addCommand('mceVisualChars', toggleVisualChars);
  52. editor.addButton('visualchars', {
  53. title: 'Show invisible characters',
  54. cmd: 'mceVisualChars',
  55. onPostRender: toggleActiveState
  56. });
  57. editor.addMenuItem('visualchars', {
  58. text: 'Show invisible characters',
  59. cmd: 'mceVisualChars',
  60. onPostRender: toggleActiveState,
  61. selectable: true,
  62. context: 'view',
  63. prependToContext: true
  64. });
  65. editor.on('beforegetcontent', function(e) {
  66. if (state && e.format != 'raw' && !e.draft) {
  67. state = true;
  68. toggleVisualChars(false);
  69. }
  70. });
  71. });