index.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import ResizeObserver from 'resize-observer-polyfill';
  2. const isServer = typeof window === 'undefined';
  3. /* istanbul ignore next */
  4. function resizeHandler(entries: any[]) {
  5. for (const entry of entries) {
  6. const listeners = entry.target.__resizeListeners__ || [];
  7. if (listeners.length) {
  8. listeners.forEach((fn: () => any) => {
  9. fn();
  10. });
  11. }
  12. }
  13. }
  14. /* istanbul ignore next */
  15. export function addResizeListener(element: any, fn: () => any) {
  16. if (isServer) return;
  17. if (!element.__resizeListeners__) {
  18. element.__resizeListeners__ = [];
  19. element.__ro__ = new ResizeObserver(resizeHandler);
  20. element.__ro__.observe(element);
  21. }
  22. element.__resizeListeners__.push(fn);
  23. }
  24. /* istanbul ignore next */
  25. export function removeResizeListener(element: any, fn: () => any) {
  26. if (!element || !element.__resizeListeners__) return;
  27. element.__resizeListeners__.splice(element.__resizeListeners__.indexOf(fn), 1);
  28. if (!element.__resizeListeners__.length) {
  29. element.__ro__.disconnect();
  30. }
  31. }
  32. export function triggerWindowResize() {
  33. const event = document.createEvent('HTMLEvents');
  34. event.initEvent('resize', true, true);
  35. (event as any).eventType = 'message';
  36. window.dispatchEvent(event);
  37. }
  38. export function setDivHeight(e: MouseEvent, minHeight, scroll, scrollClientMaxHeight = 175, scrollClientHeight = 100) {
  39. if (e && e.target && e.target['className'] && e.target['className'].includes('input')) {
  40. return;
  41. }
  42. e.preventDefault();
  43. e.stopPropagation();
  44. const divObject = e.currentTarget as HTMLElement;
  45. if (divObject) {
  46. const divHeight = divObject.offsetHeight;
  47. let height = divHeight;
  48. const startY = e.clientY;
  49. let isMove = false;
  50. document.onmousemove = function (res) {
  51. res.preventDefault();
  52. isMove = true;
  53. const distY = Math.abs(res.clientY - startY);
  54. if (res.clientY > startY) {
  55. if (divHeight - distY > minHeight) {
  56. height = divHeight - distY;
  57. } else {
  58. height = minHeight;
  59. }
  60. }
  61. if (res.clientY < startY) {
  62. height = divHeight + distY;
  63. }
  64. if (height > document.body.clientHeight - scrollClientMaxHeight) {
  65. height = document.body.clientHeight - scrollClientMaxHeight;
  66. }
  67. divObject.style.height = height + 'px';
  68. };
  69. document.onmouseup = function () {
  70. document.onmousemove = null;
  71. if (isMove && scroll.y != height - scrollClientHeight) {
  72. scroll.y = height - scrollClientHeight - 20;
  73. }
  74. };
  75. }
  76. }