PanelResizer.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import React, { PureComponent } from 'react';
  2. import { debounce, throttle } from 'lodash';
  3. import Draggable from 'react-draggable';
  4. import { PanelModel } from '../panel_model';
  5. interface Props {
  6. isEditing: boolean;
  7. render: (height: number | 'inherit') => JSX.Element;
  8. panel: PanelModel;
  9. }
  10. interface State {
  11. editorHeight: number;
  12. }
  13. export class PanelResizer extends PureComponent<Props, State> {
  14. initialHeight: number = Math.floor(document.documentElement.scrollHeight * 0.4);
  15. prevEditorHeight: number;
  16. debouncedChangeHeight: (height: number) => void;
  17. debouncedResizeDone: () => void;
  18. constructor(props) {
  19. super(props);
  20. const { panel } = this.props;
  21. this.state = {
  22. editorHeight: this.initialHeight,
  23. };
  24. this.debouncedChangeHeight = throttle(this.changeHeight, 20, { trailing: true });
  25. this.debouncedResizeDone = debounce(() => {
  26. panel.resizeDone();
  27. }, 200);
  28. }
  29. changeHeight = height => {
  30. this.prevEditorHeight = this.state.editorHeight;
  31. this.setState({
  32. editorHeight: height,
  33. });
  34. };
  35. onDrag = (evt, data) => {
  36. const newHeight = this.state.editorHeight + data.y;
  37. this.debouncedChangeHeight(newHeight);
  38. this.debouncedResizeDone();
  39. };
  40. render() {
  41. const { render, isEditing } = this.props;
  42. const { editorHeight } = this.state;
  43. return (
  44. <>
  45. {render(isEditing ? editorHeight : 'inherit')}
  46. {isEditing && (
  47. <div className="panel-editor-container__resizer">
  48. <Draggable axis="y" grid={[100, 1]} onDrag={this.onDrag} position={{ x: 0, y: 0 }}>
  49. <div className="panel-editor-resizer">
  50. <div className="panel-editor-resizer__handle" />
  51. </div>
  52. </Draggable>
  53. </div>
  54. )}
  55. </>
  56. );
  57. }
  58. }