Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge copy-notes: Text: footnotes insert into a story whose references lack footnotes

aiongg25aa2735b8ab
diff --cc crates/doc/src/edit.rs
index 3a224d039,0957ba5ab..da843b4cb
--- a/crates/doc/src/edit.rs
+++ b/crates/doc/src/edit.rs
@@@ -571,8 -400,9 +573,9 @@@ impl Document
st.xrefs.clear();
st.index_refs.clear();
st.objects.clear();
+ st.editorial.clear();
st.fix_marks();
- st.rev += 1;
+ st.rev = st.rev.wrapping_add(1);
Ok(())
}
}
diff --cc crates/doc/src/slice.rs
index e188276dd,a12101783..35006324b
--- a/crates/doc/src/slice.rs
+++ b/crates/doc/src/slice.rs
@@@ -123,66 -147,17 +147,72 @@@ impl Story
}
self.fix_tables();
}
- self.rev += 1;
+ self.rev = self.rev.wrapping_add(1);
end
}
+
+ /// Append the whole of `other` as new paragraphs after this story's text (threading two
+ /// stories merges them this way). Its formatting, notes, endnotes, editorial notes, markers,
+ /// anchored objects and tables come along; footnotes, endnotes, editorial notes and tables get
+ /// ids that are free in this story. An empty story simply takes `other`'s content. Frames stay.
+ pub fn append_story(&mut self, mut other: Story) {
+ if other.text.is_empty() {
+ return;
+ }
+ if self.text.is_empty() {
+ let (id, frames, rev, link) = (self.id, std::mem::take(&mut self.frames), self.rev, self.link.take());
+ *self = Story { id, frames, rev: rev.saturating_add(1), link, ..other };
+ return;
+ }
+ if !other.tables.is_empty() {
+ let mut next = self.tables.keys().max().copied().unwrap_or(0).saturating_add(1);
+ let mut ids = std::collections::HashMap::new();
+ for (old, t) in std::mem::take(&mut other.tables) {
+ let new = next;
+ next = next.saturating_add(1);
+ let mut t = Arc::unwrap_or_clone(t);
+ t.id = new;
+ ids.insert(old, new);
+ self.tables.insert(new, Arc::new(t));
+ }
+ for p in &mut other.paras {
+ p.table = p.table.and_then(|id| ids.get(&id).copied());
+ }
+ }
+ self.text.push('\n');
+ self.text.push_str(&other.text);
+ // The paragraph end takes the last run's format.
+ if let Some(last) = self.chars.last_mut() {
+ last.len += 1;
+ }
+ self.chars.extend(other.chars.into_iter().filter(|r| r.len > 0));
+ self.paras.extend(other.paras);
+ for n in other.notes {
+ let id = self.next_note_id();
+ self.notes.push(Arc::new(crate::notes::Footnote { id, text: n.text.clone() }));
+ }
+ for n in other.endnotes {
+ let id = self.endnotes.iter().map(|x| x.id).max().unwrap_or(0).saturating_add(1);
+ self.endnotes.push(Arc::new(crate::notes::Footnote { id, text: n.text.clone() }));
+ }
+ for n in other.editorial {
+ let id = self.editorial.iter().map(|x| x.id).max().unwrap_or(0).saturating_add(1);
+ self.editorial.push(Arc::new(crate::endnotes::EditorialNote { id, ..(*n).clone() }));
+ }
+ self.anchors.extend(other.anchors);
+ self.xrefs.extend(other.xrefs);
+ self.index_refs.extend(other.index_refs);
+ self.objects.extend(other.objects);
+ self.rev = self.rev.saturating_add(1);
+ }
}
+ /// The payloads of the markers numbered `span` (in text order), as far as `list` holds them.
+ fn marked<T: Clone>(list: &[T], span: std::ops::Range<usize>) -> Vec<T> {
+ let n = list.len();
+ list.get(span.start.min(n)..span.end.min(n)).map(<[T]>::to_vec).unwrap_or_default()
+ }
+
#[cfg(test)]
mod tests {
use super::*;
@@@ -215,21 -190,64 +245,82 @@@
assert!(a.extract(2..8).notes.is_empty());
}
+ #[test]
+ fn append_story_with_maximal_ids_does_not_overflow() {
+ let mut a = Story::with_text(StoryId(1), "Alpha", ParaFormat::default());
+ a.tables.insert(u64::MAX, Arc::new(crate::table::Table::new(u64::MAX, 1, 1, 0, 0, 100.0)));
+ let note = |id| Arc::new(crate::notes::Footnote { id, text: Story::with_text(StoryId(0), "n", ParaFormat::default()) });
+ a.endnotes.push(note(u64::MAX));
+ a.notes.push(note(u64::MAX));
+ a.rev = u64::MAX;
+ let mut b = Story::with_text(StoryId(2), "Beta", ParaFormat::default());
+ b.tables.insert(1, Arc::new(crate::table::Table::new(1, 1, 1, 0, 0, 100.0)));
+ b.endnotes.push(note(1));
+ b.notes.push(note(1));
+ a.append_story(b);
+ assert_eq!(a.text, "Alpha\nBeta");
+ assert_eq!(a.endnotes.len(), 2);
+ assert_eq!(a.notes.len(), 2);
+ }
++
+ #[test]
+ fn copy_paste_keeps_endnotes_and_editorial_notes() {
+ let mut a = Story::with_text(StoryId(1), "One two three", ParaFormat::default());
+ a.insert_endnote(3, "An endnote.", ParaFormat::default());
+ let at = a.text.find("three").unwrap();
+ a.insert(at, &NOTE_MARK.to_string());
+ let n = Arc::make_mut(&mut a.editorial[0]);
+ (n.author, n.text) = ("Ed".into(), "Check this.".into());
+ a.check().unwrap();
+ let slice = a.extract(0..a.len());
+ slice.check().unwrap();
+ assert_eq!(slice.endnotes[0].text.text, "An endnote.");
+ assert_eq!(slice.editorial[0].text, "Check this.");
+ // Duplicate into the same story: the copies get their own ids and are independent.
+ let len = a.len();
+ a.insert_story(len, &slice);
+ a.check().unwrap();
+ assert_eq!(a.endnotes.len(), 2);
+ assert_eq!(a.editorial.len(), 2);
+ assert_ne!(a.endnotes[0].id, a.endnotes[1].id);
+ assert_ne!(a.editorial[0].id, a.editorial[1].id);
+ assert_eq!(a.endnotes[1].text.text, "An endnote.");
+ assert_eq!((a.editorial[1].author.as_str(), a.editorial[1].text.as_str()), ("Ed", "Check this."));
+ let copy = a.endnotes[1].id;
+ a.endnote_mut(copy).unwrap().text = Story::with_text(StoryId(0), "Changed.", ParaFormat::default());
+ Arc::make_mut(&mut a.editorial[1]).text = "Changed.".into();
+ assert_eq!(a.endnotes[0].text.text, "An endnote.");
+ assert_eq!(a.editorial[0].text, "Check this.");
+ assert_eq!(slice.endnotes[0].text.text, "An endnote.");
+ // Into another story, between existing notes.
+ let mut b = Story::with_text(StoryId(2), "x y", ParaFormat::default());
+ b.insert_endnote(0, "B first.", ParaFormat::default());
+ b.insert_endnote(b.len(), "B last.", ParaFormat::default());
+ let mid = b.text.find(' ').unwrap();
+ b.insert_story(mid, &slice);
+ b.check().unwrap();
+ let texts: Vec<_> = b.endnotes.iter().map(|n| n.text.text.as_str()).collect();
+ assert_eq!(texts, ["B first.", "An endnote.", "B last."]);
+ assert_eq!(b.editorial[0].text, "Check this.");
+ }
+
+ #[test]
+ fn extract_survives_more_markers_than_payloads() {
+ let mut a = Story::with_text(StoryId(1), "a b c d e f", ParaFormat::default());
+ for (pos, m) in [(1, FOOTNOTE_REF), (4, ENDNOTE_REF), (7, NOTE_MARK), (10, XREF_MARK)] {
+ a.insert(pos, &format!("{m}{m}"));
+ }
+ // A hostile file: two markers of each kind, at most one payload.
+ a.notes.truncate(1);
+ a.endnotes.truncate(1);
+ a.editorial.clear();
+ a.xrefs.clear();
+ for r in [0..a.len(), 2..a.len(), 0..3] {
+ let slice = a.extract(r);
+ slice.check().unwrap();
+ let mut b = Story::with_text(StoryId(2), "z", ParaFormat::default());
+ b.insert_story(1, &slice);
+ b.check().unwrap();
+ }
+ }
}