useGraphic.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import {computed, reactive, Ref, ref, watch, watchEffect} from "vue";
  2. import { structureDraw } from "@/graphic";
  3. import VectorType from '@/graphic/enum/VectorType'
  4. import UIType from '@/graphic/enum/UIEvents'
  5. import type {RoadPhoto} from '@/store/roadPhotos'
  6. import type {AccidentPhoto} from '@/store/accidentPhotos'
  7. import {getStaticFile} from "@/dbo/main";
  8. export type UITypeT = typeof UIType
  9. export type VectorTypeT = typeof VectorType
  10. export type FocusVector = {
  11. type: VectorTypeT[keyof VectorTypeT],
  12. category?: VectorTypeT[keyof VectorTypeT],
  13. vectorId: string
  14. }
  15. const newsletter = ref<{
  16. selectUI?: UITypeT[keyof UITypeT]
  17. focusVector?: FocusVector
  18. }>({ selectUI: null, focusVector: null });
  19. const changeIndex = ref(0)
  20. export const changeStore = () => changeIndex.value++
  21. export const useChange = (fn: () => void) => {
  22. return watchEffect(() => {
  23. changeIndex.value;
  24. fn()
  25. })
  26. }
  27. export const graphicState = ref({
  28. canRevoke: false,
  29. canRecovery: false,
  30. showBackImage: true,
  31. canAngleLocationMode: false,
  32. canAllLocationMode: false
  33. })
  34. export const setCanvas = async (canvas: HTMLCanvasElement, data: Ref<AccidentPhoto | RoadPhoto>) => {
  35. await import("../graphic/index.js").then((model) => {
  36. drawRef.value = model.structureDraw(canvas, newsletter, graphicState);
  37. watch(
  38. () => data.value?.id,
  39. (id, oldId) => {
  40. if (data.value) {
  41. oldId && drawRef.value.load.clear()
  42. console.log("load", data.value)
  43. drawRef.value.load.load(data.value.data, {
  44. ...data.value.sceneData,
  45. backImage: getStaticFile(data.value.photoUrl)
  46. })
  47. } else {
  48. drawRef.value.load.clear()
  49. }
  50. },
  51. { immediate: true }
  52. )
  53. });
  54. };
  55. export const drawRef = ref<ReturnType<typeof structureDraw>>();
  56. export const uiType = reactive({
  57. current: computed(() => newsletter.value.selectUI),
  58. change: (type: UITypeT[keyof UITypeT]) => {
  59. drawRef.value.uiControl.selectUI = type
  60. }
  61. })
  62. export const currentVector = computed(() => newsletter.value.focusVector)
  63. export { VectorType, UIType }