guide-path.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { ref } from 'vue'
  2. import { autoSetModeCallback, createTemploraryID } from './sys'
  3. import {
  4. fetchGuidePaths,
  5. postAddGuidePath,
  6. postUpdateGuidePath,
  7. postDeleteGuidePath,
  8. uploadFile,
  9. } from '@/api'
  10. import {
  11. deleteStoreItem,
  12. addStoreItem,
  13. updateStoreItem,
  14. saveStoreItems,
  15. recoverStoreItems
  16. } from '@/utils'
  17. import type { GuidePath as SGuidePath } from '@/api'
  18. import type { Guide } from './guide'
  19. export type GuidePath = LocalMode<SGuidePath, 'cover'>
  20. export type GuidePaths = GuidePath[]
  21. export const guidePaths = ref<GuidePaths>([])
  22. export const getGuidePaths = (guide: Guide) =>
  23. guidePaths.value.filter(path => path.guideId === guide.id)
  24. export const createGuidePath = (path: Partial<GuidePath> = {}): GuidePath => ({
  25. id: createTemploraryID(),
  26. guideId: '',
  27. cover: '',
  28. time: 1,
  29. sort: 999,
  30. speed: 1,
  31. position: {x: 0, y: 0, z: 0},
  32. target: {x: 0, y: 0, z: 0},
  33. ...path
  34. })
  35. let bcPaths: GuidePaths = []
  36. export const getBackupGuidePaths = () => bcPaths
  37. export const backupGuidePaths = () => {
  38. bcPaths = guidePaths.value.map(path => ({ ...path }))
  39. }
  40. export const transformGuidePath = async (path: GuidePath): Promise<SGuidePath> => {
  41. const cover: string = await uploadFile(path.cover)
  42. return { ...path, cover }
  43. }
  44. export const recoverGuidePaths = recoverStoreItems(guidePaths, getBackupGuidePaths)
  45. export const addGuidePath = addStoreItem(guidePaths, postAddGuidePath, transformGuidePath)
  46. export const updateGuidePath = updateStoreItem(guidePaths, postUpdateGuidePath, transformGuidePath)
  47. export const deleteGuidePath = deleteStoreItem(guidePaths, path => postDeleteGuidePath(path.id))
  48. export const initialGuidePathsByGuide = async (guide: Guide) => {
  49. const paths = await fetchGuidePaths(guide.id)
  50. guidePaths.value = guidePaths.value
  51. .filter(path => path.guideId !== guide.id)
  52. .concat(paths)
  53. backupGuidePaths()
  54. }
  55. const _saveGuidePaths = saveStoreItems(
  56. guidePaths,
  57. getBackupGuidePaths,
  58. {
  59. add: addGuidePath,
  60. update: updateGuidePath,
  61. delete: deleteGuidePath,
  62. }
  63. )
  64. export const saveGuidePaths = async () => {
  65. await _saveGuidePaths()
  66. backupGuidePaths()
  67. const sortMaps = new Map<GuidePath['guideId'], number>()
  68. guidePaths.value.forEach((path) => {
  69. const sort = (sortMaps.get(path.guideId) || 0) + 1
  70. path.sort = sort
  71. sortMaps.set(path.guideId, sort)
  72. })
  73. console.log(guidePaths.value)
  74. await _saveGuidePaths()
  75. }
  76. export const autoSaveGuidePaths = autoSetModeCallback(guidePaths, {
  77. backup: backupGuidePaths,
  78. recovery: recoverGuidePaths,
  79. save: saveGuidePaths,
  80. })