xyyd-fatfox/src/layout/useLayoutStore.ts

69 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-09-09 17:53:07 +08:00
import { defineStore } from 'pinia'
import type { Layout } from './layout.types'
2024-09-11 16:03:17 +08:00
import { computed, reactive, ref, toRaw, watch } from 'vue'
2024-09-11 13:46:40 +08:00
import db from '@/db'
2024-09-13 18:27:39 +08:00
import useResource from './background/useResource'
2024-09-09 17:53:07 +08:00
const defaultLayout: Layout = {
2024-09-13 12:19:12 +08:00
content: [
{ background: '', pages: [] },
{ background: '', pages: [] },
{ background: '', pages: [] }
],
2024-09-09 17:53:07 +08:00
current: 0,
currentPage: 0,
dir: {},
2024-09-11 13:46:40 +08:00
dock: [null, null, null, null, null, null, null, null, null, null],
2024-09-11 16:03:17 +08:00
simple: false
2024-09-09 17:53:07 +08:00
}
export default defineStore('layout', () => {
2024-09-11 13:46:40 +08:00
const state = reactive(defaultLayout)
const ready = ref(false)
db.getItem<Layout>('layout').then((res) => {
if (res) {
Object.assign(state, res)
}
ready.value = true
})
watch(
[ready, state],
([re, s]) => {
if (!re) return
db.setItem('layout', toRaw(s))
},
{ deep: true }
)
watch(
() => state.current,
() => {
state.currentPage = 0
}
)
2024-09-13 12:19:12 +08:00
const currentMode = computed(() => state.content[state.current])
const currentPage = computed(() => {
return (
currentMode.value.pages[state.simple ? 0 : state.currentPage] || {
list: [],
label: '',
name: ''
}
)
})
const background = useResource(
computed(() => state.content[state.current]?.background || ''),
'background'
2024-09-11 16:03:17 +08:00
)
// 是让时间和搜索改变位置,使画面更紧凑 —— @/layout/grid
const isCompact = ref(false)
2024-09-09 17:53:07 +08:00
return {
2024-09-11 13:46:40 +08:00
state,
2024-09-11 16:03:17 +08:00
ready,
2024-09-13 12:19:12 +08:00
currentMode,
currentPage,
isCompact,
background
2024-09-09 17:53:07 +08:00
}
})