use super::{Axis, Shift};
use crate::coordinate::{Col, Row};
use crate::model::Spreadsheet;
use crate::model::chart::{Anchor, EditAs, Marker};
pub(super) fn move_anchors(book: &mut Spreadsheet, sheet: usize, shift: &Shift) {
let Some(target) = book.sheet(sheet) else {
return;
};
let parts: Vec<String> = target
.attachments
.iter()
.filter(|a| matches!(a.role(), "drawing" | "vmlDrawing"))
.map(|a| a.target.clone())
.collect();
for part in &mut book.parts {
if !parts.iter().any(|p| p == &part.path) {
continue;
}
let Ok(text) = core::str::from_utf8(&part.data) else {
continue;
};
part.data = rewrite(text, shift).into_bytes();
}
}
pub(super) fn move_anchor(anchor: &mut Anchor, shift: &Shift) {
let (from, to, resizes) = match anchor {
Anchor::TwoCell {
edit_as: Some(EditAs::Absolute),
..
}
| Anchor::Absolute { .. } => return,
Anchor::TwoCell { from, to, edit_as } => {
(from, Some(to), *edit_as != Some(EditAs::OneCell))
}
Anchor::OneCell { from, .. } => (from, None, true),
};
let index = |m: &Marker| match shift.axis {
Axis::Rows => m.row.index(),
Axis::Columns => m.col.index(),
};
let set = |m: &mut Marker, i: u32| match shift.axis {
Axis::Rows => m.row = Row::new(i).unwrap_or(m.row),
Axis::Columns => m.col = Col::new(i).unwrap_or(m.col),
};
let start = index(from);
let moved = shift.moved_edge(start, true).unwrap_or(start);
set(from, moved);
if let Some(to) = to {
let end = index(to);
let moved_end = if resizes {
shift.moved_edge(end, false)
} else {
u32::try_from(i64::from(end) + i64::from(moved) - i64::from(start))
.ok()
.filter(|&i| i <= shift.ceiling())
};
set(to, moved_end.unwrap_or(end));
}
}
fn tags(axis: Axis) -> [&'static str; 2] {
match axis {
Axis::Rows => ["xdr:row", "x:Row"],
Axis::Columns => ["xdr:col", "x:Column"],
}
}
fn rewrite(xml: &str, shift: &Shift) -> String {
let mut out = String::with_capacity(xml.len());
let mut rest = xml;
let mut lead: Option<i64> = None;
let mut absolute = false;
let mut resizes = true;
while let Some(open) = rest.find('<') {
out.push_str(&rest[..open]);
rest = &rest[open..];
let Some(close) = rest.find('>') else {
break;
};
let tag = &rest[1..close];
let name = tag.split([' ', '/', '\t', '\n']).next().unwrap_or(tag);
match name {
"xdr:twoCellAnchor" | "xdr:oneCellAnchor" | "xdr:absoluteAnchor" => {
lead = None;
absolute = name == "xdr:absoluteAnchor" || tag.contains(r#"editAs="absolute""#);
resizes = !tag.contains(r#"editAs="oneCell""#);
}
"xdr:from" | "x:ClientData" => lead = None,
_ => {}
}
out.push_str(&rest[..=close]);
rest = &rest[close + 1..];
if absolute || !tags(shift.axis).contains(&name) {
continue;
}
let Some(end) = rest.find('<') else {
break;
};
let Ok(index) = rest[..end].trim().parse::<u32>() else {
continue;
};
let moved = match (lead, resizes) {
(Some(delta), false) => u32::try_from(i64::from(index) + delta)
.ok()
.filter(|&i| i <= shift.ceiling()),
_ => shift.moved_edge(index, lead.is_none()),
};
let moved = moved.unwrap_or(index);
if lead.is_none() {
lead = Some(i64::from(moved) - i64::from(index));
}
out.push_str(&moved.to_string());
rest = &rest[end..];
}
out.push_str(rest);
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::edit::{Axis, Shift};
const DRAWING: &str = concat!(
r#"<xdr:wsDr><xdr:twoCellAnchor>"#,
r#"<xdr:from><xdr:col>1</xdr:col><xdr:colOff>7</xdr:colOff>"#,
r#"<xdr:row>4</xdr:row><xdr:rowOff>9</xdr:rowOff></xdr:from>"#,
r#"<xdr:to><xdr:col>3</xdr:col><xdr:colOff>7</xdr:colOff>"#,
r#"<xdr:row>8</xdr:row><xdr:rowOff>9</xdr:rowOff></xdr:to>"#,
r#"</xdr:twoCellAnchor></xdr:wsDr>"#,
);
fn rows(xml: &str) -> Vec<u32> {
xml.split("<xdr:row>")
.skip(1)
.filter_map(|s| s.split('<').next()?.parse().ok())
.collect()
}
#[test]
fn an_insert_above_moves_the_whole_object() {
let out = rewrite(DRAWING, &Shift::insert(Axis::Rows, 0, 3));
assert_eq!(rows(&out), [7, 11]);
assert!(out.contains("<xdr:rowOff>9</xdr:rowOff>"), "{out}");
}
#[test]
fn an_insert_inside_stretches_the_default_anchor() {
let out = rewrite(DRAWING, &Shift::insert(Axis::Rows, 6, 2));
assert_eq!(rows(&out), [4, 10]);
}
#[test]
fn an_object_that_keeps_its_size_does_not_stretch() {
let pinned = DRAWING.replace(
"<xdr:twoCellAnchor>",
r#"<xdr:twoCellAnchor editAs="oneCell">"#,
);
let out = rewrite(&pinned, &Shift::insert(Axis::Rows, 6, 2));
assert_eq!(rows(&out), [4, 8]);
let out = rewrite(&pinned, &Shift::insert(Axis::Rows, 0, 3));
assert_eq!(rows(&out), [7, 11]);
}
#[test]
fn an_absolute_anchor_stays_put() {
let pinned = DRAWING.replace(
"<xdr:twoCellAnchor>",
r#"<xdr:twoCellAnchor editAs="absolute">"#,
);
let out = rewrite(&pinned, &Shift::insert(Axis::Rows, 0, 3));
assert_eq!(rows(&out), [4, 8]);
}
#[test]
fn a_removal_pulls_the_corner_in() {
let out = rewrite(DRAWING, &Shift::remove(Axis::Rows, 3, 3));
assert_eq!(rows(&out), [3, 5]);
}
#[test]
fn a_comment_box_moves_too() {
let vml = r#"<x:ClientData ObjectType="Note"><x:Anchor>10,12,12,88</x:Anchor><x:Row>13</x:Row><x:Column>3</x:Column></x:ClientData>"#;
let out = rewrite(vml, &Shift::insert(Axis::Rows, 2, 5));
assert!(out.contains("<x:Row>18</x:Row>"), "{out}");
assert!(out.contains("<x:Anchor>10,12,12,88</x:Anchor>"), "{out}");
let out = rewrite(vml, &Shift::insert(Axis::Columns, 0, 1));
assert!(out.contains("<x:Column>4</x:Column>"), "{out}");
assert!(out.contains("<x:Row>13</x:Row>"), "{out}");
}
}