- Added Vue plugin to vitest.config.ts for better component testing. - Introduced vitest.setup.ts to expose Vue reactivity and lifecycle methods globally, ensuring compatibility with .vue components. - Updated mock implementations in nuxt-imports.ts to include readonly for improved reactivity handling. - Refactored useMapBookmarks and useToast composables to utilize readonly from Vue for better state management.
90 lines
2.0 KiB
TypeScript
90 lines
2.0 KiB
TypeScript
import { readonly } from 'vue'
|
|
|
|
export interface MapBookmark {
|
|
id: string
|
|
name: string
|
|
mapId: number
|
|
x: number
|
|
y: number
|
|
zoom?: number
|
|
createdAt: number
|
|
}
|
|
|
|
const STORAGE_KEY = 'hnh-map-bookmarks'
|
|
const MAX_BOOKMARKS = 50
|
|
|
|
const BOOKMARKS_STATE_KEY = 'hnh-map-bookmarks-state'
|
|
|
|
function loadBookmarks(): MapBookmark[] {
|
|
if (import.meta.server) return []
|
|
try {
|
|
const raw = localStorage.getItem(STORAGE_KEY)
|
|
if (!raw) return []
|
|
const parsed = JSON.parse(raw) as MapBookmark[]
|
|
return Array.isArray(parsed) ? parsed : []
|
|
} catch {
|
|
return []
|
|
}
|
|
}
|
|
|
|
function saveBookmarks(bookmarks: MapBookmark[]) {
|
|
if (import.meta.server) return
|
|
try {
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(bookmarks.slice(0, MAX_BOOKMARKS)))
|
|
} catch { /* ignore */ }
|
|
}
|
|
|
|
export function useMapBookmarks() {
|
|
const bookmarks = useState<MapBookmark[]>(BOOKMARKS_STATE_KEY, () => [])
|
|
|
|
function refresh() {
|
|
bookmarks.value = loadBookmarks()
|
|
}
|
|
|
|
function add(bookmark: Omit<MapBookmark, 'id' | 'createdAt'>) {
|
|
const list = loadBookmarks()
|
|
const id = `b-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`
|
|
const created: MapBookmark = {
|
|
...bookmark,
|
|
id,
|
|
createdAt: Date.now(),
|
|
}
|
|
list.unshift(created)
|
|
saveBookmarks(list)
|
|
refresh()
|
|
return id
|
|
}
|
|
|
|
function update(id: string, patch: { name: string }) {
|
|
const list = loadBookmarks()
|
|
const idx = list.findIndex((b) => b.id === id)
|
|
if (idx < 0) return false
|
|
list[idx] = { ...list[idx], name: patch.name }
|
|
saveBookmarks(list)
|
|
refresh()
|
|
return true
|
|
}
|
|
|
|
function remove(id: string) {
|
|
const list = loadBookmarks().filter((b) => b.id !== id)
|
|
saveBookmarks(list)
|
|
refresh()
|
|
}
|
|
|
|
function clear() {
|
|
saveBookmarks([])
|
|
refresh()
|
|
}
|
|
|
|
onMounted(refresh)
|
|
|
|
return {
|
|
bookmarks: readonly(bookmarks),
|
|
refresh,
|
|
add,
|
|
update,
|
|
remove,
|
|
clear,
|
|
}
|
|
}
|