HumanBodySvg.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import React, { useEffect, useState } from 'react';
  2. import { useDispatch, useSelector } from 'react-redux';
  3. import type { AppDispatch, RootState } from '@/states/store';
  4. import { fetchViewsOrProtocols } from '@/states/patient/viewSelection';
  5. import { setCurrentBodyPart } from '@/states/bodyPartSlice';
  6. // ✅ 独立 SVG 文件(不内联)
  7. // import { ReactComponent as HumanSvg } from '@/assets/imgs/VirtualHuman/human.svg';
  8. import HumanSvg from '@/assets/imgs/VirtualHuman/human.svg';
  9. interface HumanBodySvgProps {
  10. // 容器样式(覆盖默认的 .human-container)
  11. containerStyle?: React.CSSProperties;
  12. // SVG 样式(覆盖默认的 .human-svg)
  13. svgStyle?: React.CSSProperties;
  14. // 自定义类名(追加到默认类名)
  15. className?: string;
  16. // 完全自定义的内联样式
  17. style?: React.CSSProperties;
  18. }
  19. const HumanBody: React.FC<HumanBodySvgProps> = ({
  20. containerStyle,
  21. svgStyle,
  22. className,
  23. style
  24. }) => {
  25. const [activePart, setActivePart] = useState<string | null>(null);
  26. const dispatch = useDispatch<AppDispatch>();
  27. const currentPatientType = useSelector(
  28. (state: RootState) => state.patientType.current
  29. );
  30. const bodyPartsState = useSelector(
  31. (state: RootState) => state.bodyPart.byPatientType
  32. );
  33. const selected = useSelector(
  34. (state: RootState) => state.selection.selected
  35. );
  36. /**
  37. * SVG 统一点击入口(事件委托)
  38. */
  39. const handleSvgClick = (e: React.MouseEvent<SVGSVGElement>) => {
  40. const target = e.target as Element;
  41. let serverName;
  42. // 情况 1:点在 image 上(少数浏览器)
  43. if (target.tagName === 'image') {
  44. console.log((target as SVGImageElement).dataset.part);
  45. serverName= (target as SVGImageElement).dataset.part;
  46. }
  47. // 情况 2:点在 g 上
  48. if (target.tagName === 'g') {
  49. const image = target.querySelector('image[data-part]');
  50. if (image) {
  51. console.log((image as SVGImageElement).dataset.part);
  52. serverName= (image as SVGImageElement).dataset.part;
  53. }
  54. }
  55. if (!serverName) return;
  56. console.log('点击身体部位:', serverName);
  57. setActivePart(serverName);
  58. const selectedBodyPart =
  59. bodyPartsState.find(
  60. (item) => item.body_part_id === serverName
  61. ) || null;
  62. dispatch(setCurrentBodyPart(selectedBodyPart));
  63. dispatch(
  64. fetchViewsOrProtocols({
  65. selection: selected,
  66. patientType: currentPatientType?.patient_type_id ?? null,
  67. bodyPart: serverName,
  68. })
  69. );
  70. };
  71. /**
  72. * 控制 SVG 高亮状态
  73. * 不修改 SVG 文件本身
  74. */
  75. useEffect(() => {
  76. const svg = document.querySelector(
  77. '.human-svg'
  78. ) as SVGSVGElement | null;
  79. if (!svg) return;
  80. // 清空所有状态
  81. svg.querySelectorAll('[id]').forEach((el) => {
  82. el.classList.remove('active');
  83. el.classList.add('inactive');
  84. });
  85. if (activePart) {
  86. const activeEl = svg.querySelector(
  87. `#${CSS.escape(activePart)}`
  88. );
  89. activeEl?.classList.remove('inactive');
  90. activeEl?.classList.add('active');
  91. }
  92. }, [activePart]);
  93. return (
  94. <>
  95. {/* ===== 样式(集中在一个文件内)===== */}
  96. <style>
  97. {`
  98. .human-container {
  99. width: 294px;
  100. // height: 594px;
  101. user-select: none;
  102. }
  103. .human-svg {
  104. width: 100%;
  105. height: 100%;
  106. opacity: 0.95;
  107. }
  108. .human-svg g[data-part] {
  109. pointer-events: bounding-box;
  110. cursor: pointer;
  111. //transition: opacity 0.2s ease, filter 0.2s ease;
  112. outline: 1px solid red;
  113. }
  114. /* 默认状态 */
  115. // .human-svg image[data-part] {
  116. // cursor: pointer;
  117. // pointer-events: all;
  118. // // opacity: 0.35;
  119. // transition: opacity 0.2s ease, filter 0.2s ease;
  120. // outline: 1px solid red;
  121. // }
  122. /* hover */
  123. .human-svg [data-part]:hover {
  124. //opacity: 0.2;
  125. filter: drop-shadow(0 0 16px red);
  126. }
  127. /* 激活状态 */
  128. .human-svg .active {
  129. opacity: 1;
  130. filter: drop-shadow(0 0 6px #409eff);
  131. }
  132. /* 非激活 */
  133. .human-svg .inactive {
  134. opacity: 0.25;
  135. }
  136. `}
  137. </style>
  138. {/* ===== SVG 容器 ===== */}
  139. <div className="human-container" style={containerStyle}>
  140. <HumanSvg
  141. className="human-svg"
  142. style={svgStyle}
  143. onMouseDown={handleSvgClick}
  144. />
  145. </div>
  146. </>
  147. );
  148. }
  149. export default HumanBody;