CommonMarkBody.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. <template>
  2. <div
  3. class="mark-body-container tw-flex-auto tw-p-2 tw-relative"
  4. ref="dragContainer"
  5. >
  6. <div
  7. v-if="!store.currentTask"
  8. class="
  9. tw-text-center
  10. empty-task
  11. tw-flex tw-flex-col tw-place-items-center tw-justify-center
  12. "
  13. >
  14. <img src="./images/empty-task.png" />
  15. {{ store.message }}
  16. </div>
  17. <div
  18. v-else-if="store.isScanImage"
  19. :style="{ width: answerPaperScale }"
  20. :class="[`rotate-board-${rotateBoard}`]"
  21. >
  22. <div
  23. v-for="(item, index) in sliceImagesWithTrackList"
  24. :key="index"
  25. class="single-image-container"
  26. >
  27. <img
  28. :src="item.url"
  29. @click="(event) => innerMakeTrack(event, item)"
  30. draggable="false"
  31. @contextmenu="showBigImage"
  32. />
  33. <MarkDrawTrack
  34. :track-list="item.trackList"
  35. :special-tag-list="item.tagList"
  36. :slice-image-width="item.sliceImageWidth"
  37. :slice-image-height="item.sliceImageHeight"
  38. :dx="item.dx"
  39. :dy="item.dy"
  40. />
  41. <hr class="image-seperator" />
  42. </div>
  43. </div>
  44. <div v-else-if="store.isMultiMedia">
  45. <MultiMediaMarkBody />
  46. </div>
  47. <div v-else>impossible</div>
  48. <div v-if="markStatus" class="status-container">
  49. {{ markStatus }}
  50. <div class="double-triangle"></div>
  51. </div>
  52. <ZoomPaper v-if="store.isScanImage && store.currentTask" :store="store" />
  53. </div>
  54. <slot name="slot-cursor" />
  55. </template>
  56. <script setup lang="ts">
  57. import { onMounted, onUnmounted, reactive, watch, watchEffect } from "vue-demi";
  58. import { store } from "@/store/store";
  59. import MarkDrawTrack from "./MarkDrawTrack.vue";
  60. import type {
  61. MarkResult,
  62. MarkStore,
  63. SliceImage,
  64. SpecialTag,
  65. Track,
  66. } from "@/types";
  67. import { useTimers } from "@/setups/useTimers";
  68. import {
  69. getDataUrlForSliceConfig,
  70. getDataUrlForSplitConfig,
  71. loadImage,
  72. } from "@/utils/utils";
  73. import { dragImage } from "./use/draggable";
  74. import MultiMediaMarkBody from "./MultiMediaMarkBody.vue";
  75. import "viewerjs/dist/viewer.css";
  76. import Viewer from "viewerjs";
  77. import ZoomPaper from "@/components/ZoomPaper.vue";
  78. const props = defineProps<{
  79. useMarkResult?: boolean;
  80. makeTrack: Function;
  81. store: MarkStore; // 以前不是一个类型的store,现在变成一样的了,所以这个可以删掉了,并且可以优化这个组件的使用
  82. }>();
  83. const emit = defineEmits(["error"]);
  84. const { useMarkResult = false, makeTrack } = props;
  85. // start: 图片拖动。在轨迹模式下,仅当没有选择分数时可用。
  86. const { dragContainer } = dragImage();
  87. // end: 图片拖动
  88. const { addTimeout } = useTimers();
  89. let rotateBoard = $ref(0);
  90. // start: 缩略图定位
  91. watch(
  92. () => [store.minimapScrollToX, store.minimapScrollToY],
  93. () => {
  94. const container = document.querySelector(
  95. ".mark-body-container"
  96. ) as HTMLDivElement;
  97. addTimeout(() => {
  98. if (
  99. container &&
  100. typeof store.minimapScrollToX === "number" &&
  101. typeof store.minimapScrollToY === "number"
  102. ) {
  103. const { scrollWidth, scrollHeight } = container;
  104. container.scrollTo({
  105. top: scrollHeight * store.minimapScrollToY,
  106. left: scrollWidth * store.minimapScrollToX,
  107. behavior: "smooth",
  108. });
  109. }
  110. }, 10);
  111. }
  112. );
  113. // end: 缩略图定位
  114. // start: 快捷键定位
  115. const scrollContainerByKey = (e: KeyboardEvent) => {
  116. const container = document.querySelector(
  117. ".mark-body-container"
  118. ) as HTMLDivElement;
  119. if (e.key === "w") {
  120. container.scrollBy({ top: -100, behavior: "smooth" });
  121. } else if (e.key === "s") {
  122. container.scrollBy({ top: 100, behavior: "smooth" });
  123. } else if (e.key === "a") {
  124. container.scrollBy({ left: -100, behavior: "smooth" });
  125. } else if (e.key === "d") {
  126. container.scrollBy({ left: 100, behavior: "smooth" });
  127. }
  128. };
  129. onMounted(() => {
  130. document.addEventListener("keypress", scrollContainerByKey);
  131. });
  132. onUnmounted(() => {
  133. document.removeEventListener("keypress", scrollContainerByKey);
  134. });
  135. // end: 快捷键定位
  136. // start: 计算裁切图和裁切图上的分数轨迹和特殊标记轨迹
  137. let sliceImagesWithTrackList: Array<SliceImage> = reactive([]);
  138. let maxSliceWidth = 0; // 最大的裁切块宽度,图片容器以此为准
  139. let theFinalHeight = 0; // 最终宽度,用来定位轨迹在第几张图片,不包括image-seperator高度
  140. async function processSliceConfig() {
  141. let markResult = store.currentTask?.markResult as MarkResult;
  142. if (useMarkResult) {
  143. // check if have MarkResult for currentTask
  144. if (!markResult) return;
  145. }
  146. if (!store.currentTask) return;
  147. const images = [];
  148. // 必须要先加载一遍,把“选择整图”的宽高重置后,再算总高度
  149. for (const sliceConfig of store.currentTask.sliceConfig) {
  150. const url = store.currentTask.sliceUrls[sliceConfig.i - 1];
  151. const image = await loadImage(url);
  152. images[sliceConfig.i] = image;
  153. if (sliceConfig.w === 0 && sliceConfig.h === 0) {
  154. // 选择整图时,w/h 为0
  155. sliceConfig.w = image.naturalWidth;
  156. sliceConfig.h = image.naturalHeight;
  157. }
  158. }
  159. theFinalHeight = store.currentTask.sliceConfig
  160. .map((v) => v.h)
  161. .reduce((acc, v) => (acc += v));
  162. maxSliceWidth = Math.max(...store.currentTask.sliceConfig.map((v) => v.w));
  163. // 用来保存sliceImage在整个图片容器中(不包括image-seperator)的高度范围
  164. let accumTopHeight = 0;
  165. let accumBottomHeight = 0;
  166. const tempSliceImagesWithTrackList = [] as Array<SliceImage>;
  167. for (const sliceConfig of store.currentTask.sliceConfig) {
  168. accumBottomHeight += sliceConfig.h;
  169. const url = store.currentTask.sliceUrls[sliceConfig.i - 1];
  170. const indexInSliceUrls = sliceConfig.i;
  171. const image = images[sliceConfig.i];
  172. const dataUrl = await getDataUrlForSliceConfig(
  173. image,
  174. sliceConfig,
  175. maxSliceWidth,
  176. url
  177. );
  178. let trackLists = [] as Array<Track>;
  179. if (useMarkResult) {
  180. trackLists = markResult.trackList;
  181. } else {
  182. trackLists = store.currentTask.questionList
  183. .map((q) => q.trackList)
  184. .reduce((acc, t) => {
  185. acc = acc.concat(t);
  186. return acc;
  187. }, [] as Array<Track>);
  188. }
  189. const thisImageTrackList = trackLists.filter(
  190. (t) => t.offsetIndex === indexInSliceUrls
  191. );
  192. let tagLists = [] as Array<SpecialTag>;
  193. if (useMarkResult) {
  194. tagLists = markResult.specialTagList ?? [];
  195. } else {
  196. tagLists = store.currentTask.specialTagList ?? [];
  197. }
  198. const thisImageTagList = tagLists.filter(
  199. (t) => t.offsetIndex === indexInSliceUrls
  200. );
  201. const sliceImageRendered = await loadImage(dataUrl!);
  202. tempSliceImagesWithTrackList.push({
  203. url: dataUrl!,
  204. indexInSliceUrls: sliceConfig.i,
  205. // 通过positionY来定位是第几张slice的还原,并过滤出相应的track
  206. trackList: thisImageTrackList.filter(
  207. (t) =>
  208. t.positionY >= accumTopHeight / theFinalHeight &&
  209. t.positionY < accumBottomHeight / theFinalHeight
  210. ),
  211. tagList: thisImageTagList.filter(
  212. (t) =>
  213. t.positionY >= accumTopHeight / theFinalHeight &&
  214. t.positionY < accumBottomHeight / theFinalHeight
  215. ),
  216. // originalImageWidth: image.naturalWidth,
  217. // originalImageHeight: image.naturalHeight,
  218. sliceImageWidth: sliceImageRendered.naturalWidth,
  219. sliceImageHeight: sliceImageRendered.naturalHeight,
  220. dx: sliceConfig.x,
  221. dy: sliceConfig.y,
  222. accumTopHeight,
  223. effectiveWidth: sliceConfig.w,
  224. });
  225. accumTopHeight = accumBottomHeight;
  226. }
  227. // console.log("render: ", store.currentTask.secretNumber);
  228. rotateBoard = 1;
  229. setTimeout(() => {
  230. sliceImagesWithTrackList.splice(0);
  231. sliceImagesWithTrackList.push(...tempSliceImagesWithTrackList);
  232. setTimeout(() => {
  233. rotateBoard = 0;
  234. }, 300);
  235. }, 300);
  236. }
  237. async function processSplitConfig() {
  238. let markResult = store.currentTask?.markResult as MarkResult;
  239. if (useMarkResult) {
  240. // check if have MarkResult for currentTask
  241. if (!markResult) return;
  242. }
  243. if (!store.currentTask) return;
  244. const images = [];
  245. for (const url of store.currentTask.sliceUrls) {
  246. const image = await loadImage(url);
  247. images.push(image);
  248. }
  249. // 裁切块,可能是一块,两块,三块... [start, width ...] => [0, 0.3] | [0, 0.55, 0.45, 0.55] | [0, 0.35, 0.33, 0.35, 0.66, 0.35]
  250. const splitConfigPairs = store.setting.splitConfig
  251. .map((v, index, ary) => (index % 2 === 0 ? [v, ary[index + 1]] : false))
  252. .filter((v) => v) as unknown as Array<[number, number]>;
  253. // 最大的 splitConfig 的宽度
  254. const maxSplitConfig = Math.max(
  255. ...store.setting.splitConfig.filter((v, i) => i % 2)
  256. );
  257. maxSliceWidth =
  258. Math.max(...images.map((v) => v.naturalWidth)) * maxSplitConfig;
  259. theFinalHeight =
  260. splitConfigPairs.length *
  261. images.reduce((acc, v) => (acc += v.naturalHeight), 0);
  262. // 高度比宽度大的图片不裁切
  263. const imagesOfBiggerHeight = images.filter(
  264. (v) => v.naturalHeight > v.naturalWidth
  265. );
  266. if (imagesOfBiggerHeight.length > 0) {
  267. maxSliceWidth = Math.max(
  268. maxSliceWidth,
  269. ...imagesOfBiggerHeight.map((v) => v.naturalWidth)
  270. );
  271. // 不裁切的图剪切多加的高度
  272. theFinalHeight -=
  273. imagesOfBiggerHeight.map((v) => v.naturalHeight).reduce((p, c) => p + c) *
  274. (splitConfigPairs.length - 1);
  275. }
  276. let accumTopHeight = 0;
  277. let accumBottomHeight = 0;
  278. const tempSliceImagesWithTrackList = [] as Array<SliceImage>;
  279. for (const url of store.currentTask.sliceUrls) {
  280. for (const config of splitConfigPairs) {
  281. const indexInSliceUrls = store.currentTask.sliceUrls.indexOf(url) + 1;
  282. const image = images[indexInSliceUrls - 1];
  283. let shouldBreak = false;
  284. if (image.naturalHeight > image.naturalWidth) {
  285. config[0] = 0;
  286. config[1] = 1;
  287. shouldBreak = true;
  288. }
  289. accumBottomHeight += image.naturalHeight;
  290. const dataUrl = await getDataUrlForSplitConfig(
  291. image,
  292. config,
  293. maxSliceWidth,
  294. url
  295. );
  296. let trackLists = [] as Array<Track>;
  297. if (useMarkResult) {
  298. trackLists = markResult.trackList;
  299. } else {
  300. // 成绩查询 questionList 可能为空
  301. trackLists = (store.currentTask.questionList || [])
  302. .map((q) => q.trackList)
  303. .reduce((acc, t) => {
  304. acc = acc.concat(t);
  305. return acc;
  306. }, [] as Array<Track>);
  307. }
  308. const thisImageTrackList = trackLists.filter(
  309. (t) => t.offsetIndex === indexInSliceUrls
  310. );
  311. let tagLists = [] as Array<SpecialTag>;
  312. if (useMarkResult) {
  313. tagLists = markResult.specialTagList ?? [];
  314. } else {
  315. tagLists = store.currentTask.specialTagList ?? [];
  316. }
  317. const thisImageTagList = tagLists.filter(
  318. (t) => t.offsetIndex === indexInSliceUrls
  319. );
  320. const sliceImageRendered = await loadImage(dataUrl!);
  321. tempSliceImagesWithTrackList.push({
  322. url: dataUrl!,
  323. indexInSliceUrls: store.currentTask.sliceUrls.indexOf(url) + 1,
  324. trackList: thisImageTrackList.filter(
  325. (t) =>
  326. t.positionY >= accumTopHeight / theFinalHeight &&
  327. t.positionY < accumBottomHeight / theFinalHeight
  328. ),
  329. tagList: thisImageTagList.filter(
  330. (t) =>
  331. t.positionY >= accumTopHeight / theFinalHeight &&
  332. t.positionY < accumBottomHeight / theFinalHeight
  333. ),
  334. // originalImageWidth: image.naturalWidth,
  335. // originalImageHeight: image.naturalHeight,
  336. sliceImageWidth: sliceImageRendered.naturalWidth,
  337. sliceImageHeight: sliceImageRendered.naturalHeight,
  338. dx: image.naturalWidth * config[0],
  339. dy: 0,
  340. accumTopHeight,
  341. effectiveWidth: image.naturalWidth * config[1],
  342. });
  343. accumTopHeight = accumBottomHeight;
  344. // 如果本图高比宽大,不该裁切,则跳过多次裁切
  345. if (shouldBreak) {
  346. break;
  347. }
  348. }
  349. }
  350. rotateBoard = 1;
  351. addTimeout(() => {
  352. sliceImagesWithTrackList.splice(0);
  353. sliceImagesWithTrackList.push(...tempSliceImagesWithTrackList);
  354. addTimeout(() => {
  355. rotateBoard = 0;
  356. }, 300);
  357. }, 300);
  358. }
  359. // should not render twice at the same time
  360. let renderLock = false;
  361. const renderPaperAndMark = async () => {
  362. if (!store) return;
  363. if (!store.isScanImage) return;
  364. if (renderLock) {
  365. console.log("上个任务还未渲染完毕,稍等一秒再尝试渲染");
  366. await new Promise((res) => setTimeout(res, 1000));
  367. await renderPaperAndMark();
  368. return;
  369. }
  370. renderLock = true;
  371. // check if have MarkResult for currentTask
  372. let markResult = store.currentTask?.markResult;
  373. if ((useMarkResult && !markResult) || !store.currentTask) {
  374. renderLock = false;
  375. return;
  376. }
  377. try {
  378. store.globalMask = true;
  379. const hasSliceConfig = store.currentTask?.sliceConfig?.length;
  380. if (hasSliceConfig) {
  381. await processSliceConfig();
  382. } else {
  383. await processSplitConfig();
  384. }
  385. // 研究生考试需要停留在上次阅卷的位置,所以注释掉下面的代码
  386. // await new Promise((res) => setTimeout(res, 700));
  387. // const container = <HTMLDivElement>(
  388. // document.querySelector(".mark-body-container")
  389. // );
  390. // addTimeout(() => {
  391. // container?.scrollTo({
  392. // top: 0,
  393. // left: 0,
  394. // behavior: "smooth",
  395. // });
  396. // }, 10);
  397. } catch (error) {
  398. sliceImagesWithTrackList.splice(0);
  399. console.trace("render error ", error);
  400. // 图片加载出错,自动加载下一个任务
  401. emit("error");
  402. } finally {
  403. renderLock = false;
  404. store.globalMask = false;
  405. }
  406. };
  407. // watchEffect(renderPaperAndMark);
  408. // 在阻止渲染的情况下,watchEffect收集不到 store.currentTask 的依赖,会导致本组件不再更新
  409. watch(
  410. () => store.currentTask,
  411. () => renderPaperAndMark()
  412. );
  413. // end: 计算裁切图和裁切图上的分数轨迹和特殊标记轨迹
  414. // start: 放大缩小和之后的滚动
  415. let answerPaperScale = $computed(() => {
  416. // 放大、缩小不影响页面之前的滚动条定位
  417. let percentWidth = 0;
  418. let percentTop = 0;
  419. const container = document.querySelector(
  420. ".mark-body-container"
  421. ) as HTMLDivElement;
  422. if (container) {
  423. const { scrollLeft, scrollTop, scrollWidth, scrollHeight } = container;
  424. percentWidth = scrollLeft / scrollWidth;
  425. percentTop = scrollTop / scrollHeight;
  426. }
  427. addTimeout(() => {
  428. if (container) {
  429. const { scrollWidth, scrollHeight } = container;
  430. container.scrollTo({
  431. left: scrollWidth * percentWidth,
  432. top: scrollHeight * percentTop,
  433. });
  434. }
  435. }, 10);
  436. const scale = store.setting.uiSetting["answer.paper.scale"];
  437. return scale * 100 + "%";
  438. });
  439. // end: 放大缩小和之后的滚动
  440. // start: 显示评分状态和清除轨迹
  441. let markStatus = $ref("");
  442. if (useMarkResult) {
  443. watch(
  444. () => store.currentTask,
  445. () => {
  446. markStatus = store.getMarkStatus;
  447. }
  448. );
  449. // 清除分数轨迹
  450. watchEffect(() => {
  451. for (const track of store.removeScoreTracks) {
  452. for (const sliceImage of sliceImagesWithTrackList) {
  453. sliceImage.trackList = sliceImage.trackList.filter(
  454. (t) =>
  455. !(
  456. t.mainNumber === track.mainNumber &&
  457. t.subNumber === track.subNumber &&
  458. t.number === track.number
  459. )
  460. );
  461. }
  462. }
  463. // 清除后,删除,否则会影响下次切换
  464. store.removeScoreTracks.splice(0);
  465. });
  466. // 清除特殊标记轨迹
  467. watchEffect(() => {
  468. for (const sliceImage of sliceImagesWithTrackList) {
  469. sliceImage.tagList = sliceImage.tagList.filter((t) =>
  470. store.currentTask?.markResult.specialTagList.find(
  471. (st) =>
  472. st.offsetIndex === t.offsetIndex &&
  473. st.offsetX === t.offsetX &&
  474. st.offsetY === t.offsetY
  475. )
  476. );
  477. }
  478. if (store.currentTask?.markResult.specialTagList.length === 0) {
  479. for (const sliceImage of sliceImagesWithTrackList) {
  480. sliceImage.tagList = [];
  481. }
  482. }
  483. });
  484. }
  485. // end: 显示评分状态和清除轨迹
  486. // start: 评分
  487. const innerMakeTrack = (event: MouseEvent, item: SliceImage) => {
  488. makeTrack && makeTrack(event, item, maxSliceWidth, theFinalHeight);
  489. };
  490. // end: 评分
  491. // start: 显示大图,供查看和翻转
  492. const showBigImage = (event: MouseEvent) => {
  493. event.preventDefault();
  494. // console.log(event);
  495. let viewer: Viewer = null as unknown as Viewer;
  496. viewer && viewer.destroy();
  497. viewer = new Viewer(event.target as HTMLElement, {
  498. // inline: true,
  499. viewed() {
  500. viewer.zoomTo(1);
  501. },
  502. hidden() {
  503. viewer.destroy();
  504. },
  505. zIndex: 1000000,
  506. });
  507. viewer.show();
  508. };
  509. // end: 显示大图,供查看和翻转
  510. // onRenderTriggered(({ key, target, type }) => {
  511. // console.log({ key, target, type });
  512. // });
  513. </script>
  514. <style scoped>
  515. .mark-body-container {
  516. position: relative;
  517. min-height: calc(100vh - 56px);
  518. height: calc(100vh - 56px);
  519. overflow: auto;
  520. /* background-size: 8px 8px;
  521. background-image: linear-gradient(to right, #e7e7e7 4px, transparent 4px),
  522. linear-gradient(to bottom, transparent 4px, #e7e7e7 4px); */
  523. background-color: var(--app-container-bg-color);
  524. background-image: linear-gradient(45deg, #e0e0e0 25%, transparent 25%),
  525. linear-gradient(-45deg, #e0e0e0 25%, transparent 25%),
  526. linear-gradient(45deg, transparent 75%, #e0e0e0 75%),
  527. linear-gradient(-45deg, transparent 75%, #e0e0e0 75%);
  528. background-size: 20px 20px;
  529. background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
  530. transform: inherit;
  531. }
  532. .mark-body-container img {
  533. width: 100%;
  534. }
  535. .empty-task {
  536. width: calc(100%);
  537. height: calc(100%);
  538. font-size: 20px;
  539. overflow: hidden;
  540. background-color: var(--app-container-bg-color);
  541. }
  542. .empty-task img {
  543. display: block;
  544. width: 288px;
  545. height: 225px;
  546. clip-path: polygon(0 0, 0 80%, 100% 80%, 100% 0);
  547. }
  548. .single-image-container {
  549. position: relative;
  550. }
  551. .image-seperator {
  552. border: 2px solid transparent;
  553. }
  554. .status-container {
  555. position: fixed;
  556. top: 56px;
  557. right: 340px;
  558. color: white;
  559. pointer-events: none;
  560. font-size: var(--app-title-font-size);
  561. background-color: #ef7c78;
  562. width: 30px;
  563. height: 50px;
  564. text-align: center;
  565. z-index: 1000;
  566. }
  567. .double-triangle {
  568. background-color: #ef7c78;
  569. width: 30px;
  570. height: 6px;
  571. clip-path: polygon(0 0, 0 6px, 50% 0, 100% 0, 100% 6px, 50% 0);
  572. position: absolute;
  573. bottom: -5px;
  574. }
  575. @keyframes rotate {
  576. 0% {
  577. transform: rotateY(0deg);
  578. opacity: 1;
  579. }
  580. 50% {
  581. transform: rotateY(90deg);
  582. }
  583. 100% {
  584. transform: rotateY(0deg);
  585. opacity: 0;
  586. }
  587. }
  588. .rotate-board-1 {
  589. animation: rotate 0.6s ease-in-out;
  590. }
  591. </style>