fix(editor): prevent editor from scrolling on focus#1546
Closed
yanglbme wants to merge 1 commit into
Closed
Conversation
|
🚀 Cloudflare Workers Preview has been successfully deployed! Preview URL: https://md-pr-1546.doocs.workers.dev Built with commit 03bd95d |
|
🚀 Surge Preview has been successfully deployed! Preview URL: https://doocs-md-preview-pr-1546.surge.sh Built with commit 03bd95d |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adjusts the shared CodeMirror 6 theme/extensions to stop the editor from “jumping” its scroll position when the editor receives focus, aiming to neutralize CodeMirror’s default scrollIntoView behavior on focus.
Changes:
- Added a
focusDOM event handler extension that restores the scroller’sscrollTop/scrollLefton the next animation frame. - Added CodeMirror theme rules to remove the focused outline and to set
overscrollBehavior: containon the scroller. - Included the new event handler extension in both light and dark theme extension arrays.
Comments suppressed due to low confidence (1)
packages/shared/src/editor/themes.ts:23
- Removing the focus outline on
.cm-focusedeliminates the default visible focus indicator. Unless another focus style is applied elsewhere, this is an accessibility regression for keyboard users (WCAG focus visible). Prefer keeping the outline or replacing it with an explicit, theme-consistent focus ring (e.g., box-shadow/border using existing CSS variables).
// 移除聚焦时的蓝色边框
'&.cm-focused': {
outline: `none`,
},
Comment on lines
+7
to
+16
| const noScrollOnFocus = EditorView.domEventHandlers({ | ||
| focus(_event, _view) { | ||
| const scrollDOM = _view.scrollDOM | ||
| const { scrollTop, scrollLeft } = scrollDOM | ||
| requestAnimationFrame(() => { | ||
| scrollDOM.scrollTop = scrollTop | ||
| scrollDOM.scrollLeft = scrollLeft | ||
| }) | ||
| return false | ||
| }, |
Comment on lines
+8
to
+9
| focus(_event, _view) { | ||
| const scrollDOM = _view.scrollDOM |
Capture scroll position before focus and restore it via requestAnimationFrame to undo CodeMirror's automatic scrollIntoView.
987fcca to
03bd95d
Compare
|
🗑️ Cloudflare Workers preview deployment has been cleaned up. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When clicking/focusing the editor area, the CodeMirror editor would jump/scroll automatically due to CodeMirror 6's default
scrollIntoViewbehavior on focus.Solution
Added a
focusevent handler that:scrollTopandscrollLeftbefore CodeMirror's automatic scrollrequestAnimationFrameafter CodeMirror finishes its scrollIntoViewAlso added CSS styles:
&.cm-focused { outline: none }- removes focus outline.cm-scroller { overscrollBehavior: contain }- prevents scroll chaining