Skip to content

fix(editor): save rich-markdown preview edits on blur, switch, and quit before the serialize debounce (#9730) - #9823

Merged
brennanb2025 merged 2 commits into
stablyai:mainfrom
rodboev:pr/fix-rich-markdown-preview-save-gap
Jul 30, 2026
Merged

brennanb2025 merged 2 commits into
stablyai:mainfrom
rodboev:pr/fix-rich-markdown-preview-save-gap

Conversation

@rodboev

@rodboev rodboev commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fast edits in Rich Markdown preview could remain inside the 300 ms serialization buffer when the editor blurred. The file was marked dirty, but the existing autosave controller did not yet have a draft to persist.

This PR flushes pending Rich Markdown serialization on blur so the existing draft and autosave pipeline handles the save:

  • blur publishes the pending draft without writing to disk directly
  • autosave suspension and changed-on-disk protection remain authoritative
  • existing write deduplication and SSH routing remain unchanged
  • the 300 ms typing debounce remains unchanged

Closes #9730.

Testing

  • pnpm typecheck
  • pnpm lint
  • 3 focused test files, 21 tests
  • focused oxfmt --check
  • live Electron validation: blur inside the debounce preserved the draft, autosave-off did not write disk, and re-enabling autosave saved through the central controller

Performance and security

No new IPC, disk-write path, listener, polling loop, subprocess, cache, or trust boundary is introduced. Blur uses the existing bounded serialization flush.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Rich Markdown preview editing now tracks dirty state in a ref and exposes a flush-and-save callback. Dirty previews flush pending editor changes and request a file save on blur and component unmount, while clean previews do nothing. New tests cover dirty blur, dirty unmount, and clean lifecycle behavior before debounce publication.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ⚠️ Warning It covers summary, testing, and security, but omits the required Screenshots, AI Review Report, and Notes sections. Add the missing template sections, including screenshots, an AI review report with cross-platform checks, and notes or follow-up details.
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes flush and save dirty preview edits on blur and unmount, matching issue #9730's data-loss fix.
Out of Scope Changes check ✅ Passed The touched files and new test stay focused on preview-save behavior; no unrelated changes stand out.
Title check ✅ Passed The title is specific and accurately describes the main behavior change: saving rich-markdown preview edits on blur/teardown before debounce.

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

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/renderer/src/components/editor/rich-markdown-preview-save.test.ts (1)

50-54: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Exercise the registered serialization flush.

registerPendingEditorFlush and flushPendingEditorChange are both no-op mocks, so these tests do not prove that "edited" reaches onContentChange before saving. Retain the registered callback, invoke it from the flush mock, and assert the content publication; otherwise a broken registration or file-ID mismatch can still pass while saving stale content.

Also applies to: 194-224


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: da920391-928b-42ce-b6f9-41b68ac16efa

📥 Commits

Reviewing files that changed from the base of the PR and between 261a7b7 and 42358342aea57282724a174e0340eba01b7195cb.

📒 Files selected for processing (3)
  • src/renderer/src/components/editor/RichMarkdownEditor.tsx
  • src/renderer/src/components/editor/rich-markdown-editor-config.ts
  • src/renderer/src/components/editor/rich-markdown-preview-save.test.ts

@rodboev
rodboev force-pushed the pr/fix-rich-markdown-preview-save-gap branch from 4235834 to 56ef0ed Compare July 22, 2026 02:27
@nwparker nwparker added the bug Something isn't working label Jul 27, 2026
@WYK15

WYK15 commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Hi @rodboev , @brennanb2025 , thanks for the fix! Just a quick heads-up that this branch currently has conflicts that need to be resolved and a workflow awaiting approval. Could you please take a look when you have a moment?

@rodboev
rodboev force-pushed the pr/fix-rich-markdown-preview-save-gap branch from 56ef0ed to a4e0da7 Compare July 30, 2026 03:09
@rodboev
rodboev force-pushed the pr/fix-rich-markdown-preview-save-gap branch from a4e0da7 to 58986b7 Compare July 30, 2026 03:09
@rodboev

rodboev commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main and resolved the conflict.

Focused reruns on the new head passed for the rich-preview regression test, oxlint, and oxfmt --check. pnpm run typecheck:web still fails in src/shared/emoji-shortcode-catalog.ts because emojibase-data/en/shortcodes/github.json is missing, and I reproduced that unchanged on clean origin/main at 8ad944890, so that part isn't from this PR.

Greptile is running on the new head now.

@brennanb2025 brennanb2025 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice focused fix. Blur now flushes pending Rich Markdown serialization into the existing draft/autosave path, preserving fast edits without bypassing autosave suspension or changed-on-disk protection. Verified with the 21-test focused suite, typecheck/lint, and live Electron validation.

Comment on lines +109 to +125
it('flushes pending serialization when the rich editor blurs', () => {
const setMarkdownEditorFocused = vi.fn()
vi.stubGlobal('window', {
api: { ui: { setMarkdownEditorFocused } }
})
const flushPendingSerialization = vi.fn()
const clearAnnotationTarget = vi.fn()
const config = createRichMarkdownEditorConfig(
createConfigParams({ clearAnnotationTarget, flushPendingSerialization })
)

config.onBlur?.({} as never)

expect(setMarkdownEditorFocused).toHaveBeenCalledWith(false)
expect(clearAnnotationTarget).toHaveBeenCalledOnce()
expect(flushPendingSerialization).toHaveBeenCalledOnce()
})

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 Testing checklist references a non-existent file

The PR description's testing checklist checks off running rich-markdown-preview-save.test.ts, but that file does not exist anywhere in the repository. The PR notes also describe the regression harness as one that "mounts RichMarkdownEditor, drives its real rich-editor config through an edit, advances fake timers by 299 ms" — but the actual test added here exercises only createRichMarkdownEditorConfig directly with a mock flushPendingSerialization. Anyone trying to reproduce the listed test run would get an error on that command.

@greptile-apps

greptile-apps Bot commented Jul 30, 2026

Copy link
Copy Markdown

Greptile Summary

This PR fixes a data-loss window in the rich-markdown preview editor where fast edits could be dropped if the user blurred the editor, switched tabs, or quit before the 300 ms serialization debounce fired. The fix adds a single flushPendingSerialization() call to the onBlur handler in createRichMarkdownEditorConfig, matching the behavior already present in the useLayoutEffect teardown path.

  • rich-markdown-editor-config.ts: onBlur now calls params.flushPendingSerialization() immediately after clearing the annotation target, cancelling any pending debounce timer and synchronously committing serialization + propagating via onContentChange.
  • rich-markdown-editor-config.test.ts: Adds afterEach(vi.unstubAllGlobals) cleanup and a new focused test confirming flushPendingSerialization is invoked on blur alongside the existing setMarkdownEditorFocused and clearAnnotationTarget calls.

Confidence Score: 4/5

Safe to merge. The production change is a single-line addition to an existing event handler; the flush function is idempotent and cannot cause duplicate serialization.

The core fix is minimal and correct. The PR testing checklist checks off a test file that does not exist in the repository, meaning the described integration test was never run. The added unit test only verifies the mock is called, not that the full save pipeline completes.

Files Needing Attention: No files require special attention beyond the discrepancy noted in the test file checklist reference.

Important Files Changed

Filename Overview
src/renderer/src/components/editor/rich-markdown-editor-config.ts Added params.flushPendingSerialization() call to onBlur handler, mirroring the existing unmount path. The change is minimal, correct, and idempotent since flushPendingSerialization short-circuits when no timer is pending.
src/renderer/src/components/editor/rich-markdown-editor-config.test.ts Added afterEach(vi.unstubAllGlobals) cleanup and a new test that verifies flushPendingSerialization is called on blur. Test is correct and covers the new behavior, but the PR checklist references a different test file (rich-markdown-preview-save.test.ts) that does not exist.

Reviews (1): Last reviewed commit: "fix(editor): keep rich markdown blur sav..." | Re-trigger Greptile

@brennanb2025
brennanb2025 merged commit 38e9581 into stablyai:main Jul 30, 2026
1 check passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…it before the serialize debounce (stablyai#9730) (stablyai#9823)

* fix(editor): flush markdown preview saves before teardown (stablyai#9730)

* fix(editor): keep rich markdown blur saves policy-safe

---------

Co-authored-by: Brennan Benson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Markdown changes are not auto-saved when editing in preview mode on Windows

4 participants