fix(layout): cache pageWidth in localStorage to avoid render glitch

The page width from .remanso.json was only applied after an async
PouchDB + network fetch, so notes briefly rendered at the default
500px before snapping to the configured value. Persist pageWidth
alongside the existing font cache (key renamed to remanso:layout:*),
so it is read synchronously during setUserRepo and applied before
the first render. Also always reset --note-width with a default
fallback to prevent stale values leaking across repo navigation.
This commit is contained in:
Julien Calixte
2026-04-26 13:44:10 +02:00
parent 181ffd1e5c
commit 9f75e7971d
2 changed files with 23 additions and 20 deletions

View File

@@ -34,22 +34,24 @@ export const useUserRepoStore = defineStore("USER_REPO_STATE", {
_requestId: 0 _requestId: 0
}), }),
actions: { actions: {
_persistFonts() { _persistLayout() {
if (!this.userSettings) return if (!this.userSettings) return
try { try {
const { const {
chosenTitleFont, chosenTitleFont,
chosenBodyFont, chosenBodyFont,
chosenFontSize, chosenFontSize,
chosenFontFamily chosenFontFamily,
pageWidth
} = this.userSettings } = this.userSettings
localStorage.setItem( localStorage.setItem(
`remanso:fonts:${this.user}:${this.repo}`, `remanso:layout:${this.user}:${this.repo}`,
JSON.stringify({ JSON.stringify({
chosenTitleFont, chosenTitleFont,
chosenBodyFont, chosenBodyFont,
chosenFontSize, chosenFontSize,
chosenFontFamily chosenFontFamily,
pageWidth
}) })
) )
} catch { } catch {
@@ -61,18 +63,18 @@ export const useUserRepoStore = defineStore("USER_REPO_STATE", {
this.user = user this.user = user
this.repo = repo this.repo = repo
let lsFonts: Partial<UserSettings> = {} let lsLayout: Partial<UserSettings> = {}
try { try {
const lsRaw = localStorage.getItem(`remanso:fonts:${user}:${repo}`) const lsRaw = localStorage.getItem(`remanso:layout:${user}:${repo}`)
if (lsRaw) lsFonts = JSON.parse(lsRaw) if (lsRaw) lsLayout = JSON.parse(lsRaw)
} catch { } catch {
// ignore // ignore
} }
if (Object.keys(lsFonts).length) { if (Object.keys(lsLayout).length) {
if (!this.userSettings) if (!this.userSettings)
this.userSettings = { $type: DataType.UserSettings } this.userSettings = { $type: DataType.UserSettings }
Object.assign(this.userSettings, lsFonts) Object.assign(this.userSettings, lsLayout)
} }
const savedRepoId = generateId(DataType.SavedRepo, `${user}-${repo}`) const savedRepoId = generateId(DataType.SavedRepo, `${user}-${repo}`)
@@ -90,8 +92,8 @@ export const useUserRepoStore = defineStore("USER_REPO_STATE", {
} }
if (cachedUserSettings) { if (cachedUserSettings) {
// localStorage font choices take priority over PouchDB cache // localStorage layout choices take priority over PouchDB cache
this.userSettings = { ...cachedUserSettings, ...lsFonts } this.userSettings = { ...cachedUserSettings, ...lsLayout }
} }
try { try {
@@ -145,6 +147,8 @@ export const useUserRepoStore = defineStore("USER_REPO_STATE", {
this.userSettings.chosenTitleFont = chosenTitleFont this.userSettings.chosenTitleFont = chosenTitleFont
this.userSettings.chosenBodyFont = chosenBodyFont this.userSettings.chosenBodyFont = chosenBodyFont
this._persistLayout()
// Persist only repo config fields — chosen* are localStorage-only // Persist only repo config fields — chosen* are localStorage-only
const { const {
chosenTitleFont: _t, chosenTitleFont: _t,
@@ -205,28 +209,28 @@ export const useUserRepoStore = defineStore("USER_REPO_STATE", {
this.userSettings = { $type: DataType.UserSettings } this.userSettings = { $type: DataType.UserSettings }
} }
this.userSettings.chosenFontFamily = fontFamily this.userSettings.chosenFontFamily = fontFamily
this._persistFonts() this._persistLayout()
}, },
setFontSize(fontSize: string) { setFontSize(fontSize: string) {
if (!this.userSettings) { if (!this.userSettings) {
this.userSettings = { $type: DataType.UserSettings } this.userSettings = { $type: DataType.UserSettings }
} }
this.userSettings.chosenFontSize = fontSize this.userSettings.chosenFontSize = fontSize
this._persistFonts() this._persistLayout()
}, },
setTitleFont(font: string) { setTitleFont(font: string) {
if (!this.userSettings) { if (!this.userSettings) {
this.userSettings = { $type: DataType.UserSettings } this.userSettings = { $type: DataType.UserSettings }
} }
this.userSettings.chosenTitleFont = font this.userSettings.chosenTitleFont = font
this._persistFonts() this._persistLayout()
}, },
setBodyFont(font: string) { setBodyFont(font: string) {
if (!this.userSettings) { if (!this.userSettings) {
this.userSettings = { $type: DataType.UserSettings } this.userSettings = { $type: DataType.UserSettings }
} }
this.userSettings.chosenBodyFont = font this.userSettings.chosenBodyFont = font
this._persistFonts() this._persistLayout()
} }
} }
}) })

View File

@@ -6,6 +6,7 @@ import { downloadFont } from "@/utils/downloadFont"
const DEFAULT_FONT_POLICY = '"Libertinus Serif", serif' const DEFAULT_FONT_POLICY = '"Libertinus Serif", serif'
const DEFAULT_FONT_SIZE = "16px" const DEFAULT_FONT_SIZE = "16px"
const DEFAULT_NOTE_WIDTH = "500px"
export const useUserSettings = () => { export const useUserSettings = () => {
const store = useUserRepoStore() const store = useUserRepoStore()
@@ -24,10 +25,8 @@ export const useUserSettings = () => {
) )
root.style.setProperty("--font-size", fontSize || DEFAULT_FONT_SIZE) root.style.setProperty("--font-size", fontSize || DEFAULT_FONT_SIZE)
const pageWidth = store.userSettings?.pageWidth const pageWidth = store.userSettings?.pageWidth ?? DEFAULT_NOTE_WIDTH
if (pageWidth) { root.style.setProperty("--note-width", pageWidth)
root.style.setProperty("--note-width", pageWidth) resetNoteWidthCache()
resetNoteWidthCache()
}
}) })
} }