/
folder.ts
59 lines (48 loc) · 1.55 KB
/
folder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { defineStore } from 'pinia'
import { watch } from 'vue'
import { useStorage } from '@vueuse/core'
import { useStoryStore } from './story'
export const useFolderStore = defineStore('folder', () => {
const openedFolders = useStorage<Map<string, boolean>>(
'_histoire-tree-state',
new Map(),
)
function getStringPath (path: Array<string>) {
return path.join('␜')
}
function toggleFolder (path: Array<string>, defaultToggleValue = true) {
const stringPath = getStringPath(path)
const currentValue = openedFolders.value.get(stringPath)
if (currentValue == null) {
setFolderOpen(stringPath, defaultToggleValue)
} else if (currentValue) {
setFolderOpen(stringPath, false)
} else {
setFolderOpen(stringPath, true)
}
}
function setFolderOpen (path: Array<string> | string, value: boolean) {
const stringPath = typeof path === 'string' ? path : getStringPath(path)
openedFolders.value.set(stringPath, value)
}
function isFolderOpened (path: Array<string>, defaultValue = false) {
const value = openedFolders.value.get(getStringPath(path))
if (value == null) {
return defaultValue
}
return value
}
function openFileFolders (path: Array<string>) {
for (let pathLength = 1; pathLength < path.length; pathLength++) {
setFolderOpen(path.slice(0, pathLength), true)
}
}
const storyStore = useStoryStore()
watch(() => storyStore.currentStory, (story) => {
openFileFolders(story.file.path)
})
return {
isFolderOpened,
toggleFolder,
}
})