✨ feat: add Svelte actions and global stores for enhanced functionality
This commit is contained in:
@@ -2,8 +2,19 @@ import { get, writable } from "svelte/store"
|
||||
|
||||
/*********** location **************************/
|
||||
|
||||
/**
|
||||
* Strip trailing slash from a path, preserving root "/".
|
||||
* E.g. "/about/" → "/about"
|
||||
*/
|
||||
const stripTrailingSlash = (path: string): string => {
|
||||
if (path && path.length > 1 && path.endsWith("/")) {
|
||||
return path.replace(/\/+$/, "")
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
const initLoc = {
|
||||
path: (typeof window !== "undefined" && window.location?.pathname) || "/",
|
||||
path: stripTrailingSlash((typeof window !== "undefined" && window.location?.pathname) || "/"),
|
||||
search: (typeof window !== "undefined" && window.location?.search) || "",
|
||||
hash: (typeof window !== "undefined" && window.location?.hash) || "",
|
||||
push: false,
|
||||
@@ -26,8 +37,9 @@ const publishLocation = (_p?: string) => {
|
||||
if (_s) _s = "?" + _s
|
||||
}
|
||||
|
||||
const rawPath = _p || (typeof window !== "undefined" && window.location?.pathname)
|
||||
const newLocation: LocationStore = {
|
||||
path: _p || (typeof window !== "undefined" && window.location?.pathname),
|
||||
path: stripTrailingSlash(rawPath),
|
||||
search: _p ? _s : typeof window !== "undefined" && window.location?.search,
|
||||
hash: _p ? _h : typeof window !== "undefined" && window.location?.hash,
|
||||
push: !!_p,
|
||||
@@ -79,6 +91,26 @@ typeof window !== "undefined" &&
|
||||
publishLocation()
|
||||
})
|
||||
|
||||
/********************** UI State Stores *****************/
|
||||
|
||||
// Store for mobile menu open state (shared between Header and navigation components)
|
||||
export const mobileMenuOpen = writable<boolean>(false)
|
||||
|
||||
/********************** Current Content Page *****************/
|
||||
|
||||
// Store for tracking the currently displayed content page (used for cross-language linking etc.)
|
||||
export const currentContentEntry = writable<{ translationKey?: string; lang?: string; path?: string } | null>(null)
|
||||
|
||||
/********************** override for admin ui *****************/
|
||||
|
||||
export const apiBaseOverride = writable<string | null>(null)
|
||||
|
||||
/********************** Navigation History *****************/
|
||||
|
||||
// Store for tracking previous path (used for conditional back button)
|
||||
export const previousPath = writable<string | null>(null)
|
||||
|
||||
/********************** Cookie Consent *****************/
|
||||
|
||||
// Whether the cookie consent banner is currently visible
|
||||
export const cookieConsentVisible = writable(false)
|
||||
|
||||
Reference in New Issue
Block a user