Skip to content

fix: uneditable editor input#91

Merged
will-lp1 merged 2 commits intowill-lp1:mainfrom
Praashh:fix/uneditable_editor_input
Sep 2, 2025
Merged

fix: uneditable editor input#91
will-lp1 merged 2 commits intowill-lp1:mainfrom
Praashh:fix/uneditable_editor_input

Conversation

@Praashh
Copy link
Collaborator

@Praashh Praashh commented Sep 2, 2025

This PR fixes the uneditable editor input reported here: https://x.com/studio_hungry/status/1962794756439716321

Fix Screen recording

Screen.Recording.2025-09-02.at.4.00.52.PM.mov

Summary by CodeRabbit

  • New Features
    • None.
  • Bug Fixes
    • Prevents the editor from unexpectedly switching between editable and read-only during preview, cancel, or apply, improving focus and interaction stability.
  • Refactor
    • Editability handling delegated to the main editor for consistent behavior.
    • Version fork flow now relies on server validation and allows forking from the latest version; leaving a version preview no longer auto-commits.

@vercel
Copy link

vercel bot commented Sep 2, 2025

@Praashh is attempting to deploy a commit to the william Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Sep 2, 2025

Walkthrough

Removed direct contenteditable toggles from the diff plugin and delegated editability to the Editor component; removed client-side validation for version-fork and allow forking from the latest version; stopped auto-committing to the latest version on pointer leave. Server-side validation and fork request flow remain.

Changes

Cohort / File(s) Summary
Editor editability delegation
apps/saru/lib/editor/diff-plugin.ts
Three calls that toggled contenteditable during preview start, cancel, and apply were commented out and annotated to indicate editability is handled by the Editor component; event handling otherwise unchanged.
Versioning & fork flow
apps/saru/components/document/document-workspace.tsx, apps/saru/components/document/version-rail.tsx
Removed client-side validation for version-fork (no checks for numeric/valid versionIndex or forkFromTimestamp); allowed forking from the latest version (removed guard preventing fork from last index); removed automatic commit-to-latest on pointer leave; fork request still POSTs to /api/document and relies on server response for navigation and errors.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant VersionRail
    participant DiffPlugin
    participant Editor
    participant Server

    User->>VersionRail: triggerFork(idx, forkFromTimestamp)
    VersionRail-->>DiffPlugin: dispatch version-fork event (detail includes idx, timestamp)
    DiffPlugin->>Server: POST /api/document { action: 'fork', versionIndex, forkFromTimestamp, ... }
    Server-->>DiffPlugin: 200 { id: newDocId } / error
    DiffPlugin->>User: navigate to newDocId or show error

    rect #EFEFEF
      Note over DiffPlugin,Editor: Preview/cancel/apply flow\n(Editability delegated to Editor)
    end

    User->>DiffPlugin: preview-document-update
    DiffPlugin->>Editor: emit preview request (no contenteditable toggles)
    Editor-->>User: handles editability and UI state
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I twitched my nose and read the patch tonight,
Hid three toggles where the Editor holds light.
Forks now leap straight to the server's gate,
No last-version block, no leave-that-commit fate.
I hop, I cheer — tidier burrow, prompt and bright. 🥕🐇

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
apps/saru/lib/editor/diff-plugin.ts (3)

26-27: Good call removing direct contenteditable flips; add a non-invasive preview signal for Editor/UI.

Directly mutating contenteditable on editorView.dom can desync ProseMirror’s internal editable state. To help the Editor component react (styling, toolbar state, etc.) without reintroducing that risk, set a data attribute when preview starts.

Please confirm the Editor component already toggles editable in sync with preview/apply/cancel; otherwise users could still edit while preview is shown.

Apply this small addition within the same block:

-          // Commented out: editorView.dom.setAttribute('contenteditable', 'false');
-          // Let the Editor component handle editability
+          // Let the Editor component handle editability; expose non-invasive signal for UI/logic.
+          editorView.dom.setAttribute('data-preview-active', 'true');

68-69: On cancel: clear preview signal and restore focus for better UX.

This keeps the Editor in charge of editable while ensuring UI state and caret are restored.

-        // Commented out: editorView.dom.setAttribute('contenteditable', 'true');
-        // Let the Editor component handle editability
+        // Clear preview signal; Editor controls editability.
+        editorView.dom.removeAttribute('data-preview-active');
+        // Optionally restore caret/focus.
+        editorView.focus();

112-116: On apply: clear preview signal, restore focus, and avoid dispatch after unmount by tracking the timeout.

Store the timeout id and clear it in destroy() to prevent a late dispatch if the view unmounts during the animation window.

-        setTimeout(finalizeApply, animationDuration);
-        // Commented out: editorView.dom.setAttribute('contenteditable', 'true');
-        // Let the Editor component handle editability
+        applyTimeoutId = window.setTimeout(finalizeApply, animationDuration);
+        // Clear preview signal; Editor controls editability.
+        editorView.dom.removeAttribute('data-preview-active');
+        editorView.focus();

Additions outside this hunk (for clarity):

// After lastPreviewContentRef declaration:
let applyTimeoutId: number | null = null;
// Inside destroy():
if (applyTimeoutId !== null) {
  clearTimeout(applyTimeoutId);
  applyTimeoutId = null;
}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 666479b and f81eaf3.

📒 Files selected for processing (1)
  • apps/saru/lib/editor/diff-plugin.ts (3 hunks)

@will-lp1 will-lp1 merged commit deda6bb into will-lp1:main Sep 2, 2025
1 of 3 checks passed
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
apps/saru/components/document/version-rail.tsx (1)

29-31: Fix long‑press detection: stale state captured in timeout prevents reliable triggering

The timeout closure captures an out-of-date pressStart value from the render when the timeout was scheduled. This can cause missed or spurious forks. Track press start with a ref and clear it on pointer up/leave.

Apply these diffs:

-  const [pressStart, setPressStart] = React.useState<number | null>(null);
+  const pressStartRef = React.useRef<number | null>(null);
   const handlePointerDown = (e: React.PointerEvent<HTMLDivElement>) => {
     maybeRefresh();
     const rect = e.currentTarget.getBoundingClientRect();
     const x = e.clientX - rect.left;
     const fraction = x / rect.width;
     const idx = Math.min(Math.max(Math.round(fraction * (versions.length - 1)), 0), versions.length - 1);
     
-    setPressStart(Date.now());
-    
-    setTimeout(() => {
-      if (pressStart !== null && Date.now() - pressStart >= 800) {
-        triggerFork(idx);
-        setPressStart(null);
-      }
-    }, 800);
+    const start = Date.now();
+    pressStartRef.current = start;
+    setTimeout(() => {
+      if (pressStartRef.current !== null && Date.now() - pressStartRef.current >= 800) {
+        triggerFork(idx);
+        pressStartRef.current = null;
+      }
+    }, 800);
   };
   const handlePointerUp = () => {
-    setPressStart(null);
+    pressStartRef.current = null;
   };
   const handlePointerLeave = () => {
-    setPressStart(null);
+    pressStartRef.current = null;
     if (hoverIndex !== null) {
       if (isViewingHistory && selectedIndex !== null && selectedIndex < versions.length - 1) {
         const v = versions[selectedIndex];
         window.dispatchEvent(
           new CustomEvent('preview-document-update', {
             detail: { documentId: baseDocumentId, newContent: v.content },
           })
         );
       } else {
         window.dispatchEvent(
           new CustomEvent('cancel-document-update', {
             detail: { documentId: baseDocumentId },
           })
         );
       }
       setHoverIndex(null);
     }

Optional: also store the timeout id in a ref and clear it on unmount/pointer cancel to avoid late triggers if the component unmounts within 800ms.

Also applies to: 171-186, 188-190, 192-194

🧹 Nitpick comments (3)
apps/saru/components/document/version-rail.tsx (3)

122-135: Fork payload timestamp: normalize to ISO and prefer updatedAt for latest version

Now that forking from the latest is allowed, createdAt may be stale. Also, createdAt is typed Date | string; passing a Date across the event boundary can break consumers expecting strings.

Apply this diff to compute a safe timestamp:

   window.dispatchEvent(
     new CustomEvent('version-fork', {
-      detail: {
-        originalDocumentId: baseDocumentId,
-        versionIndex: idx,
-        forkFromTimestamp: version.createdAt,
-      },
+      detail: {
+        originalDocumentId: baseDocumentId,
+        versionIndex: idx,
+        forkFromTimestamp: (() => {
+          const t = idx === versions.length - 1
+            ? ((version as any).updatedAt ?? version.createdAt)
+            : version.createdAt;
+          return typeof t === 'string' ? t : (t as Date).toISOString();
+        })(),
+      },
     })
   );

Please confirm the server expects an ISO string and whether it relies on this timestamp for validation or snapshot lookup.


230-239: Clear hover state when returning to latest to avoid immediate re-previews

After cancelling preview, also clear hoverIndex so moving the pointer inside the rail doesn’t re-dispatch a preview based on stale hover.

   const goToLatest = () => {
     const latestIndex = versions.length - 1;
     setSelectedIndex(latestIndex);
     onIndexChange(latestIndex);
+    setHoverIndex(null);
     window.dispatchEvent(
       new CustomEvent('cancel-document-update', {
         detail: { documentId: baseDocumentId },
       })
     );
   };

71-77: Remove redundant branch in effect

Both branches set selectedIndex(currentIndex). Simplify and reduce re-renders.

-  React.useEffect(() => {
-    if (currentIndex === versions.length - 1) {
-      setSelectedIndex(currentIndex);
-    } else {
-      setSelectedIndex(currentIndex);
-    }
-  }, [currentIndex, versions.length]);
+  React.useEffect(() => {
+    setSelectedIndex(currentIndex);
+  }, [currentIndex]);
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between f81eaf3 and dd6a125.

📒 Files selected for processing (2)
  • apps/saru/components/document/document-workspace.tsx (0 hunks)
  • apps/saru/components/document/version-rail.tsx (1 hunks)
💤 Files with no reviewable changes (1)
  • apps/saru/components/document/document-workspace.tsx
🔇 Additional comments (1)
apps/saru/components/document/version-rail.tsx (1)

192-212: Pointer-leave behavior change looks good; verify editability resets end-to-end

Dropping auto-commit to latest on leave should help UX. Please verify that cancel-document-update reliably restores editor editability after hovering history, including when versions refresh mid-hover.

@coderabbitai coderabbitai bot mentioned this pull request Sep 10, 2025
@coderabbitai coderabbitai bot mentioned this pull request Oct 20, 2025
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