fix: font size caching + persistence debug logging

Font fix: setChatFontSize() was called in the view factory before
onOpen() created the DOM. querySelector returned null and the call
silently failed. Now stores _chatFontSize field — setChatFontSize()
always caches the value, and onOpen() reapplies it once the container
exists. This fixes the "font resets to default on restart" bug.

Persistence debug: added console.log in onOpen() restore loop and
onClose() save to help diagnose why messages aren't repopulating.
data.json is confirmed written correctly on disk — the debug logs
will reveal whether loadMessages() returns the saved data at all.
This commit is contained in:
Helm 2026-07-10 13:38:29 -04:00
parent fc5c9c01bf
commit 175cfa2c61
2 changed files with 22 additions and 8 deletions

12
main.js

File diff suppressed because one or more lines are too long

View file

@ -39,6 +39,9 @@ export class ChatView extends ItemView {
* saveMessages() calls to avoid N racing saveData() writes. */ * saveMessages() calls to avoid N racing saveData() writes. */
private _restoring: boolean = false; private _restoring: boolean = false;
/** Cached font size — applied after DOM is ready in onOpen(). */
private _chatFontSize: number = 14;
/** Context captured on chat focus avoids the timing bug where /** Context captured on chat focus avoids the timing bug where
* activeEditor is null by the time handleSend() runs. */ * activeEditor is null by the time handleSend() runs. */
private capturedNote: { private capturedNote: {
@ -81,6 +84,11 @@ export class ChatView extends ItemView {
container.empty(); container.empty();
container.addClass("hermes-chat-container"); container.addClass("hermes-chat-container");
// Apply cached font size now that the DOM is ready.
// setChatFontSize() may have been called by the factory before
// onOpen() created the container, so re-apply here.
this.setChatFontSize(this._chatFontSize);
// --- Message list area --- // --- Message list area ---
this.messagesContainer = container.createDiv("chat-messages"); this.messagesContainer = container.createDiv("chat-messages");
this.messagesContainer.id = "chat-messages"; this.messagesContainer.id = "chat-messages";
@ -185,12 +193,14 @@ export class ChatView extends ItemView {
// we do a single save at the end to avoid N racing saveData() calls. // we do a single save at the end to avoid N racing saveData() calls.
if (this.persistence) { if (this.persistence) {
const saved = this.persistence.loadMessages(); const saved = this.persistence.loadMessages();
if (saved.length > 0) { console.log("[Hermes] Restoring messages — count:", saved?.length ?? 0);
if (saved && saved.length > 0) {
this._restoring = true; this._restoring = true;
try { try {
for (const msg of saved) { for (const msg of saved) {
await this.appendMessage(msg.role, msg.content); await this.appendMessage(msg.role, msg.content);
} }
console.log("[Hermes] Restored", saved.length, "messages successfully");
} catch (err) { } catch (err) {
console.error("[Hermes] Failed to restore messages:", err); console.error("[Hermes] Failed to restore messages:", err);
} finally { } finally {
@ -203,6 +213,7 @@ export class ChatView extends ItemView {
} }
async onClose(): Promise<void> { async onClose(): Promise<void> {
console.log("[Hermes] onClose — saving", this.chatState.messages.length, "messages");
await this.persistence?.saveMessages(this.chatState.messages); await this.persistence?.saveMessages(this.chatState.messages);
} }
@ -225,8 +236,11 @@ export class ChatView extends ItemView {
this.slashCommands = commands; this.slashCommands = commands;
} }
/** Set chat font size via CSS custom property on the container. */ /** Set chat font size via CSS custom property on the container.
* Stores the value regardless of DOM state applies immediately
* if the container exists, otherwise waits for onOpen(). */
setChatFontSize(size: number): void { setChatFontSize(size: number): void {
this._chatFontSize = size;
const el = this.containerEl.querySelector(".hermes-chat-container") as HTMLElement; const el = this.containerEl.querySelector(".hermes-chat-container") as HTMLElement;
if (el) el.style.setProperty("--hermes-chat-font-size", `${size}px`); if (el) el.style.setProperty("--hermes-chat-font-size", `${size}px`);
} }