Skip to content

code-block-render-handoff

Carries focus and plugin state from a code block to its next render.

Obsidian renders a code block again whenever its source text changes: Reading view re-renders the section and Live Preview replaces the widget. So a block that edits its own source (a counter, a checkbox, a settings form) loses the element the user was interacting with, its focus and whatever the plugin kept on it. The element itself cannot be kept without patching Obsidian’s renderers, so the write records a handoff instead, and the processor rendering the new source claims it: it gets back the state the write passed and can put the focus back on the same element.

The handoff is matched by source path, language and the new block content, and it lives for CODE_BLOCK_RENDER_HANDOFF_LIFETIME_IN_MILLISECONDS. The first render to claim it consumes it, so when the note is open in two panes only one of the new renders gets it.

Interface Description
ClaimCodeBlockRenderHandoffParams Options for claimCodeBlockRenderHandoff.
CodeBlockFocusSnapshot Where the focus was inside a rendered code block.
CodeBlockRenderHandoff A handoff claimed by the render of a code block whose source a write just changed.
RecordCodeBlockRenderHandoffParams Options for recordCodeBlockRenderHandoff.
Function Description
captureCodeBlockFocus Captures where the focus is inside a rendered code block.
claimCodeBlockRenderHandoff Claims the handoff a write recorded for the code block being rendered. Call it from a code block processor. It returns null when no write is waiting for this render.
recordCodeBlockRenderHandoff Records a handoff for the next render of a code block a write is about to change.
restoreCodeBlockFocus Puts the focus back on the element a CodeBlockFocusSnapshot describes.
Variable Description
CODE_BLOCK_RENDER_HANDOFF_LIFETIME_IN_MILLISECONDS How long a recorded handoff waits for the render that claims it.