Keep structural units out of text edit reflow
This commit is contained in:
1 parent
753aac632d
commit
c98ec0056f
2 files changed
+42
-2
No files matched your search
@@ -138,6 +138,11 @@ pub struct Unit {
|
|||||||
pub id: UnitId,
|
pub id: UnitId,
|
||||||
pub source: String,
|
pub source: String,
|
||||||
pub items: Vec<VerticalItem>,
|
pub items: Vec<VerticalItem>,
|
||||||
|
/// Present when this unit was built from text and can be re-flowed by
|
||||||
|
/// []. Structural units (halign, floats, insertions) are not
|
||||||
|
/// editable through a plain source string.
|
||||||
|
#[serde(default)]
|
||||||
|
pub paragraph_style: Option<ParagraphStyle>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Unit {
|
impl Unit {
|
||||||
@@ -192,6 +197,7 @@ impl Unit {
|
|||||||
Self {
|
Self {
|
||||||
id,
|
id,
|
||||||
source: "halign".to_owned(),
|
source: "halign".to_owned(),
|
||||||
|
paragraph_style: None,
|
||||||
items: vec![VerticalItem::Halign(Halign {
|
items: vec![VerticalItem::Halign(Halign {
|
||||||
rows,
|
rows,
|
||||||
column_widths,
|
column_widths,
|
||||||
@@ -222,6 +228,7 @@ impl Unit {
|
|||||||
width: paragraph.width.max(style.line_width),
|
width: paragraph.width.max(style.line_width),
|
||||||
height: paragraph.height,
|
height: paragraph.height,
|
||||||
})],
|
})],
|
||||||
|
paragraph_style: Some(style),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,6 +238,7 @@ impl Unit {
|
|||||||
id,
|
id,
|
||||||
source: String::new(),
|
source: String::new(),
|
||||||
items,
|
items,
|
||||||
|
paragraph_style: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -537,8 +545,12 @@ impl Engine {
|
|||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let mut next = document.clone();
|
let mut next = document.clone();
|
||||||
next.units[unit_index].source = source.into();
|
let source = source.into();
|
||||||
next.units[unit_index] = Unit::paragraph(unit_id, next.units[unit_index].source.clone());
|
let Some(style) = next.units[unit_index].paragraph_style.clone() else {
|
||||||
|
next.units[unit_index].source = source;
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
next.units[unit_index] = Unit::paragraph_with_style(unit_id, source, style);
|
||||||
|
|
||||||
let mut checkpoint =
|
let mut checkpoint =
|
||||||
self.layout
|
self.layout
|
||||||
@@ -754,6 +766,7 @@ mod tests {
|
|||||||
let unit = Unit {
|
let unit = Unit {
|
||||||
id: 7,
|
id: 7,
|
||||||
source: "footnote layout".to_owned(),
|
source: "footnote layout".to_owned(),
|
||||||
|
paragraph_style: None,
|
||||||
items: vec![
|
items: vec![
|
||||||
VerticalItem::Insertion(Insertion {
|
VerticalItem::Insertion(Insertion {
|
||||||
class: 0,
|
class: 0,
|
||||||
@@ -785,6 +798,7 @@ mod tests {
|
|||||||
let unit = |id| Unit {
|
let unit = |id| Unit {
|
||||||
id,
|
id,
|
||||||
source: "boxes".to_owned(),
|
source: "boxes".to_owned(),
|
||||||
|
paragraph_style: None,
|
||||||
items: vec![
|
items: vec![
|
||||||
VerticalItem::Box(VerticalBox {
|
VerticalItem::Box(VerticalBox {
|
||||||
width: 10,
|
width: 10,
|
||||||
@@ -816,6 +830,7 @@ mod tests {
|
|||||||
let unit = Unit {
|
let unit = Unit {
|
||||||
id: 0,
|
id: 0,
|
||||||
source: "penalty".to_owned(),
|
source: "penalty".to_owned(),
|
||||||
|
paragraph_style: None,
|
||||||
items: vec![
|
items: vec![
|
||||||
VerticalItem::Box(VerticalBox {
|
VerticalItem::Box(VerticalBox {
|
||||||
width: 10,
|
width: 10,
|
||||||
@@ -842,6 +857,7 @@ mod tests {
|
|||||||
vec![Unit {
|
vec![Unit {
|
||||||
id: 0,
|
id: 0,
|
||||||
source: text.to_owned(),
|
source: text.to_owned(),
|
||||||
|
paragraph_style: None,
|
||||||
items: vec![
|
items: vec![
|
||||||
VerticalItem::Box(VerticalBox {
|
VerticalItem::Box(VerticalBox {
|
||||||
width: 10,
|
width: 10,
|
||||||
@@ -899,6 +915,7 @@ mod tests {
|
|||||||
let unit = Unit {
|
let unit = Unit {
|
||||||
id: 0,
|
id: 0,
|
||||||
source: "float".to_owned(),
|
source: "float".to_owned(),
|
||||||
|
paragraph_style: None,
|
||||||
items: vec![
|
items: vec![
|
||||||
VerticalItem::Box(VerticalBox {
|
VerticalItem::Box(VerticalBox {
|
||||||
width: 10,
|
width: 10,
|
||||||
@@ -927,6 +944,7 @@ mod tests {
|
|||||||
let unit = Unit {
|
let unit = Unit {
|
||||||
id: 0,
|
id: 0,
|
||||||
source: "bottom float".to_owned(),
|
source: "bottom float".to_owned(),
|
||||||
|
paragraph_style: None,
|
||||||
items: vec![
|
items: vec![
|
||||||
VerticalItem::Box(VerticalBox {
|
VerticalItem::Box(VerticalBox {
|
||||||
width: 10,
|
width: 10,
|
||||||
@@ -950,6 +968,25 @@ mod tests {
|
|||||||
assert_eq!(engine.layout.pages[1].items.len(), 1);
|
assert_eq!(engine.layout.pages[1].items.len(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn edit_ignores_structural_units() {
|
||||||
|
let rows = vec![HalignRow {
|
||||||
|
cells: vec![HalignCell {
|
||||||
|
text: "A".to_owned(),
|
||||||
|
span: 1,
|
||||||
|
alignment: CellAlignment::Left,
|
||||||
|
}],
|
||||||
|
rule: false,
|
||||||
|
}];
|
||||||
|
let document = Document::new(100, vec![Unit::halign(0, rows, 1, 0)]);
|
||||||
|
let mut engine = Engine::layout(&document);
|
||||||
|
let before: Vec<_> = engine.pages().iter().map(|page| page.items.len()).collect();
|
||||||
|
engine.edit(&document, 0, "replacement text");
|
||||||
|
assert_eq!(engine.recomputed_pages(), 0);
|
||||||
|
let after: Vec<_> = engine.pages().iter().map(|page| page.items.len()).collect();
|
||||||
|
assert_eq!(before, after);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn insertion_reserves_page_height() {
|
fn insertion_reserves_page_height() {
|
||||||
let boxed = |height: i64| VerticalItem::Box(VerticalBox { width: 100, height });
|
let boxed = |height: i64| VerticalItem::Box(VerticalBox { width: 100, height });
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ operation from the start. It does **not** embed pdfTeX, XeTeX or Tectonic.
|
|||||||
- `Engine::edit` which restores the checkpoint before the edited unit,
|
- `Engine::edit` which restores the checkpoint before the edited unit,
|
||||||
re-executes only the suffix, compares page hashes, and reports
|
re-executes only the suffix, compares page hashes, and reports
|
||||||
`recomputed_pages`;
|
`recomputed_pages`;
|
||||||
|
- `Unit::paragraph_style` records whether a unit is text and can be
|
||||||
|
re-flowed by `Engine::edit`; structural units (halign, insertion, float)
|
||||||
|
are never rewritten from a plain source string.
|
||||||
- serde round-trip for checkpoints.
|
- serde round-trip for checkpoints.
|
||||||
|
|
||||||
M0 proves the incremental contract; M1 starts replacing the placeholder with
|
M0 proves the incremental contract; M1 starts replacing the placeholder with
|
||||||
|
|||||||
Reference in new issue
Block a user