|
1 | 1 | import { resolveResource } from '@tauri-apps/api/path' |
2 | 2 | import { nanoid } from 'nanoid' |
3 | 3 | import { defineStore } from 'pinia' |
4 | | -import { onMounted, ref } from 'vue' |
| 4 | +import { ref } from 'vue' |
5 | 5 |
|
6 | 6 | import { join } from '@/utils/path' |
7 | 7 |
|
@@ -37,33 +37,47 @@ export const useModelStore = defineStore('model', () => { |
37 | 37 | const motions = ref<MotionGroup>({}) |
38 | 38 | const expressions = ref<Expression[]>([]) |
39 | 39 |
|
40 | | - onMounted(async () => { |
41 | | - const modelsPath = await resolveResource('assets/models') |
| 40 | + const init = async () => { |
| 41 | + const presetModelsPath = await resolveResource('assets/models') |
42 | 42 |
|
43 | 43 | if (models.value.length === 0) { |
44 | 44 | const modes: ModelMode[] = ['standard', 'keyboard'] |
45 | 45 |
|
46 | 46 | for await (const mode of modes) { |
47 | | - const path = join(modelsPath, mode) |
48 | | - |
49 | 47 | models.value.push({ |
50 | | - id: nanoid(), |
51 | | - path, |
52 | 48 | mode, |
| 49 | + id: nanoid(), |
53 | 50 | isPreset: true, |
| 51 | + path: join(presetModelsPath, mode), |
54 | 52 | }) |
55 | 53 | } |
| 54 | + } else { |
| 55 | + models.value = models.value.map((item) => { |
| 56 | + const { isPreset, mode } = item |
| 57 | + |
| 58 | + if (!isPreset) return item |
| 59 | + |
| 60 | + return { |
| 61 | + ...item, |
| 62 | + path: join(presetModelsPath, mode), |
| 63 | + } |
| 64 | + }) |
56 | 65 | } |
57 | 66 |
|
58 | | - if (currentModel.value) return |
| 67 | + const matched = models.value.find(item => item.id === currentModel.value?.id) |
| 68 | + |
| 69 | + if (matched) { |
| 70 | + return currentModel.value = matched |
| 71 | + } |
59 | 72 |
|
60 | 73 | currentModel.value = models.value[0] |
61 | | - }) |
| 74 | + } |
62 | 75 |
|
63 | 76 | return { |
64 | 77 | models, |
65 | 78 | currentModel, |
66 | 79 | motions, |
67 | 80 | expressions, |
| 81 | + init, |
68 | 82 | } |
69 | 83 | }) |
0 commit comments