MarkBody.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <template>
  2. <CommonMarkBody
  3. :hasMarkResultToRender="true"
  4. :makeTrack="makeTrack"
  5. @error="$emit('error')"
  6. />
  7. <div class="cursor">
  8. <div class="cursor-border">
  9. <span class="text">{{
  10. store.currentSpecialTag || store.currentScore
  11. }}</span>
  12. </div>
  13. </div>
  14. <!-- <MarkBody /> -->
  15. </template>
  16. <script setup lang="ts">
  17. import { onMounted, onUnmounted, watch } from "vue";
  18. import { store } from "@/store/store";
  19. import { SliceImage, SpecialTag, Track } from "@/types";
  20. // @ts-ignore
  21. import CustomCursor from "custom-cursor.js";
  22. import CommonMarkBody from "./CommonMarkBody.vue";
  23. // import { message } from "ant-design-vue";
  24. // 开启本组件,测试后台在整卷的还原效果
  25. // import MarkBody from "@/features/student/studentInspect/MarkBody.vue";
  26. defineEmits(["error", "allZeroSubmit"]);
  27. const makeScoreTrack = (
  28. event: MouseEvent,
  29. item: SliceImage,
  30. maxSliceWidth: number,
  31. theFinalHeight: number
  32. ) => {
  33. // console.log(item);
  34. if (!store.currentQuestion || typeof store.currentScore === "undefined")
  35. return;
  36. const target = event.target as HTMLImageElement;
  37. const track = {} as Track;
  38. track.mainNumber = store.currentQuestion?.mainNumber;
  39. track.subNumber = store.currentQuestion?.subNumber;
  40. track.score = store.currentScore;
  41. track.offsetIndex = item.indexInSliceUrls;
  42. track.offsetX = Math.round(
  43. event.offsetX * (target.naturalWidth / target.width) + item.dx
  44. );
  45. track.offsetY = Math.round(
  46. event.offsetY * (target.naturalHeight / target.height) + item.dy
  47. );
  48. track.positionX = (track.offsetX - item.dx) / maxSliceWidth;
  49. track.positionY =
  50. (track.offsetY - item.dy + item.accumTopHeight) / theFinalHeight;
  51. // const isIllegalRange = (testNum: number, min: number, max: number) => {
  52. // return testNum < min || testNum > max;
  53. // };
  54. // // 检测有问题,此处没有给原图的宽高,如果有的话,要稍微修改下数据类型
  55. // // 但其实下面也做了一个基本检测
  56. // if (
  57. // isIllegalRange(track.offsetX, 0, target.naturalWidth) ||
  58. // isIllegalRange(track.offsetY, 0, target.naturalHeight) ||
  59. // isIllegalRange(track.positionX, 0, 1) ||
  60. // isIllegalRange(track.positionY, 0, 1)
  61. // ) {
  62. // console.error(
  63. // "错误的track",
  64. // track,
  65. // target.naturalWidth,
  66. // target.naturalHeight
  67. // );
  68. // void message.error("系统错误,请联系管理员!");
  69. // }
  70. if (track.offsetX > item.effectiveWidth + item.dx) {
  71. console.log("不在有效宽度内,轨迹不生效");
  72. return;
  73. }
  74. if (
  75. item.trackList.some((t) => {
  76. return (
  77. Math.pow(Math.abs(t.offsetX - track.offsetX), 2) +
  78. Math.pow(Math.abs(t.offsetY - track.offsetY), 2) <
  79. 500
  80. );
  81. })
  82. ) {
  83. console.log("两个轨迹相距过近");
  84. return;
  85. }
  86. // 是否保留当前的轨迹分
  87. const questionScore =
  88. store.currentTask &&
  89. store.currentQuestion &&
  90. store.currentTask.markResult.scoreList[store.currentQuestion.__index];
  91. const ifKeepScore =
  92. Math.round(
  93. store.currentQuestion.maxScore * 100 -
  94. (questionScore || 0) * 100 -
  95. store.currentScore * 2 * 100
  96. ) / 100;
  97. if (ifKeepScore < 0 && store.currentScore > 0) {
  98. store.currentScore = undefined;
  99. }
  100. const markResult = store.currentTaskEnsured.markResult;
  101. const maxNumber =
  102. markResult.trackList.length === 0
  103. ? 0
  104. : Math.max(...markResult.trackList.map((t) => t.number));
  105. track.number = maxNumber + 1;
  106. // console.log(
  107. // maxNumber,
  108. // track.number,
  109. // markResult.trackList.map((t) => t.number),
  110. // Math.max(...markResult.trackList.map((t) => t.number))
  111. // );
  112. markResult.trackList = [...markResult.trackList, track];
  113. const { __index, mainNumber, subNumber } = store.currentQuestion;
  114. markResult.scoreList[__index] =
  115. markResult.trackList
  116. .filter((t) => t.mainNumber === mainNumber && t.subNumber === subNumber)
  117. .map((t) => t.score)
  118. .reduce((acc, v) => (acc += Math.round(v * 100)), 0) / 100;
  119. item.trackList.push(track);
  120. };
  121. const makeSpecialTagTrack = (
  122. event: MouseEvent,
  123. item: SliceImage,
  124. maxSliceWidth: number,
  125. theFinalHeight: number
  126. ) => {
  127. // console.log(item);
  128. if (!store.currentTask || typeof store.currentSpecialTag === "undefined")
  129. return;
  130. const target = event.target as HTMLImageElement;
  131. const track = {} as SpecialTag;
  132. track.tagName = store.currentSpecialTag;
  133. track.offsetIndex = item.indexInSliceUrls;
  134. track.offsetX = Math.round(
  135. event.offsetX * (target.naturalWidth / target.width) + item.dx
  136. );
  137. track.offsetY = Math.round(
  138. event.offsetY * (target.naturalHeight / target.height) + item.dy
  139. );
  140. track.positionX = (track.offsetX - item.dx) / maxSliceWidth;
  141. track.positionY =
  142. (track.offsetY - item.dy + item.accumTopHeight) / theFinalHeight;
  143. // const isIllegalRange = (testNum: number, min: number, max: number) => {
  144. // return testNum < min || testNum > max;
  145. // };
  146. // if (
  147. // isIllegalRange(track.offsetX, 0, target.naturalWidth) ||
  148. // isIllegalRange(track.offsetY, 0, target.naturalHeight) ||
  149. // isIllegalRange(track.positionX, 0, 1) ||
  150. // isIllegalRange(track.positionY, 0, 1)
  151. // ) {
  152. // console.error("错误的track", track);
  153. // void message.error("系统错误,请联系管理员!");
  154. // }
  155. // if (track.offsetX > item.effectiveWidth + item.dx) {
  156. // console.log("不在有效宽度内,轨迹不生效");
  157. // return;
  158. // }
  159. if (
  160. item.tagList.some((t) => {
  161. return (
  162. Math.pow(Math.abs(t.offsetX - track.offsetX), 2) +
  163. Math.pow(Math.abs(t.offsetY - track.offsetY), 2) <
  164. 500
  165. );
  166. })
  167. ) {
  168. console.log("两个轨迹相距过近");
  169. return;
  170. }
  171. store.currentTaskEnsured.markResult.specialTagList.push(track);
  172. item.tagList.push(track);
  173. };
  174. const makeTrack = (
  175. event: MouseEvent,
  176. item: SliceImage,
  177. maxSliceWidth: number,
  178. theFinalHeight: number
  179. ) => {
  180. if (store.setting.uiSetting["specialTag.modal"] && store.currentSpecialTag) {
  181. makeSpecialTagTrack(event, item, maxSliceWidth, theFinalHeight);
  182. } else {
  183. makeScoreTrack(event, item, maxSliceWidth, theFinalHeight);
  184. }
  185. };
  186. watch(
  187. () => store.setting.mode,
  188. () => {
  189. const shouldHide = store.setting.mode === "COMMON";
  190. if (shouldHide) {
  191. // console.log("hide cursor", theCursor);
  192. // eslint-disable-next-line @typescript-eslint/no-unsafe-call
  193. theCursor && theCursor.destroy();
  194. } else {
  195. if (document.querySelector(".cursor")) {
  196. // console.log("show cursor", theCursor);
  197. // theCursor && theCursor.enable();
  198. // eslint-disable-next-line @typescript-eslint/no-unsafe-call
  199. theCursor = new CustomCursor(".cursor", {
  200. focusElements: [
  201. {
  202. selector: ".mark-body-container",
  203. focusClass: "cursor--focused-view",
  204. },
  205. ],
  206. }).initialize();
  207. }
  208. }
  209. }
  210. );
  211. let theCursor = null as any;
  212. onMounted(() => {
  213. if (store.isTrackMode) {
  214. // eslint-disable-next-line @typescript-eslint/no-unsafe-call
  215. theCursor = new CustomCursor(".cursor", {
  216. focusElements: [
  217. {
  218. selector: ".mark-body-container",
  219. focusClass: "cursor--focused-view",
  220. },
  221. ],
  222. }).initialize();
  223. }
  224. });
  225. onUnmounted(() => {
  226. // eslint-disable-next-line @typescript-eslint/no-unsafe-call
  227. theCursor && theCursor.destroy();
  228. });
  229. </script>
  230. <style scoped>
  231. .cursor {
  232. color: #ff5050;
  233. display: none;
  234. pointer-events: none;
  235. user-select: none;
  236. top: 0;
  237. left: 0;
  238. position: fixed;
  239. will-change: transform;
  240. z-index: 1000;
  241. }
  242. .cursor-border {
  243. position: absolute;
  244. box-sizing: border-box;
  245. align-items: center;
  246. border: 1px solid #ff5050;
  247. border-radius: 50%;
  248. display: flex;
  249. justify-content: center;
  250. height: 0px;
  251. width: 0px;
  252. left: 0;
  253. top: 0;
  254. transform: translate(-50%, -50%);
  255. transition: all 360ms cubic-bezier(0.23, 1, 0.32, 1);
  256. }
  257. .cursor.cursor--initialized {
  258. display: block;
  259. }
  260. .cursor .text {
  261. font-size: 2rem;
  262. opacity: 0;
  263. transition: opacity 80ms cubic-bezier(0.23, 1, 0.32, 1);
  264. }
  265. .cursor.cursor--off-screen {
  266. opacity: 0;
  267. }
  268. .cursor.cursor--focused .cursor-border,
  269. .cursor.cursor--focused-view .cursor-border {
  270. width: 90px;
  271. height: 90px;
  272. }
  273. .cursor.cursor--focused-view .text {
  274. opacity: 1;
  275. transition: opacity 360ms cubic-bezier(0.23, 1, 0.32, 1);
  276. }
  277. </style>