2024-09-09 17:53:07 +08:00
|
|
|
import { defineStore } from 'pinia'
|
2024-09-18 16:27:43 +08:00
|
|
|
import type { Block, 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-18 16:27:43 +08:00
|
|
|
import { globalToast } from '@/main'
|
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-25 14:25:44 +08:00
|
|
|
dockLabels: 'QWERASDFGB',
|
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
|
2024-09-25 18:23:54 +08:00
|
|
|
console.log(toRaw(s))
|
2024-09-11 13:46:40 +08:00
|
|
|
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-18 16:27:43 +08:00
|
|
|
|
|
|
|
// 添加图标
|
|
|
|
const addBlock = (block: Block, _page: number = -1) => {
|
|
|
|
const page = _page >= 0 ? _page : state.currentPage
|
2024-09-26 11:47:16 +08:00
|
|
|
console.log(page)
|
2024-09-18 16:27:43 +08:00
|
|
|
console.log(state.content[state.current].pages[page])
|
|
|
|
if (!state.content[state.current].pages[page]) {
|
|
|
|
globalToast.warning('请先添加页面')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const pageList = state.content[state.current].pages[page].list
|
|
|
|
pageList.push(block)
|
|
|
|
globalToast.success('添加成功')
|
|
|
|
}
|
2024-09-25 18:23:54 +08:00
|
|
|
const openDir = ref('')
|
|
|
|
// 文件夹只有一个时,将当前界面的文件夹替换为图标
|
|
|
|
const checkDir = (id: string) => {
|
|
|
|
const dir = state.dir[id]
|
|
|
|
if (!dir) return
|
|
|
|
if (dir && dir.list.length === 1) {
|
|
|
|
const item = dir.list[0]
|
|
|
|
if (!item) return
|
|
|
|
const idx = currentPage.value.list.findIndex((el) => el.link === 'id:' + id)
|
|
|
|
if (idx < 0) return
|
|
|
|
currentPage.value.list.splice(idx, 1, toRaw(item))
|
|
|
|
if (openDir.value === id) {
|
|
|
|
openDir.value = ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const getLabel = (block: Block) => {
|
|
|
|
if (block.link.startsWith('id:')) {
|
|
|
|
const dir = state.dir[block.link.slice(3)]
|
|
|
|
if (dir) return dir.label
|
|
|
|
}
|
|
|
|
return block.label || ''
|
|
|
|
}
|
2024-10-09 14:57:24 +08:00
|
|
|
const changeBackground = (url: string) => {
|
|
|
|
state.content[state.current].background = url
|
|
|
|
}
|
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,
|
2024-09-18 16:27:43 +08:00
|
|
|
background,
|
2024-09-25 18:23:54 +08:00
|
|
|
addBlock,
|
|
|
|
openDir,
|
|
|
|
checkDir,
|
2024-10-09 14:57:24 +08:00
|
|
|
getLabel,
|
|
|
|
changeBackground
|
2024-09-09 17:53:07 +08:00
|
|
|
}
|
|
|
|
})
|