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();