useGraphic.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {
  2. computed,
  3. nextTick,
  4. reactive,
  5. Ref,
  6. ref,
  7. watch,
  8. watchEffect,
  9. } from "vue";
  10. import { structureDraw } from "@/graphic";
  11. import VectorType from "@/graphic/enum/VectorType";
  12. import UIType from "@/graphic/enum/UIEvents";
  13. import type { RoadPhoto } from "@/store/roadPhotos";
  14. import type { AccidentPhoto } from "@/store/accidentPhotos";
  15. import { api } from "@/store/sync";
  16. import { genUseLoading } from "./useLoading.js";
  17. import mitt from "mitt";
  18. export const bus = mitt();
  19. export type UITypeT = typeof UIType;
  20. export type VectorTypeT = typeof VectorType;
  21. export type FocusVector = {
  22. type: VectorTypeT[keyof VectorTypeT];
  23. category?: VectorTypeT[keyof VectorTypeT];
  24. vectorId: string;
  25. };
  26. const newsletter = ref<{
  27. selectUI?: UITypeT[keyof UITypeT];
  28. focusVector?: FocusVector;
  29. }>({ selectUI: null, focusVector: null });
  30. const changeIndex = ref(0);
  31. export const changeStore = () => {
  32. changeIndex.value++;
  33. };
  34. export const useChange = (fn: () => void) => {
  35. let old;
  36. return watchEffect(() => {
  37. if (old !== changeIndex.value) {
  38. old = changeIndex.value;
  39. fn();
  40. }
  41. });
  42. };
  43. export const graphicState = ref({
  44. canRevoke: false,
  45. canRecovery: false,
  46. showBackImage: true,
  47. canAngleLocationMode: false,
  48. canVerticalMeasure: false,
  49. canAllLocationMode: false,
  50. existsBaseLine: false,
  51. continuedMode: false,
  52. });
  53. export const loadData = genUseLoading(
  54. async (data?: RoadPhoto, oldId?: RoadPhoto["id"]) => {
  55. if (data) {
  56. oldId && drawRef.value.load.clear();
  57. await drawRef.value.load.load(JSON.parse(JSON.stringify(data.data)), {
  58. ...data.sceneData,
  59. backImage: data.photoUrl,
  60. });
  61. bus.emit("graphicLoader");
  62. graphicState.value.canRecovery = false;
  63. graphicState.value.canRevoke = false;
  64. } else {
  65. drawRef.value.load.clear();
  66. }
  67. }
  68. );
  69. export const setCanvas = async (
  70. canvas: HTMLCanvasElement,
  71. data: Ref<AccidentPhoto | RoadPhoto>
  72. ) => {
  73. await import("../graphic/index.js").then((model) => {
  74. drawRef.value = model.structureDraw(canvas, newsletter, graphicState);
  75. watch(
  76. () => data.value?.id,
  77. (id, oldId) => {
  78. loadData(data.value, oldId);
  79. },
  80. { immediate: true, flush: "post" }
  81. );
  82. });
  83. };
  84. export const drawRef = ref<ReturnType<typeof structureDraw>>();
  85. export const uiType = reactive({
  86. current: computed(() => newsletter.value.selectUI),
  87. change: (type: UITypeT[keyof UITypeT]) => {
  88. drawRef.value.uiControl.selectUI = type;
  89. },
  90. });
  91. export const currentVector = computed(() => newsletter.value.focusVector);
  92. export { VectorType, UIType };