animation.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { ref } from "vue";
  2. import { autoSetModeCallback, createTemploraryID, isTemploraryID } 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. import { uuid } from "@/components/drawing/hook";
  24. import { ui18n } from "@/lang";
  25. export type {
  26. AnimationModelAction,
  27. AnimationModelFrame,
  28. AnimationModelPath,
  29. AnimationModelSubtitle,
  30. } from '@/api'
  31. export type { AnimationModel, AnimationModels };
  32. export const ams = ref<AnimationModels>([]);
  33. export const createAnimationModel = (
  34. am: Partial<AnimationModel> = {}
  35. ): AnimationModel => ({
  36. id: createTemploraryID(),
  37. title: ui18n.t("am.model"),
  38. url: "",
  39. showTitle: true,
  40. fontSize: 12,
  41. globalVisibility: true,
  42. visibilityRange: 12,
  43. key: uuid(),
  44. subtitles: [],
  45. actions: [],
  46. frames: [],
  47. paths: [],
  48. ...am,
  49. });
  50. let bcAms: AnimationModels = [];
  51. export const getBackupAnimationModels = () => bcAms;
  52. export const backupAnimationModels = () => {
  53. bcAms = JSON.parse(JSON.stringify(ams.value))
  54. };
  55. export const addAnimationModel = addStoreItem(ams, postInsertAnimationModel);
  56. export const updateAnimationModels = updateStoreItem(
  57. ams,
  58. postUpdateAnimationModel
  59. );
  60. export const deleteAnimationModel = deleteStoreItem(ams, ({ id }) =>
  61. postDeleteAnimationModel(id)
  62. );
  63. export const initialAnimationModels = async () => {
  64. ams.value = await fetchAnimationModels();
  65. backupAnimationModels();
  66. };
  67. export const recoverAnimationModels = recoverStoreItems(
  68. ams,
  69. getBackupAnimationModels
  70. );
  71. export const saveAnimationModels = saveStoreItems(
  72. ams,
  73. getBackupAnimationModels,
  74. {
  75. add: addAnimationModel,
  76. update: updateAnimationModels,
  77. delete: deleteAnimationModel,
  78. }
  79. );
  80. export const autoSaveAnimationModel = autoSetModeCallback([ams], {
  81. backup: togetherCallback([backupAnimationModels]),
  82. recovery: togetherCallback([recoverAnimationModels]),
  83. isUpdate: (newv) => {
  84. return inRevise(newv, getBackupAnimationModels())
  85. },
  86. save: async () => {
  87. await saveAnimationModels();
  88. },
  89. });