Reuse page prefixes for structural route-D edits

Extract Engine::relayout_from from Engine::edit so a caller can rebuild from any checkpoint with an unchanged unit prefix. Route-D project previews now use the first differing paragraph for both text and structural edits, keeping sealed pages before the change instead of always relaying out the whole document.
This commit is contained in:
gloamxun committed 2026-10-21 08:53:17 +08:00
1 parent 8cbd72663a
commit f2deac541e
4 files changed
+113 -58

No files matched your search

+52 -7
View File
@@ -1118,14 +1118,31 @@ impl Engine {
return;
};
next.units[unit_index] = Unit::paragraph_with_style(unit_id, source, style);
self.relayout_from(&next, unit_index);
}
/// Rebuild pages from the checkpoint before `start_index`, keeping every
/// sealed page before it. This generalizes `edit` to structural changes:
/// callers may add, remove or replace units at and after `start_index` as
/// long as the prefix is unchanged.
pub fn relayout_from(&mut self, document: &Document, start_index: usize) {
if start_index == 0 || start_index >= self.layout.checkpoints.len() {
self.layout = Self::layout(document).layout;
self.layout.recomputed_pages = self.layout.pages.len();
self.layout.changed_pages = (0..self.layout.pages.len()).collect();
return;
}
self.relayout_from_checkpoint(document, start_index);
}
fn relayout_from_checkpoint(&mut self, next: &Document, start_index: usize) {
let mut checkpoint =
self.layout
.checkpoints
.get(unit_index)
.get(start_index)
.cloned()
.unwrap_or(Checkpoint {
unit_index,
unit_index: start_index,
page_index: 0,
used: 0,
page_items: Vec::new(),
@@ -1134,7 +1151,7 @@ impl Engine {
page_marks: Vec::new(),
page_floats: Vec::new(),
});
let edited_height: i64 = next.units[unit_index]
let edited_height: i64 = next.units[start_index]
.items
.iter()
.map(|item| match item {
@@ -1167,9 +1184,9 @@ impl Engine {
};
let mut used = checkpoint.used;
let mut insertion_height = checkpoint.insertion_height;
let mut checkpoints = self.layout.checkpoints[..unit_index].to_vec();
let mut checkpoints = self.layout.checkpoints[..start_index].to_vec();
for (index, unit) in next.units.iter().enumerate().skip(unit_index) {
for (index, unit) in next.units.iter().enumerate().skip(start_index) {
checkpoints.push(Checkpoint {
unit_index: index,
page_index: pages.len(),
@@ -1193,7 +1210,7 @@ impl Engine {
< next.float_page_fraction_percent as i64 * next.page_goal / 100
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
used = next.top_skip;
insertion_height = 0;
}
match item {
@@ -1387,7 +1404,7 @@ impl Engine {
current.floats.push(float.clone());
} else {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
used = next.top_skip;
insertion_height = 0;
current.floats.push(float.clone());
}
@@ -1841,6 +1858,34 @@ mod tests {
assert_eq!(layout.pages()[1].insertions.len(), 1);
}
#[test]
fn relayout_from_keeps_the_unchanged_page_prefix() {
let first = Unit::paragraph(0, "alpha");
let mut document = Document::new(
20,
vec![
first.clone(),
Unit::paragraph(1, "beta"),
Unit::paragraph(2, "gamma"),
],
);
let mut engine = Engine::layout(&document);
let prefix_hash = engine.pages()[0].hash;
document = Document::new(
20,
vec![
first,
Unit::paragraph(3, "inserted"),
Unit::paragraph(1, "beta changed"),
Unit::paragraph(2, "gamma"),
],
);
engine.relayout_from(&document, 1);
assert_eq!(engine.pages()[0].hash, prefix_hash);
assert!(engine.pages().len() >= 2);
assert!(engine.changed_pages().iter().all(|page| *page >= 1));
}
#[test]
fn incremental_layout_respects_insertion_class_limits() {
let rule = VerticalItem::Rule {