tools.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { CSSProperties, useEffect, useState } from 'react';
  2. import { usePlaygroundTools, useClientContext, LineType } from '@flowgram.ai/free-layout-editor';
  3. export const Tools = () => {
  4. const { history } = useClientContext();
  5. const tools = usePlaygroundTools();
  6. const [canUndo, setCanUndo] = useState(false);
  7. const [canRedo, setCanRedo] = useState(false);
  8. const buttonStyle: CSSProperties = {
  9. border: '1px solid #e0e0e0',
  10. borderRadius: '4px',
  11. cursor: 'pointer',
  12. padding: '4px',
  13. color: '#141414',
  14. background: '#e1e3e4',
  15. };
  16. useEffect(() => {
  17. const disposable = history.undoRedoService.onChange(() => {
  18. setCanUndo(history.canUndo());
  19. setCanRedo(history.canRedo());
  20. });
  21. return () => disposable.dispose();
  22. }, [history]);
  23. return (
  24. <div
  25. style={{ position: 'absolute', zIndex: 10, bottom: 34, right: 16, display: 'flex', gap: 8 }}
  26. >
  27. <button style={buttonStyle} onClick={() => tools.zoomin()}>
  28. ZoomIn
  29. </button>
  30. <button style={buttonStyle} onClick={() => tools.zoomout()}>
  31. ZoomOut
  32. </button>
  33. <span
  34. style={{
  35. ...buttonStyle,
  36. display: 'flex',
  37. alignItems: 'center',
  38. justifyContent: 'center',
  39. cursor: 'default',
  40. width: 40,
  41. }}
  42. >
  43. {Math.floor(tools.zoom * 100)}%
  44. </span>
  45. <button style={buttonStyle} onClick={() => tools.fitView()}>
  46. FitView
  47. </button>
  48. <button style={buttonStyle} onClick={() => tools.autoLayout()}>
  49. AutoLayout
  50. </button>
  51. <button
  52. style={buttonStyle}
  53. onClick={() =>
  54. tools.switchLineType(
  55. tools.lineType === LineType.BEZIER ? LineType.LINE_CHART : LineType.BEZIER
  56. )
  57. }
  58. >
  59. {tools.lineType === LineType.BEZIER ? 'Bezier' : 'Fold'}
  60. </button>
  61. <button
  62. style={{
  63. ...buttonStyle,
  64. cursor: canUndo ? 'pointer' : 'not-allowed',
  65. color: canUndo ? '#141414' : '#b1b1b1',
  66. }}
  67. onClick={() => history.undo()}
  68. disabled={!canUndo}
  69. >
  70. Undo
  71. </button>
  72. <button
  73. style={{
  74. ...buttonStyle,
  75. cursor: canRedo ? 'pointer' : 'not-allowed',
  76. color: canRedo ? '#141414' : '#b1b1b1',
  77. }}
  78. onClick={() => history.redo()}
  79. disabled={!canRedo}
  80. >
  81. Redo
  82. </button>
  83. </div>
  84. );
  85. };