import { ref } from "vue"; import { autoSetModeCallback, createTemploraryID } from "./sys"; import { fetchAnimationActions, fetchAnimationModels, postDeleteAnimationModel, postInsertAnimationModel, postUpdateAnimationModel, } from "@/api"; import { togetherCallback, deleteStoreItem, addStoreItem, updateStoreItem, saveStoreItems, recoverStoreItems, } from "@/utils"; import type { AnimationAction, AnimationModel, AnimationModels, } from "@/api"; export type { AnimationModelAction, AnimationModelFrame, AnimationModelPath, AnimationModelSubtitle, } from '@/api' export type { AnimationModel, AnimationModels }; export const ams = ref([]); export const amActions = ref([]); export const initAnimationActions = async () => { amActions.value = await fetchAnimationActions(); }; export const createAnimationModel = ( am: Partial = {} ): AnimationModel => ({ id: createTemploraryID(), title: `模型`, url: "", showTitle: true, fontSize: 12, globalVisibility: true, visibilityRange: 12, subtitles: [], actions: [], frames: [], paths: [], ...am, }); let bcAms: AnimationModels = []; export const getBackupAnimationModels = () => bcAms; export const backupAnimationModels = () => { bcAms = ams.value.map((am) => ({ ...am })); }; export const addAnimationModel = addStoreItem(ams, postInsertAnimationModel); export const updateAnimationModels = updateStoreItem( ams, postUpdateAnimationModel ); export const deleteAnimationModel = deleteStoreItem(ams, ({ id }) => postDeleteAnimationModel(id) ); export const initialAnimationModels = async () => { ams.value = await fetchAnimationModels(); backupAnimationModels(); }; export const recoverAnimationModels = recoverStoreItems( ams, getBackupAnimationModels ); export const saveAnimationModels = saveStoreItems( ams, getBackupAnimationModels, { add: addAnimationModel, update: updateAnimationModels, delete: deleteAnimationModel, } ); export const autoSaveAnimationModel = autoSetModeCallback([ams], { backup: togetherCallback([backupAnimationModels]), recovery: togetherCallback([recoverAnimationModels]), save: async () => { await saveAnimationModels(); }, });