editor_plugin_src.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * editor_plugin_src.js
  3. *
  4. * Copyright 2009, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://tinymce.moxiecode.com/license
  8. * Contributing: http://tinymce.moxiecode.com/contributing
  9. */
  10. (function() {
  11. /**
  12. * Auto Resize
  13. *
  14. * This plugin automatically resizes the content area to fit its content height.
  15. * It will retain a minimum height, which is the height of the content area when
  16. * it's initialized.
  17. */
  18. tinymce.create('tinymce.plugins.AutoResizePlugin', {
  19. /**
  20. * Initializes the plugin, this will be executed after the plugin has been created.
  21. * This call is done before the editor instance has finished it's initialization so use the onInit event
  22. * of the editor instance to intercept that event.
  23. *
  24. * @param {tinymce.Editor} ed Editor instance that the plugin is initialized in.
  25. * @param {string} url Absolute URL to where the plugin is located.
  26. */
  27. init : function(ed, url) {
  28. var t = this, oldSize = 0;
  29. if (ed.getParam('fullscreen_is_enabled'))
  30. return;
  31. /**
  32. * This method gets executed each time the editor needs to resize.
  33. */
  34. function resize() {
  35. var deltaSize, d = ed.getDoc(), body = d.body, de = d.documentElement, DOM = tinymce.DOM, resizeHeight = t.autoresize_min_height, myHeight;
  36. // Get height differently depending on the browser used
  37. myHeight = tinymce.isIE ? body.scrollHeight : (tinymce.isWebKit && body.clientHeight == 0 ? 0 : body.offsetHeight);
  38. // Don't make it smaller than the minimum height
  39. if (myHeight > t.autoresize_min_height)
  40. resizeHeight = myHeight;
  41. // If a maximum height has been defined don't exceed this height
  42. if (t.autoresize_max_height && myHeight > t.autoresize_max_height) {
  43. resizeHeight = t.autoresize_max_height;
  44. body.style.overflowY = "auto";
  45. de.style.overflowY = "auto"; // Old IE
  46. } else {
  47. body.style.overflowY = "hidden";
  48. de.style.overflowY = "hidden"; // Old IE
  49. body.scrollTop = 0;
  50. }
  51. // Resize content element
  52. if (resizeHeight !== oldSize) {
  53. deltaSize = resizeHeight - oldSize;
  54. DOM.setStyle(DOM.get(ed.id + '_ifr'), 'height', resizeHeight + 'px');
  55. oldSize = resizeHeight;
  56. // WebKit doesn't decrease the size of the body element until the iframe gets resized
  57. // So we need to continue to resize the iframe down until the size gets fixed
  58. if (tinymce.isWebKit && deltaSize < 0)
  59. resize();
  60. }
  61. };
  62. t.editor = ed;
  63. // Define minimum height
  64. t.autoresize_min_height = parseInt(ed.getParam('autoresize_min_height', ed.getElement().offsetHeight));
  65. // Define maximum height
  66. t.autoresize_max_height = parseInt(ed.getParam('autoresize_max_height', 0));
  67. // Add padding at the bottom for better UX
  68. ed.onInit.add(function(ed){
  69. ed.dom.setStyle(ed.getBody(), 'paddingBottom', ed.getParam('autoresize_bottom_margin', 50) + 'px');
  70. });
  71. // Add appropriate listeners for resizing content area
  72. ed.onChange.add(resize);
  73. ed.onSetContent.add(resize);
  74. ed.onPaste.add(resize);
  75. ed.onKeyUp.add(resize);
  76. ed.onPostRender.add(resize);
  77. if (ed.getParam('autoresize_on_init', true)) {
  78. ed.onLoad.add(resize);
  79. ed.onLoadContent.add(resize);
  80. }
  81. // Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('mceExample');
  82. ed.addCommand('mceAutoResize', resize);
  83. },
  84. /**
  85. * Returns information about the plugin as a name/value array.
  86. * The current keys are longname, author, authorurl, infourl and version.
  87. *
  88. * @return {Object} Name/value array containing information about the plugin.
  89. */
  90. getInfo : function() {
  91. return {
  92. longname : 'Auto Resize',
  93. author : 'Moxiecode Systems AB',
  94. authorurl : 'http://tinymce.moxiecode.com',
  95. infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/autoresize',
  96. version : tinymce.majorVersion + "." + tinymce.minorVersion
  97. };
  98. }
  99. });
  100. // Register plugin
  101. tinymce.PluginManager.add('autoresize', tinymce.plugins.AutoResizePlugin);
  102. })();