animation.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { ref } from "vue";
  2. import { autoSetModeCallback, createTemploraryID } from "./sys";
  3. import {
  4. fetchAnimationActions,
  5. fetchAnimationModels,
  6. postDeleteAnimationModel,
  7. postInsertAnimationModel,
  8. postUpdateAnimationModel,
  9. } from "@/api";
  10. import {
  11. togetherCallback,
  12. deleteStoreItem,
  13. addStoreItem,
  14. updateStoreItem,
  15. saveStoreItems,
  16. recoverStoreItems,
  17. } from "@/utils";
  18. import type {
  19. AnimationAction,
  20. AnimationModel,
  21. AnimationModels,
  22. } from "@/api";
  23. export type {
  24. AnimationModelAction,
  25. AnimationModelFrame,
  26. AnimationModelPath,
  27. AnimationModelSubtitle,
  28. } from '@/api'
  29. export type { AnimationModel, AnimationModels };
  30. export const ams = ref<AnimationModels>([]);
  31. export const amActions = ref<AnimationAction[]>([]);
  32. export const initAnimationActions = async () => {
  33. amActions.value = await fetchAnimationActions();
  34. };
  35. export const createAnimationModel = (
  36. am: Partial<AnimationModel> = {}
  37. ): AnimationModel => ({
  38. id: createTemploraryID(),
  39. title: `模型`,
  40. url: "",
  41. showTitle: true,
  42. fontSize: 12,
  43. globalVisibility: true,
  44. visibilityRange: 12,
  45. subtitles: [],
  46. actions: [],
  47. frames: [],
  48. paths: [],
  49. ...am,
  50. });
  51. let bcAms: AnimationModels = [];
  52. export const getBackupAnimationModels = () => bcAms;
  53. export const backupAnimationModels = () => {
  54. bcAms = ams.value.map((am) => ({ ...am }));
  55. };
  56. export const addAnimationModel = addStoreItem(ams, postInsertAnimationModel);
  57. export const updateAnimationModels = updateStoreItem(
  58. ams,
  59. postUpdateAnimationModel
  60. );
  61. export const deleteAnimationModel = deleteStoreItem(ams, ({ id }) =>
  62. postDeleteAnimationModel(id)
  63. );
  64. export const initialAnimationModels = async () => {
  65. ams.value = await fetchAnimationModels();
  66. backupAnimationModels();
  67. };
  68. export const recoverAnimationModels = recoverStoreItems(
  69. ams,
  70. getBackupAnimationModels
  71. );
  72. export const saveAnimationModels = saveStoreItems(
  73. ams,
  74. getBackupAnimationModels,
  75. {
  76. add: addAnimationModel,
  77. update: updateAnimationModels,
  78. delete: deleteAnimationModel,
  79. }
  80. );
  81. export const autoSaveAnimationModel = autoSetModeCallback([ams], {
  82. backup: togetherCallback([backupAnimationModels]),
  83. recovery: togetherCallback([recoverAnimationModels]),
  84. save: async () => {
  85. await saveAnimationModels();
  86. },
  87. });