Files
964e709817 fix(editor): preserve toggle fold state across remote and local document edits (#13425)
* fix(editor): preserve toggle fold state across remote and local document edits

Fold state was re-derived from localStorage on every remote collaborative
transaction (and partially on local doc-changing ones), so a silently
failed Storage.set (quota exceeded, blocked storage) left a stale
{fold:true} and the next edit re-folded blocks the user had expanded —
the plugin seed from previous state was ineffective because
toggleFoldPluginKey.getState(newState) is always undefined during the
plugin's own apply.

Track a knownIds set alongside foldedIds and reconcile from the previous
plugin state (the second apply argument, which is reliable): existing
fold decisions are preserved verbatim and storage is only consulted for
toggle blocks seen for the first time.

Fixes #13424

* fix(editor): bound and harden toggle fold state persistence

Fold state was stored under a localStorage key per toggle block id with
no bound on how many keys accumulate, and a write that failed silently
(quota exceeded, storage unavailable) left a stale value that the next
edit would re-fold from. Two complementary changes:

- Storage falls back to session storage when a write to the primary
  interface fails at runtime, so values at least survive the browsing
  session; reads prefer the fallback copy while it is the most recent
  successful write, and successful writes drop it again.
- Toggle fold state now lives in an LRUCache mirrored to local storage,
  capping the number of stored keys and evicting the least recently used
  fold states first. The persisted key format is unchanged so fold state
  continues to sync across tabs via storage events.

Follow-up to #13425, addressing the underlying feedback in #13424.

Co-Authored-By: Claude <noreply@anthropic.com>

* docs: add development workflow best practices to CLAUDE.md

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(editor): rename persistTo option to storage in LRUCache

Rename the `persistTo` cache option to `storage` with a union type of
`"memory" | "localStorage" | "sessionStorage"` per review feedback.
The option is now required instead of optional; callers that only need
an in-memory cache pass `storage: "memory"`.

The private Storage instance field is renamed from `storage` to `store`
to avoid colliding with the new option name, and the mapping from
user-facing values to Storage constructor arguments is handled in
`hydrate()`.

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(editor): eliminate redundant findBlockNodes scan in reconcileFoldState

The hot path for every local document change previously called
findBlockNodes twice on the same document — once in reconcileFoldState
and again in createDecorations.  Now reconcileFoldState collects the
blocks once and passes them directly to the decoration builder.

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(editor): fix createDecorations signature to accept optional blocks parameter

The init and ACTION branches pass a NodeWithPos[] as the second argument,
which previously landed in the foldedIds parameter — a type error missed
due to missing tsc validation.

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(editor): correct createDecorations argument order and NodeWithPos import

The createDecorations signature is (doc, foldedIds, blocks?) but both the
init and ACTION branches passed arguments in the old order (doc, blocks,
foldedIds). Because the Set is always truthy, the ?? fallback never fired
and buildDecorationsFromBlocks iterated over strings, crashing on
block.node.attrs. NodeWithPos is also not exported from findChildren.

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(editor): address review feedback and fix CI

- Document the default-folded fallback for evicted blocks in the
  fold-state cache comment.
- Cast the Storage test mock through unknown so the type check passes.
- Provide in-memory web storage for the shared jsdom test project so the
  persistence tests run instead of crashing on a missing implementation.

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(editor): scope storage fallback writes and drop unrelated changes

- Track keys mirrored to the storage fallback and clear only those,
  rather than clearing all of session storage
- Restore CLAUDE.md as a symlink to AGENTS.md
- Remove the unrelated scrollToNonce changes
- Make the fallback interface private and probe with a namespaced key

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Tom Moor <tom@getoutline.com>
2026-09-04 08:33:10 -04:00

80 lines
2.7 KiB
TypeScript

import { Storage } from "./Storage";
// Shared tests run in both node and jsdom; web storage only exists in jsdom.
const hasStorage = typeof window !== "undefined";
describe.runIf(hasStorage)("Storage", () => {
beforeEach(() => {
localStorage.clear();
sessionStorage.clear();
});
it("stores and retrieves values", () => {
const storage = new Storage("local");
storage.set("key", "value");
expect(storage.get("key")).toBe("value");
});
it("falls back to session storage when a write fails", () => {
const storage = new Storage("local");
const primary = storage.interface;
storage.interface = {
getItem: (key: string) => primary.getItem(key),
setItem: () => {
throw new Error("QuotaExceededError");
},
removeItem: (key: string) => primary.removeItem(key),
clear: () => primary.clear(),
} as unknown as typeof localStorage;
storage.set("toggle:1", { fold: false });
// The value survived in session storage, and reads prefer the fallback
// copy as it is the most recent successful write.
expect(sessionStorage.getItem("toggle:1")).toBe(
JSON.stringify({ fold: false })
);
expect(storage.get("toggle:1")).toEqual({ fold: false });
// A later successful write removes the fallback copy so reads of the key
// stay consistent.
storage.interface = primary;
storage.set("toggle:1", { fold: true });
expect(sessionStorage.getItem("toggle:1")).toBeNull();
expect(storage.get("toggle:1")).toEqual({ fold: true });
});
it("removes values from both interfaces", () => {
const storage = new Storage("local");
storage.set("key", "value");
sessionStorage.setItem("key", JSON.stringify("value"));
storage.remove("key");
expect(localStorage.getItem("key")).toBeNull();
expect(sessionStorage.getItem("key")).toBeNull();
});
it("clears only the keys written by this instance", () => {
const storage = new Storage("local");
const primary = storage.interface;
storage.interface = {
getItem: (key: string) => primary.getItem(key),
setItem: () => {
throw new Error("QuotaExceededError");
},
removeItem: (key: string) => primary.removeItem(key),
clear: () => primary.clear(),
} as unknown as typeof localStorage;
storage.set("toggle:1", { fold: false });
storage.interface = primary;
sessionStorage.setItem("unrelated", "keep");
storage.clear();
expect(storage.get("toggle:1")).toBeUndefined();
expect(sessionStorage.getItem("toggle:1")).toBeNull();
// Session storage is shared with unrelated features and is left alone.
expect(sessionStorage.getItem("unrelated")).toBe("keep");
});
});