mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
* 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>
163 lines
4.5 KiB
TypeScript
163 lines
4.5 KiB
TypeScript
import type { Primitive } from "utility-types";
|
|
|
|
/**
|
|
* Storage is a wrapper class for web storage that allows safe usage when
|
|
* localStorage or sessionStorage are not available.
|
|
*/
|
|
export class Storage {
|
|
interface: typeof localStorage | MemoryStorage;
|
|
|
|
// Used when a write to the primary interface fails at runtime, e.g. when
|
|
// the local storage quota has been exceeded.
|
|
private fallbackInterface: typeof sessionStorage | null = null;
|
|
|
|
/** Keys whose most recent write went to the fallback interface. */
|
|
private fallbackKeys = new Set<string>();
|
|
|
|
/**
|
|
* @param type whether to persist for the session only, or indefinitely.
|
|
*/
|
|
public constructor(type: "local" | "session" = "local") {
|
|
try {
|
|
// Avoid touching the storage globals outside the browser; in Node they
|
|
// resolve to an experimental Web Storage API that emits a warning on access.
|
|
if (typeof window === "undefined") {
|
|
throw new Error("Web storage is not available");
|
|
}
|
|
const storage = type === "session" ? sessionStorage : localStorage;
|
|
storage.setItem("test", "test");
|
|
storage.removeItem("test");
|
|
this.interface = storage;
|
|
} catch (_err) {
|
|
this.interface = new MemoryStorage();
|
|
}
|
|
|
|
// Session storage is used as a fallback when writes to the primary
|
|
// interface fail at runtime, so that values at least survive the current
|
|
// browsing session.
|
|
if (type === "local") {
|
|
try {
|
|
if (typeof window !== "undefined") {
|
|
sessionStorage.setItem("storage:probe", "1");
|
|
sessionStorage.removeItem("storage:probe");
|
|
this.fallbackInterface = sessionStorage;
|
|
}
|
|
} catch (_err) {
|
|
// Ignore errors
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set a value in storage. For efficiency, this method will remove the
|
|
* value if it is undefined.
|
|
*
|
|
* @param key The key to set under.
|
|
* @param value The value to set
|
|
*/
|
|
public set<T>(key: string, value: T) {
|
|
if (value === undefined) {
|
|
this.remove(key);
|
|
return;
|
|
}
|
|
try {
|
|
this.interface.setItem(key, JSON.stringify(value));
|
|
// Drop any fallback copy so that reads of this key stay consistent.
|
|
this.fallbackInterface?.removeItem(key);
|
|
this.fallbackKeys.delete(key);
|
|
} catch (_err) {
|
|
// The primary interface can fail at runtime, e.g. when its quota has
|
|
// been exceeded — write to session storage instead so the value at
|
|
// least survives the browsing session.
|
|
try {
|
|
this.fallbackInterface?.setItem(key, JSON.stringify(value));
|
|
this.fallbackKeys.add(key);
|
|
} catch (_err) {
|
|
// Ignore errors
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get a value from storage.
|
|
*
|
|
* @param key The key to get.
|
|
* @param fallback The fallback value if the key doesn't exist.
|
|
* @returns The value or undefined if it doesn't exist.
|
|
*/
|
|
public get(key: string, fallback?: Primitive) {
|
|
try {
|
|
// A fallback copy only exists when the last successful write of this
|
|
// key went there, so when present it is the most recent value.
|
|
const value =
|
|
this.fallbackInterface?.getItem(key) ?? this.interface.getItem(key);
|
|
if (typeof value === "string") {
|
|
return JSON.parse(value);
|
|
}
|
|
} catch (_err) {
|
|
// Ignore errors
|
|
}
|
|
|
|
return fallback;
|
|
}
|
|
|
|
/**
|
|
* Remove a value from storage.
|
|
*
|
|
* @param key The key to remove.
|
|
*/
|
|
public remove(key: string) {
|
|
try {
|
|
this.interface.removeItem(key);
|
|
this.fallbackInterface?.removeItem(key);
|
|
this.fallbackKeys.delete(key);
|
|
} catch (_err) {
|
|
// Ignore errors
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Clear all values from storage.
|
|
*/
|
|
public clear() {
|
|
try {
|
|
this.interface.clear();
|
|
// The fallback interface is shared with unrelated features, so only
|
|
// the keys mirrored by this instance are removed rather than all of
|
|
// the session storage.
|
|
for (const key of this.fallbackKeys) {
|
|
this.fallbackInterface?.removeItem(key);
|
|
}
|
|
this.fallbackKeys.clear();
|
|
} catch (_err) {
|
|
// Ignore errors
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* MemoryStorage is a simple in-memory storage implementation that is used
|
|
* when localStorage is not available.
|
|
*/
|
|
class MemoryStorage {
|
|
private data: Record<string, string> = {};
|
|
|
|
getItem(key: string) {
|
|
return this.data[key] || null;
|
|
}
|
|
|
|
setItem(key: string, value: Primitive) {
|
|
return (this.data[key] = String(value));
|
|
}
|
|
|
|
removeItem(key: string) {
|
|
return delete this.data[key];
|
|
}
|
|
|
|
clear() {
|
|
return (this.data = {});
|
|
}
|
|
}
|
|
|
|
export default new Storage();
|