xyyd-fatfox/src/layout/useLayoutStore.ts

50 lines
1.1 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-09 17:53:07 +08:00
const defaultLayout: Layout = {
content: [[], [], []],
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-11 16:03:17 +08:00
const currentPageList = computed(() =>
state.simple ? [] : state.content[state.current]?.[state.currentPage]?.list || []
)
// 是让时间和搜索改变位置,使画面更紧凑 —— @/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,
currentPageList,
isCompact
2024-09-09 17:53:07 +08:00
}
})