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.
This commit is contained in:
2026-11-11 14:19:16 +08:00
parent f1bda62d84
commit 6565352ab5
2 changed files with 33 additions and 3 deletions

View File

@@ -746,11 +746,15 @@
return; return;
} }
ed = createCodeMirrorEditor(cm); ed = createCodeMirrorEditor(cm);
cm.on('change', function(change) { // CodeMirror 5 calls change handlers as (instance, changeObj). Naming the
if (cm.__mdpFenceCache) { // 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); cm.__mdpFenceCache.validTo = Math.min(cm.__mdpFenceCache.validTo, change.from.line);
} }
if (applyingRemote) return;
setDirty(true); setDirty(true);
scheduleAutosave(); scheduleAutosave();
updateDocumentStats(ed.value()); updateDocumentStats(ed.value());

View File

@@ -264,6 +264,32 @@ mod tests {
assert!(!page.contains("CodeMirror.defineMode")); 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] #[test]
fn edit_mode_find_bar_has_clickable_controls_and_no_escape_exit() { fn edit_mode_find_bar_has_clickable_controls_and_no_escape_exit() {
let strings = Strings::english(); let strings = Strings::english();