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:
@@ -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());
|
||||
|
||||
26
src/page.rs
26
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();
|
||||
|
||||
Reference in New Issue
Block a user