autoheight.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. lastHeight = 0,
  18. options = me.options,
  19. currentHeight,
  20. timer;
  21. function adjustHeight() {
  22. var me = this;
  23. clearTimeout(timer);
  24. if(isFullscreen)return;
  25. if (!me.queryCommandState || me.queryCommandState && me.queryCommandState('source') != 1) {
  26. timer = setTimeout(function(){
  27. var node = me.body.lastChild;
  28. while(node && node.nodeType != 1){
  29. node = node.previousSibling;
  30. }
  31. if(node && node.nodeType == 1){
  32. node.style.clear = 'both';
  33. currentHeight = Math.max(domUtils.getXY(node).y + node.offsetHeight + 25 ,Math.max(options.minFrameHeight, options.initialFrameHeight)) ;
  34. if (currentHeight != lastHeight) {
  35. if (currentHeight !== parseInt(me.iframe.parentNode.style.height)) {
  36. me.iframe.parentNode.style.height = currentHeight + 'px';
  37. }
  38. me.body.style.height = currentHeight + 'px';
  39. lastHeight = currentHeight;
  40. }
  41. domUtils.removeStyle(node,'clear');
  42. }
  43. },50)
  44. }
  45. }
  46. var isFullscreen;
  47. me.addListener('fullscreenchanged',function(cmd,f){
  48. isFullscreen = f
  49. });
  50. me.addListener('destroy', function () {
  51. me.removeListener('contentchange afterinserthtml keyup mouseup',adjustHeight)
  52. });
  53. me.enableAutoHeight = function () {
  54. var me = this;
  55. if (!me.autoHeightEnabled) {
  56. return;
  57. }
  58. var doc = me.document;
  59. me.autoHeightEnabled = true;
  60. bakOverflow = doc.body.style.overflowY;
  61. doc.body.style.overflowY = 'hidden';
  62. me.addListener('contentchange afterinserthtml keyup mouseup',adjustHeight);
  63. //ff不给事件算得不对
  64. setTimeout(function () {
  65. adjustHeight.call(me);
  66. }, browser.gecko ? 100 : 0);
  67. me.fireEvent('autoheightchanged', me.autoHeightEnabled);
  68. };
  69. me.disableAutoHeight = function () {
  70. me.body.style.overflowY = bakOverflow || '';
  71. me.removeListener('contentchange', adjustHeight);
  72. me.removeListener('keyup', adjustHeight);
  73. me.removeListener('mouseup', adjustHeight);
  74. me.autoHeightEnabled = false;
  75. me.fireEvent('autoheightchanged', me.autoHeightEnabled);
  76. };
  77. me.on('setHeight',function(){
  78. me.disableAutoHeight()
  79. });
  80. me.addListener('ready', function () {
  81. me.enableAutoHeight();
  82. //trace:1764
  83. var timer;
  84. domUtils.on(browser.ie ? me.body : me.document, browser.webkit ? 'dragover' : 'drop', function () {
  85. clearTimeout(timer);
  86. timer = setTimeout(function () {
  87. //trace:3681
  88. adjustHeight.call(me);
  89. }, 100);
  90. });
  91. //修复内容过多时,回到顶部,顶部内容被工具栏遮挡问题
  92. var lastScrollY;
  93. window.onscroll = function(){
  94. if(lastScrollY === null){
  95. lastScrollY = this.scrollY
  96. }else if(this.scrollY == 0 && lastScrollY != 0){
  97. me.window.scrollTo(0,0);
  98. lastScrollY = null;
  99. }
  100. }
  101. });
  102. };