Something went wrong. Try again.
a tool for shared writing and social publishing
Something went wrong. Try again.
# Paste Performance Plan: Bulk-Paste Mutator with Pre-Encoded Yjs Content
## Context
`components/Blocks/TextBlock/useHandlePaste.ts` is the paste handler for ProseMirror text blocks. Large pastes (long Google Docs, long articles, Leaflet cards with many embedded facts) are visibly slow and have several correctness footguns:
1. **Per-block fact fanout.** Each pasted block fires up to ~6 separate `rep.mutate.*` calls (`addBlock`, then individual `assertFact` calls for `heading-level`, `text-alignment`, `text-size`, `is-list`, `check-list`, `list-style`). Each is its own Replicache transaction and IDB commit. A 100-block paste produces ~500 transactions.
2. **`setTimeout(..., 10)` mount race.** The handler creates a block via `addBlock`, then schedules a 10ms timer to inject content into the not-yet-mounted ProseMirror view. If mount takes longer than 10ms (likely with 200 blocks committing in one React commit), content is silently dropped.
3. **Editor-state storm.** Each timer fires its own `setEditorState`, which triggers the unkeyed `useEditorStates.subscribe` listener in `mountProsemirror.ts:214` for every mounted block on the page.
4. **Per-block undo entries.** Pasting 200 blocks requires 200 Cmd-Z presses to undo. `undoManager.withUndoGroup` cannot fix this because the per-block undo adds happen inside `setTimeout` bodies, well after the synchronous group has closed.
5. **300ms Yjs persistence debounce.** Pasted content reaches Replicache only after the `useYJSValue` `observeDeep` callback fires; a tab close within 300ms of paste loses everything.
The fix is architectural: stop driving paste through the live editor view. Instead, encode each pasted block's content as a Yjs binary update on the client, then write all entities + facts (including `block/text`) in a **single Replicache mutator transaction**. When the new blocks subsequently mount, `useYJSValue` reads the `block/text` fact via the standard load path and ProseMirror initializes with content already present.
## Architecture
A Replicache mutato…