plugin.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * This plugin will force TinyMCE to produce deprecated legacy output such as font elements, u elements, align
  11. * attributes and so forth. There are a few cases where these old items might be needed for example in email applications or with Flash
  12. *
  13. * However you should NOT use this plugin if you are building some system that produces web contents such as a CMS. All these elements are
  14. * not apart of the newer specifications for HTML and XHTML.
  15. */
  16. /*global tinymce:true */
  17. (function(tinymce) {
  18. // Override inline_styles setting to force TinyMCE to produce deprecated contents
  19. tinymce.on('AddEditor', function(e) {
  20. e.editor.settings.inline_styles = false;
  21. });
  22. tinymce.PluginManager.add('legacyoutput', function(editor) {
  23. editor.on('init', function() {
  24. var alignElements = 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img',
  25. fontSizes = tinymce.explode(editor.settings.font_size_style_values),
  26. schema = editor.schema;
  27. // Override some internal formats to produce legacy elements and attributes
  28. editor.formatter.register({
  29. // Change alignment formats to use the deprecated align attribute
  30. alignleft: {selector: alignElements, attributes: {align: 'left'}},
  31. aligncenter: {selector: alignElements, attributes: {align: 'center'}},
  32. alignright: {selector: alignElements, attributes: {align: 'right'}},
  33. alignjustify: {selector: alignElements, attributes: {align: 'justify'}},
  34. // Change the basic formatting elements to use deprecated element types
  35. bold: [
  36. {inline: 'b', remove: 'all'},
  37. {inline: 'strong', remove: 'all'},
  38. {inline: 'span', styles: {fontWeight: 'bold'}}
  39. ],
  40. italic: [
  41. {inline: 'i', remove: 'all'},
  42. {inline: 'em', remove: 'all'},
  43. {inline: 'span', styles: {fontStyle: 'italic'}}
  44. ],
  45. underline: [
  46. {inline: 'u', remove: 'all'},
  47. {inline: 'span', styles: {textDecoration: 'underline'}, exact: true}
  48. ],
  49. strikethrough: [
  50. {inline: 'strike', remove: 'all'},
  51. {inline: 'span', styles: {textDecoration: 'line-through'}, exact: true}
  52. ],
  53. // Change font size and font family to use the deprecated font element
  54. fontname: {inline: 'font', attributes: {face: '%value'}},
  55. fontsize: {
  56. inline: 'font',
  57. attributes: {
  58. size: function(vars) {
  59. return tinymce.inArray(fontSizes, vars.value) + 1;
  60. }
  61. }
  62. },
  63. // Setup font elements for colors as well
  64. forecolor: {inline: 'font', attributes: {color: '%value'}},
  65. hilitecolor: {inline: 'font', styles: {backgroundColor: '%value'}}
  66. });
  67. // Check that deprecated elements are allowed if not add them
  68. tinymce.each('b,i,u,strike'.split(','), function(name) {
  69. schema.addValidElements(name + '[*]');
  70. });
  71. // Add font element if it's missing
  72. if (!schema.getElementRule("font")) {
  73. schema.addValidElements("font[face|size|color|style]");
  74. }
  75. // Add the missing and depreacted align attribute for the serialization engine
  76. tinymce.each(alignElements.split(','), function(name) {
  77. var rule = schema.getElementRule(name);
  78. if (rule) {
  79. if (!rule.attributes.align) {
  80. rule.attributes.align = {};
  81. rule.attributesOrder.push('align');
  82. }
  83. }
  84. });
  85. // Listen for the onNodeChange event so that we can do special logic for the font size and font name drop boxes
  86. /*editor.on('NodeChange', function() {
  87. var fontElm, fontName, fontSize;
  88. // Find font element get it's name and size
  89. fontElm = editor.dom.getParent(editor.selection.getNode(), 'font');
  90. if (fontElm) {
  91. fontName = fontElm.face;
  92. fontSize = fontElm.size;
  93. }
  94. });*/
  95. });
  96. });
  97. })(tinymce);