animation.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. AnimationModel,
  20. AnimationModels,
  21. } from "@/api";
  22. import { inRevise } from "bill/utils";
  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 createAnimationModel = (
  32. am: Partial<AnimationModel> = {}
  33. ): AnimationModel => ({
  34. id: createTemploraryID(),
  35. title: `模型`,
  36. url: "",
  37. showTitle: true,
  38. fontSize: 12,
  39. globalVisibility: true,
  40. visibilityRange: 12,
  41. subtitles: [],
  42. actions: [],
  43. frames: [],
  44. paths: [],
  45. ...am,
  46. });
  47. let bcAms: AnimationModels = [];
  48. export const getBackupAnimationModels = () => bcAms;
  49. export const backupAnimationModels = () => {
  50. bcAms = JSON.parse(JSON.stringify(ams.value))
  51. };
  52. export const addAnimationModel = addStoreItem(ams, postInsertAnimationModel);
  53. export const updateAnimationModels = updateStoreItem(
  54. ams,
  55. postUpdateAnimationModel
  56. );
  57. export const deleteAnimationModel = deleteStoreItem(ams, ({ id }) =>
  58. postDeleteAnimationModel(id)
  59. );
  60. export const initialAnimationModels = async () => {
  61. ams.value = await fetchAnimationModels();
  62. backupAnimationModels();
  63. };
  64. export const recoverAnimationModels = recoverStoreItems(
  65. ams,
  66. getBackupAnimationModels
  67. );
  68. export const saveAnimationModels = saveStoreItems(
  69. ams,
  70. getBackupAnimationModels,
  71. {
  72. add: addAnimationModel,
  73. update: updateAnimationModels,
  74. delete: deleteAnimationModel,
  75. }
  76. );
  77. export const autoSaveAnimationModel = autoSetModeCallback([ams], {
  78. backup: togetherCallback([backupAnimationModels]),
  79. recovery: togetherCallback([recoverAnimationModels]),
  80. isUpdate: (newv) => {
  81. return inRevise(newv, getBackupAnimationModels())
  82. },
  83. save: async () => {
  84. console.error('savesave')
  85. await saveAnimationModels();
  86. },
  87. });