index.tsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { Layout } from 'antd'
  2. import { useRef, forwardRef, useImperativeHandle, memo, useCallback, useEffect, useState } from 'react'
  3. import { usePathData } from 'router'
  4. import style from './style.module.scss'
  5. import boardFactory from './board'
  6. import DfSlider from './slider'
  7. import DfHeader from './header'
  8. import EShape from './eshape'
  9. import { getBoardById, BoardType, BoardData, getExample, BoardTypeDesc } from 'api'
  10. import { useRefersh } from 'hook'
  11. import fileDefault from './default.json'
  12. import type { Board } from './board'
  13. const { Header, Sider, Content } = Layout
  14. const DfBoard = memo(forwardRef(({ data: boardData }: { data: BoardData }, ref) => {
  15. const board = useRef<Board>()
  16. const createBoard = useCallback((dom: HTMLCanvasElement | null) => {
  17. if (dom) {
  18. console.log(boardData)
  19. const boardRef = boardFactory(boardData, dom)
  20. board.current = boardRef
  21. } else if (board.current) {
  22. board.current.destroy()
  23. }
  24. // eslint-disable-next-line react-hooks/exhaustive-deps
  25. }, [])
  26. useImperativeHandle(ref, () => board.current)
  27. return (
  28. <div className={style['df-board']}>
  29. <div className={style['df-board-content']}>
  30. <canvas ref={createBoard}></canvas>
  31. </div>
  32. </div>
  33. )
  34. }))
  35. export const DrawFile = () => {
  36. const refersh = useRefersh()
  37. const path = usePathData()
  38. const pathId = Number(path!.id)
  39. const caseId = Number(path!.caseId)
  40. const pathType = path!.type as BoardType
  41. const [boardData, setBoardData] = useState<BoardData>()
  42. const board = useRef<Board>()
  43. const pathRef = useRef(path)
  44. const updateBoard = useCallback((data: Board) => {
  45. if (data !== board.current) {
  46. board.current = data
  47. refersh()
  48. }
  49. }, [refersh])
  50. useEffect(() => {
  51. if (!boardData || pathId !== boardData.id) {
  52. if (pathId !== -1) {
  53. getBoardById({ id: pathId }).then((data) => setBoardData({ ...data.content, id: data.filesId! }))
  54. } else {
  55. getExample({ caseId }).then((data) => {
  56. fileDefault.floors[0].title.value = data.caseTitle + "案发" + BoardTypeDesc[pathType];
  57. setBoardData(fileDefault)
  58. })
  59. }
  60. }
  61. }, [pathId, boardData?.id, boardData, caseId])
  62. useEffect(() => {
  63. if (JSON.stringify(path) !== JSON.stringify(pathRef.current)) {
  64. location.reload()
  65. }
  66. }, [path])
  67. return (
  68. <Layout className={style['df-layout']}>
  69. <Header className={style['df-header']}>
  70. { board.current && <DfHeader board={ board.current } type={ pathType } /> }
  71. </Header>
  72. <Layout>
  73. <Sider className={style['df-sider']}>
  74. { board.current && <DfSlider board={ board.current } type={ pathType } caseId={ caseId } /> }
  75. </Sider>
  76. <Content className={style['def-content']}>
  77. { board.current && <EShape board={ board.current } /> }
  78. { pathId === boardData?.id && <DfBoard data={boardData} ref={updateBoard} /> }
  79. </Content>
  80. </Layout>
  81. </Layout>
  82. )
  83. }
  84. export default DrawFile