Skip to main content

invert

Function invert 

Source
pub fn invert(
    base: &Node,
    changes: &[Change],
) -> Result<Vec<Change>, ApplyError>
Expand description

Invert a change list: produce the reverse changes that, applied to the result of apply(base, changes), restore base — the basis for undo.

Computed as diff(apply(base, changes), base): replay the forward changes, then diff back to base. This reuses the diff round-trip guarantee, so it handles every value/shape edge exactly — subject to the same non-minimality caveats as diff (e.g. no move detection). Errors only if changes itself doesn’t apply to base.

use tiptap_rusty_parser::Document;
let a = Document::from_json_str(r#"{"type":"doc","content":[{"type":"paragraph"}]}"#).unwrap();
let b = Document::from_json_str(r#"{"type":"doc","content":[{"type":"heading"}]}"#).unwrap();
let forward = a.diff(&b);
let undo = a.invert(&forward).unwrap();
let mut c = b.clone();
c.apply(&undo).unwrap();
assert_eq!(c, a);