index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import React from 'react';
  2. import G6 from '@antv/g6';
  3. import { pick } from '@utils';
  4. import { MINIMAP_CONTAINER } from '@common/constants';
  5. import withGGEditorContext from '@common/context/GGEditorContext/withGGEditorContext';
  6. require('@antv/g6/build/plugin.tool.minimap');
  7. const { Minimap: G6Minimap } = G6.Components;
  8. class Minimap extends React.Component {
  9. minimap = null;
  10. get containerId() {
  11. const { editor } = this.props;
  12. return `${MINIMAP_CONTAINER}_${editor.id}`;
  13. }
  14. get currentPage() {
  15. const { editor } = this.props;
  16. return editor.getCurrentPage();
  17. }
  18. constructor(props) {
  19. super(props);
  20. this.bindEvent();
  21. }
  22. componentDidMount() {
  23. this.init();
  24. this.bindPage();
  25. }
  26. init() {
  27. const {
  28. container = this.containerId,
  29. width,
  30. height,
  31. viewportWindowStyle,
  32. viewportBackStyle,
  33. } = this.props;
  34. const { clientWidth, clientHeight } = document.getElementById(container);
  35. this.minimap = new G6Minimap({
  36. container,
  37. width: width || clientWidth,
  38. height: height || clientHeight,
  39. viewportWindowStyle,
  40. viewportBackStyle,
  41. });
  42. this.minimap.getGraph = () => this.currentPage.getGraph();
  43. }
  44. bindPage() {
  45. if (!this.minimap || !this.currentPage) {
  46. return;
  47. }
  48. const graph = this.currentPage.getGraph();
  49. this.minimap.bindGraph(graph);
  50. this.minimap.debounceRender();
  51. }
  52. bindEvent() {
  53. const { onAfterAddPage } = this.props;
  54. onAfterAddPage(() => {
  55. this.bindPage();
  56. });
  57. }
  58. render() {
  59. const { container } = this.props;
  60. if (container) {
  61. return null;
  62. }
  63. return <div id={this.containerId} {...pick(this.props, ['style', 'className'])} />;
  64. }
  65. }
  66. export default withGGEditorContext(Minimap);