tools.tsx 2.2 KB

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