header.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { ArrowLeftOutlined, ArrowRightOutlined } from "@ant-design/icons"
  2. import { Button } from "antd"
  3. import { useNavigate, fillRoutePath, RoutePath, usePathData } from "router"
  4. import { saveAs } from 'utils'
  5. import style from './style.module.scss'
  6. import {
  7. useSelector,
  8. boardStatus,
  9. useDispatch,
  10. insertBoard,
  11. updateBoard,
  12. currentBoard,
  13. BoardTypeDesc,
  14. BoardType
  15. } from "store"
  16. import type { RefObject } from "react"
  17. import type { Board } from "./board"
  18. type HeaderProps = {
  19. board: RefObject<Board>
  20. type: BoardType
  21. }
  22. const Header = ({ board, type }: HeaderProps) => {
  23. const path = usePathData()
  24. const status = useSelector(boardStatus)
  25. const current = useSelector(currentBoard)
  26. const dispatch = useDispatch()
  27. const navigate = useNavigate()
  28. const exportPng = async () => {
  29. if (board.current) {
  30. const blob = await board.current.export()
  31. saveAs(blob, '现场图.png')
  32. }
  33. }
  34. const save = async () => {
  35. if (current.id === -1) {
  36. const data = await dispatch(insertBoard({ type, data: current })).unwrap()
  37. navigate(
  38. fillRoutePath(RoutePath.drawFile, { ...path as any, id: data.id.toString() }),
  39. { replace: true }
  40. )
  41. } else {
  42. dispatch(updateBoard({ id: current.id, data: current }))
  43. }
  44. }
  45. return (
  46. <>
  47. <div className={style['df-header-left']}>
  48. <span className={`${style['df-header-back']} icon`} onClick={() => navigate(-1)}>
  49. <ArrowLeftOutlined />
  50. 返回
  51. </span>
  52. </div>
  53. <h1 className={style['df-header-center']}>
  54. 创建{ BoardTypeDesc[type] }
  55. </h1>
  56. <div className={style['df-header-right']}>
  57. <div className={style['df-header-action']}>
  58. <ArrowLeftOutlined
  59. className={!status.canBack ? 'disabled': 'icon'}
  60. onClick={() => dispatch({ type: 'board/backoff' })}
  61. />
  62. <ArrowRightOutlined
  63. className={!status.canForward ? 'disabled': 'icon'}
  64. onClick={() => dispatch({ type: 'board/forward' })}
  65. />
  66. </div>
  67. <Button type="primary" size="middle" onClick={save}>保存</Button>
  68. <Button type="default" size="middle" onClick={exportPng}>导出</Button>
  69. </div>
  70. </>
  71. )
  72. }
  73. export default Header