From 6565352ab509eef9f0104f90afa49c20ceb66dd4 Mon Sep 17 00:00:00 2001 From: Richard Wu Date: Wed, 11 Nov 2026 14:19:16 +0800 Subject: [PATCH] fix(editor): track dirty changes after the first autosave CodeMirror 5 change handlers receive (instance, changeObj). The handler named the first argument change, so once the fold gutter populated __mdpFenceCache it threw on change.from.line before setDirty(true). Edits after the first autosave stayed clean: Ctrl+S and Ctrl+E skipped saving and the preview stayed stale. Use the correct signature, return early for remote setValue, and guard the cache update. Add a page regression test. --- assets/page/page.js | 10 +++++++--- src/page.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/assets/page/page.js b/assets/page/page.js index b73f13c..b24d2a1 100644 --- a/assets/page/page.js +++ b/assets/page/page.js @@ -746,11 +746,15 @@ return; } ed = createCodeMirrorEditor(cm); - cm.on('change', function(change) { - if (cm.__mdpFenceCache) { + // CodeMirror 5 calls change handlers as (instance, changeObj). Naming the + // first argument `change` made `change.from` undefined; once the fold + // gutter had populated __mdpFenceCache this threw before setDirty(true), + // so edits after the first autosave never saved. Keep the object second. + cm.on('change', function(instance, change) { + if (applyingRemote) return; + if (cm.__mdpFenceCache && change && change.from) { cm.__mdpFenceCache.validTo = Math.min(cm.__mdpFenceCache.validTo, change.from.line); } - if (applyingRemote) return; setDirty(true); scheduleAutosave(); updateDocumentStats(ed.value()); diff --git a/src/page.rs b/src/page.rs index 4f08161..f35a61d 100644 --- a/src/page.rs +++ b/src/page.rs @@ -264,6 +264,32 @@ mod tests { assert!(!page.contains("CodeMirror.defineMode")); } + #[test] + fn editor_change_handler_uses_the_codemirror_instance_and_change_object() { + let strings = Strings::english(); + let page = build_page( + &md_to_html("# Hello"), + "# Hello", + None, + EnhanceFlags::default(), + &strings, + false, + ); + + // CodeMirror 5 calls change handlers as (instance, changeObj). Using the + // first argument as the change object made change.from undefined; once + // the fold cache existed, the handler threw before setDirty(true), so + // edits after the first autosave were never marked dirty or saved. + assert!(page.contains("cm.on('change', function(instance, change) {")); + assert!( + page.contains( + "if (applyingRemote) return;\n if (cm.__mdpFenceCache && change && change.from) {" + ), + "the remote guard must run before the fold-cache update" + ); + assert!(!page.contains("cm.on('change', function(change) {")); + } + #[test] fn edit_mode_find_bar_has_clickable_controls_and_no_escape_exit() { let strings = Strings::english();