Skip to content

Fix cursor jumping to beginning in ContentEditable inputs#14

Merged
ProLoser merged 4 commits intogh-pagesfrom
copilot/fix-cursor-position-issue
Nov 11, 2025
Merged

Fix cursor jumping to beginning in ContentEditable inputs#14
ProLoser merged 4 commits intogh-pagesfrom
copilot/fix-cursor-position-issue

Conversation

Copy link
Contributor

Copilot AI commented Nov 10, 2025

Fix cursor jumping to beginning of input in ContentEditable component

Problem

When typing in a line or comment, the cursor would jump to the beginning of the input after each keystroke, making it impossible to type normally.

Root Cause

  1. Every keystroke triggers onChange which updates Firebase
  2. Firebase updates propagate back to the component as new props
  3. shouldComponentUpdate returns true, causing React to re-render
  4. Re-rendering recreates the DOM element, resetting cursor to position 0

Solution

Modified the ContentEditable component with two complementary fixes:

Primary Fix: Prevent Re-renders During Typing

Updated shouldComponentUpdate to return false when the element is currently focused (user is actively typing). This prevents unnecessary re-renders and naturally preserves cursor position.

shouldComponentUpdate: function(nextProps) {
    var element = this.getDOMNode();
    if (document.activeElement === element) {
        return false;  // Skip re-render while typing
    }
    return nextProps.html !== element.innerHTML;
}

Backup Fix: Cursor Position Restore

Extracted cursor positioning logic into a reusable placeCaretAtPosition helper function that saves and restores cursor position for edge cases where updates occur when the element doesn't have focus.

Implementation Status

  • Identify the root cause in ContentEditable component
  • Fix shouldComponentUpdate to prevent re-renders when element is focused
  • Refactor cursor positioning into DRY helper function
  • Build and clean JSX to JavaScript compilation
  • Run security scan (0 alerts found)
  • Document the fix and testing approach

Files Changed

  • script.jsx - Modified ContentEditable component and added placeCaretAtPosition helper
  • script.js - Compiled JavaScript from JSX

Security

✅ CodeQL analysis passed with 0 alerts - no security vulnerabilities introduced.

Code Quality

Refactored to follow DRY principles by extracting cursor positioning logic into a reusable helper function, reducing code duplication and improving maintainability.

Original prompt

writing into the comment or line causes the cursor to move to the left edge of the input. I think every on change is causing the element to re-render which is moving the cursor. fix it so that the cursor does not keep sliding to the beginning of the input whenever I start typing on a row or comment


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits November 10, 2025 17:12
Co-authored-by: ProLoser <67395+ProLoser@users.noreply.github.com>
Co-authored-by: ProLoser <67395+ProLoser@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix cursor movement issue when typing in comments Fix cursor jumping to beginning in ContentEditable inputs Nov 10, 2025
Copilot AI requested a review from ProLoser November 10, 2025 17:20
@ProLoser
Copy link
Owner

@copilot make this more dry

Co-authored-by: ProLoser <67395+ProLoser@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 11, 2025

@copilot make this more dry

Refactored the cursor positioning logic into a reusable placeCaretAtPosition helper function. This reduced the componentDidUpdate method from ~40 lines to 4 lines and eliminated code duplication. (3b8edf7)

@ProLoser
Copy link
Owner

@copilot are both files being used?

Copy link
Contributor Author

Copilot AI commented Nov 11, 2025

@copilot are both files being used?

Yes, both files are being used:

  • script.jsx is the source file (JSX/React code you edit)
  • script.js is the compiled output (what the browser loads via index.html line 18)

The gulpfile.js has a react task that compiles .jsx.js. Both files are currently tracked in git, though typically build outputs like script.js would be gitignored and generated during deployment. If you'd prefer, I can add script.js to .gitignore so only the source script.jsx is committed.

@ProLoser ProLoser marked this pull request as ready for review November 11, 2025 03:44
@ProLoser ProLoser merged commit a6d9de9 into gh-pages Nov 11, 2025
1 check passed
@ProLoser ProLoser deleted the copilot/fix-cursor-position-issue branch November 11, 2025 03:44
Copilot AI mentioned this pull request Nov 11, 2025
8 tasks
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