1
0

autoheight.js 3.5 KB

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