Skip to content

Cursor Positioning: Performance and Test Coverage Improvements #177

Closed

Description

@malibio

Overview

Follow-up improvements for cursor positioning implementation from PR #176. These enhancements focus on performance optimization, comprehensive test coverage, and race condition handling.

Tasks

1. Optimize setCursorAtPixelOffset() with Binary Search

Current Issue: Linear search through every character position is O(n), causing ~50ms lag for 500-character lines.

Location: packages/desktop-app/src/lib/design/components/base-node-viewer.svelte:648-676

Proposed Solution: Implement binary search algorithm to reduce complexity from O(n) to O(log n):

function findCharacterAtPixelOffset(textNode: Text, targetPixel: number): number {
  let left = 0;
  let right = textNode.textContent?.length || 0;
  let bestOffset = 0;
  let bestDistance = Infinity;

  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    const range = document.createRange();
    range.setStart(textNode, mid);
    range.setEnd(textNode, mid);
    const currentPixel = range.getBoundingClientRect().left + window.scrollX;
    const distance = Math.abs(currentPixel - targetPixel);

    if (distance < bestDistance) {
      bestDistance = distance;
      bestOffset = mid;
    }

    if (currentPixel < targetPixel) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }

  return bestOffset;
}

Impact: For 500 characters: ~9 operations instead of 500

Priority: Low (only affects very long lines)


2. Implement Multi-Node Arrow Navigation Test

Current Issue: Primary use case (arrow navigation between nodes with different font sizes) is not tested.

Location: packages/desktop-app/src/tests/integration/cursor-positioning.test.ts:109-113

Proposed Solution: Create integration test using BaseNodeViewer:

it('should maintain horizontal position when navigating between H1 and regular text', async () => {
  const { container } = render(BaseNodeViewer, {
    parentId: 'test-parent',
    // Pre-populate with nodes: H1, H2, regular text at different depths
  });

  // Focus H1 node, position cursor at specific pixel offset
  // Arrow down to text node
  // Verify cursor maintains approximately same horizontal position
  // Test across multiple node types and depth levels
});

Priority: Medium (validates core feature)


3. Fix Shift+Enter Race Condition

Current Issue: positionCursorAfterSyntax() runs immediately (0ms) while recentShiftEnter flag clears after 100ms, creating potential timing issues.

Location: packages/desktop-app/src/lib/design/components/contentEditableController.ts:1124-1127

Current Code:

this.recentShiftEnter = true;
setTimeout(() => {
  this.recentShiftEnter = false;
}, 100);

setTimeout(() => {
  this.positionCursorAfterSyntax();
}, 0);

Proposed Solution: Use requestAnimationFrame for better DOM synchronization:

this.recentShiftEnter = true;
requestAnimationFrame(() => {
  this.positionCursorAfterSyntax();
  // Clear flag after positioning completes
  setTimeout(() => {
    this.recentShiftEnter = false;
  }, 50);
});

Priority: Low (edge case, rarely manifests)


Acceptance Criteria

  • Binary search algorithm implemented in setCursorAtPixelOffset()
  • Performance benchmarks show O(log n) complexity
  • Multi-node navigation test created with BaseNodeViewer
  • Test validates cursor position across H1, H2, H3, and regular text nodes
  • Race condition fixed with proper async coordination
  • All existing tests continue to pass
  • Documentation updated if implementation differs from proposals

Notes

These improvements are non-blocking and can be implemented incrementally. The current implementation is production-ready; these enhancements focus on edge cases and performance optimization.

Related PR: #176

Activity

  1. malibio commented on Dec 5, 2025

    @malibio
    CollaboratorAuthor

    Outdated - references contentEditableController.ts which no longer exists. Cursor positioning is now handled by:

    • cursor-positioning.ts
    • textarea-controller.svelte.ts
    • position-cursor.ts action

    Architecture has changed significantly. Any cursor improvements should be new issues.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementperformancetesting

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions