Skip to content

[edit] Edit Focus Polish Improvements#1017

Open
amadeus wants to merge 12 commits into
mainfrom
amadeus/edit-focus-polish
Open

[edit] Edit Focus Polish Improvements#1017
amadeus wants to merge 12 commits into
mainfrom
amadeus/edit-focus-polish

Conversation

@amadeus

@amadeus amadeus commented Jul 22, 2026

Copy link
Copy Markdown
Member

Mostly the was to improve some API paper cuts around line focusing with the editor

  • Extend Editor.focus() with explicit line/character targeting and a 'first-visible' mode that preserves scroll position.
  • Resolve visible editor rows across standard diffs, element/window Virtualizers, and CodeView, including sticky headers and scroll-container fallbacks.
  • Keep onAttach reliable across queued rerenders and virtualized recycling without targeting detached DOM or stealing focus from other controls.
  • Add opt-in autofocus examples to every editable playground mode and document the React and vanilla APIs.
  • Add some documentation around editor focusing and related APIs
  • Expand regression coverage for viewport resolution, editable-row filtering, attachment lifecycle, and CodeView option forwarding.

amadeus added 4 commits July 21, 2026 16:38
Call `editor.focus()` with a one-based line number and optional
zero-based character offset to replace the current selection with a
collapsed caret at that position.

Normalize out-of-range targets through the text document and preserve
`preventScroll` behavior. Calls made before attachment remain no-ops.
Call `editor.focus()` with `lineNumber: "first-visible"` to place the
caret on the first editable row whose top is inside the virtualized
viewport. Use `offset` to keep the target below additional overlays.

Expose viewport roots for virtualized files and diffs, account for sticky
headers, and preserve focus when no eligible row exists.
Pass an `onAttach` callback through React CodeView `editOptions` for
simultaneous file and diff sessions. Assert the provider receives the
same callback while item-specific `onChange` routing remains isolated.
Enter edit mode from the playground or a virtualized surface: focus could
miss the first visible line, and later row replacement could reclaim focus
from another control.

Fall back to the nearest scrollable viewport, defer onAttach until the live
editable DOM is synchronized, and restore focus only while the editor
already owns it. Document and demonstrate the opt-in autofocus recipe.
@vercel

vercel Bot commented Jul 22, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
pierre-docs-diffs Ready Ready Preview Jul 23, 2026 2:10am
pierre-docs-diffshub Ready Ready Preview Jul 23, 2026 2:10am
pierre-docs-trees Ready Ready Preview Jul 23, 2026 2:10am
pierrejs-diff-demo Ready Ready Preview Jul 23, 2026 2:10am

Request Review

@amadeus

amadeus commented Jul 22, 2026

Copy link
Copy Markdown
Member Author

@codex review

@amadeus
amadeus requested a review from ije July 22, 2026 00:31
(why link to the section you are in, lol...)
This reverts commit f0772f0.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6e44dfcb25

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/diffs/src/editor/editor.ts
Comment thread packages/diffs/src/types.ts
A focused editor loses its caret when a full render replaces or moves the
editable DOM, so typing stops until the user focuses it again.

Capture focus intent before renderer mutations and restore it after the new
DOM synchronizes. Cancel stale restoration when focus moves elsewhere, and
coordinate deferred focus through the shared render queue.
amadeus added 5 commits July 22, 2026 16:02
Define component-owned view capture and restoration across files, diffs,
and CodeView. Cover logical vertical positions, horizontal code scrolling,
and recycle behavior before replacing the raw scroll-container API.
Allow editor surfaces to read and restore logical vertical positions without
reaching into the virtualizer root. Support element and document roots with
a browser-style scroll operation while preserving cached DOM reads.
Expose code scrollLeft through editable components and let virtualized
variants return the Virtualizer or CodeView that owns vertical scrolling.

Capture horizontal state before teardown clears the code elements.
Capture horizontal scrolling from the editable code surface and logical
vertical scrolling from its Virtualizer or CodeView.

Restore each axis through its owner so rebased CodeView positions remain
correct and non-virtualized state omits unsupported vertical state.
Editor state should follow the editable code surface rather than a shared
virtualized viewport. Capturing scrollTop ties per-file persistence to
Virtualizer and CodeView and can move the viewer during state restoration.

Persist only the code scroller's scrollLeft, remove the vertical owner
bridge and obsolete scroll-container accessors, and leave vertical scroll
APIs on their virtualizer owners.
@amadeus

amadeus commented Jul 23, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 295d44e0b6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

const unifiedAST = this.hunksRenderer.renderCodeAST('unified', result);
const deletionsAST = this.hunksRenderer.renderCodeAST('deletions', result);
const additionsAST = this.hunksRenderer.renderCodeAST('additions', result);
this.editor?.__captureFocusForDOMReplacement();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Guard the new internal editor hook

When an app supplies a custom/loosely typed DiffsEditor from createEditor or attachEditor instead of the built-in Editor, existing implementations won't have the new internal focus hook. This unconditional call runs during every FileDiff render, so those editors now throw before the diff can paint; make the hook optional at the type and call site (or provide a compatibility no-op) so existing custom editors keep working.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants