autoheight.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ///import core
  2. ///commands 当输入内容超过编辑器高度时,编辑器自动增高
  3. ///commandsName AutoHeight,autoHeightEnabled
  4. ///commandsTitle 自动增高
  5. /**
  6. * @description 自动伸展
  7. * @author zhanyi
  8. */
  9. UE.plugins['autoheight'] = function () {
  10. var me = this;
  11. //提供开关,就算加载也可以关闭
  12. me.autoHeightEnabled = me.options.autoHeightEnabled !== false;
  13. if (!me.autoHeightEnabled) {
  14. return;
  15. }
  16. var bakOverflow,
  17. span, tmpNode,
  18. lastHeight = 0,
  19. options = me.options,
  20. currentHeight,
  21. timer;
  22. function adjustHeight() {
  23. var me = this;
  24. clearTimeout(timer);
  25. if(isFullscreen)return;
  26. timer = setTimeout(function () {
  27. if (me.queryCommandState && me.queryCommandState('source') != 1) {
  28. if (!span) {
  29. span = me.document.createElement('span');
  30. //trace:1764
  31. span.style.cssText = 'display:block;width:0;margin:0;padding:0;border:0;clear:both;';
  32. span.innerHTML = '.';
  33. }
  34. tmpNode = span.cloneNode(true);
  35. me.body.appendChild(tmpNode);
  36. currentHeight = Math.max(domUtils.getXY(tmpNode).y + tmpNode.offsetHeight,Math.max(options.minFrameHeight, options.initialFrameHeight));
  37. if (currentHeight != lastHeight) {
  38. me.setHeight(currentHeight);
  39. lastHeight = currentHeight;
  40. }
  41. domUtils.remove(tmpNode);
  42. }
  43. }, 50);
  44. }
  45. var isFullscreen;
  46. me.addListener('fullscreenchanged',function(cmd,f){
  47. isFullscreen = f
  48. });
  49. me.addListener('destroy', function () {
  50. me.removeListener('contentchange', adjustHeight);
  51. me.removeListener('afterinserthtml',adjustHeight);
  52. me.removeListener('keyup', adjustHeight);
  53. me.removeListener('mouseup', adjustHeight);
  54. });
  55. me.enableAutoHeight = function () {
  56. if (!me.autoHeightEnabled) {
  57. return;
  58. }
  59. var doc = me.document;
  60. me.autoHeightEnabled = true;
  61. bakOverflow = doc.body.style.overflowY;
  62. doc.body.style.overflowY = 'hidden';
  63. me.addListener('contentchange', adjustHeight);
  64. me.addListener('afterinserthtml',adjustHeight)
  65. me.addListener('keyup', adjustHeight);
  66. me.addListener('mouseup', adjustHeight);
  67. //ff不给事件算得不对
  68. setTimeout(function () {
  69. adjustHeight.call(this);
  70. }, browser.gecko ? 100 : 0);
  71. me.fireEvent('autoheightchanged', me.autoHeightEnabled);
  72. };
  73. me.disableAutoHeight = function () {
  74. me.body.style.overflowY = bakOverflow || '';
  75. me.removeListener('contentchange', adjustHeight);
  76. me.removeListener('keyup', adjustHeight);
  77. me.removeListener('mouseup', adjustHeight);
  78. me.autoHeightEnabled = false;
  79. me.fireEvent('autoheightchanged', me.autoHeightEnabled);
  80. };
  81. me.addListener('ready', function () {
  82. me.enableAutoHeight();
  83. //trace:1764
  84. var timer;
  85. domUtils.on(browser.ie ? me.body : me.document, browser.webkit ? 'dragover' : 'drop', function () {
  86. clearTimeout(timer);
  87. timer = setTimeout(function () {
  88. adjustHeight.call(this);
  89. }, 100);
  90. });
  91. });
  92. };