autoheight.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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)return;
  14. var bakOverflow,
  15. span, tmpNode,
  16. lastHeight = 0,
  17. currentHeight,
  18. timer;
  19. function adjustHeight() {
  20. clearTimeout(timer);
  21. timer = setTimeout(function () {
  22. if (me.queryCommandState('source') != 1) {
  23. if (!span) {
  24. span = me.document.createElement('span');
  25. //trace:1764
  26. span.style.cssText = 'display:block;width:0;margin:0;padding:0;border:0;clear:both;';
  27. span.innerHTML = '.';
  28. }
  29. tmpNode = span.cloneNode(true);
  30. me.body.appendChild(tmpNode);
  31. currentHeight = Math.max(domUtils.getXY(tmpNode).y + tmpNode.offsetHeight, me.options.minFrameHeight);
  32. if (currentHeight != lastHeight) {
  33. me.setHeight(currentHeight);
  34. lastHeight = currentHeight;
  35. }
  36. domUtils.remove(tmpNode);
  37. }
  38. }, 50)
  39. }
  40. me.addListener('destroy', function () {
  41. me.removeListener('contentchange', adjustHeight);
  42. me.removeListener('keyup', adjustHeight);
  43. me.removeListener('mouseup', adjustHeight);
  44. });
  45. me.enableAutoHeight = function () {
  46. if(!me.autoHeightEnabled)return;
  47. var doc = me.document;
  48. me.autoHeightEnabled = true;
  49. bakOverflow = doc.body.style.overflowY;
  50. doc.body.style.overflowY = 'hidden';
  51. me.addListener('contentchange', adjustHeight);
  52. me.addListener('keyup', adjustHeight);
  53. me.addListener('mouseup', adjustHeight);
  54. //ff不给事件算得不对
  55. setTimeout(function () {
  56. adjustHeight();
  57. }, browser.gecko ? 100 : 0);
  58. me.fireEvent('autoheightchanged', me.autoHeightEnabled);
  59. };
  60. me.disableAutoHeight = function () {
  61. me.body.style.overflowY = bakOverflow || '';
  62. me.removeListener('contentchange', adjustHeight);
  63. me.removeListener('keyup', adjustHeight);
  64. me.removeListener('mouseup', adjustHeight);
  65. me.autoHeightEnabled = false;
  66. me.fireEvent('autoheightchanged', me.autoHeightEnabled);
  67. };
  68. me.addListener('ready', function () {
  69. me.enableAutoHeight();
  70. //trace:1764
  71. var timer;
  72. domUtils.on(browser.ie ? me.body : me.document,browser.webkit ? 'dragover' : 'drop',function(){
  73. clearTimeout(timer);
  74. timer = setTimeout(function(){
  75. adjustHeight()
  76. },100)
  77. });
  78. });
  79. };