Skip to content

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

Text: copied text keeps its endnotes and editorial notes

aionggd6309bae3268
diff --git a/crates/doc/src/edit.rs b/crates/doc/src/edit.rs
index 3bb827135..0957ba5ab 100644
--- a/crates/doc/src/edit.rs
+++ b/crates/doc/src/edit.rs
@@ -394,10 +394,13 @@ impl Document {
st.tables.clear();
st.notes.clear();
st.fix_notes();
+ st.endnotes.clear();
+ st.fix_endnotes();
st.anchors.clear();
st.xrefs.clear();
st.index_refs.clear();
st.objects.clear();
+ st.editorial.clear();
st.fix_marks();
st.rev += 1;
Ok(())
diff --git a/crates/doc/src/endnotes.rs b/crates/doc/src/endnotes.rs
index b700254a4..c5a3ab887 100644
--- a/crates/doc/src/endnotes.rs
+++ b/crates/doc/src/endnotes.rs
@@ -108,6 +108,17 @@ impl Story {
}
}
+ /// Make the endnote list match the reference characters (after wholesale text changes).
+ pub fn fix_endnotes(&mut self) {
+ let n = self.text.matches(ENDNOTE_REF).count();
+ if n < self.endnotes.len() {
+ self.endnotes.truncate(n);
+ } else if n > self.endnotes.len() {
+ let k = self.endnotes.len();
+ self.endnotes_inserted(k, n - k);
+ }
+ }
+
pub(crate) fn check_endnotes(&self) -> Result<(), String> {
let n = self.text.matches(ENDNOTE_REF).count();
if n != self.endnotes.len() {
diff --git a/crates/doc/src/slice.rs b/crates/doc/src/slice.rs
index f85ec4adc..a12101783 100644
--- a/crates/doc/src/slice.rs
+++ b/crates/doc/src/slice.rs
@@ -1,8 +1,10 @@
//! Formatted text slices: copy a range of a story (with paragraph and character formats,
-//! footnotes, cross-references, index markers and tables) and insert it into another story.
+//! footnotes, endnotes, editorial notes, cross-references, index markers and tables) and insert
+//! it into another story.
use std::sync::Arc;
+use crate::endnotes::{ENDNOTE_REF, NOTE_MARK};
use crate::ids::StoryId;
use crate::index::INDEX_MARK;
use crate::notes::FOOTNOTE_REF;
@@ -30,13 +32,22 @@ impl Story {
if out.chars.is_empty() {
out.chars.push(CharRun { len: 0, format: self.char_format_at(a).clone() });
}
- let count = |m: char, upto: usize| self.text[..upto].matches(m).count();
- let span = |m: char| count(m, a)..count(m, b);
- out.notes = self.notes[span(FOOTNOTE_REF)].to_vec();
- out.anchors = self.anchors[span(ANCHOR_MARK)].to_vec();
- out.xrefs = self.xrefs[span(XREF_MARK)].to_vec();
- out.index_refs = self.index_refs[span(INDEX_MARK)].to_vec();
- out.objects = self.objects[span(crate::anchored::OBJECT_MARK)].to_vec();
+ // Payloads in text order; a file may hold fewer payloads than markers, so the lists are
+ // read with `get` and the slice's own lists are made to match its markers below.
+ let span = |m: char| {
+ let lo = self.text[..a].matches(m).count();
+ lo..lo + self.text[a..b].matches(m).count()
+ };
+ out.notes = marked(&self.notes, span(FOOTNOTE_REF));
+ out.endnotes = marked(&self.endnotes, span(ENDNOTE_REF));
+ out.editorial = marked(&self.editorial, span(NOTE_MARK));
+ out.anchors = marked(&self.anchors, span(ANCHOR_MARK));
+ out.xrefs = marked(&self.xrefs, span(XREF_MARK));
+ out.index_refs = marked(&self.index_refs, span(INDEX_MARK));
+ out.objects = marked(&self.objects, span(crate::anchored::OBJECT_MARK));
+ out.fix_notes();
+ out.fix_endnotes();
+ out.fix_marks();
for p in &out.paras {
if let Some(id) = p.table
&& let Some(t) = self.tables.get(&id)
@@ -74,10 +85,10 @@ impl Story {
*p = crate::story::ParaFormat { table: None, ..pf.clone() };
}
}
- // Payloads of footnotes, cross-references and index markers, in order.
+ // Payloads of notes, cross-references and index markers, in order.
let first = |m: char| self.text[..pos].matches(m).count();
let (nf, nx, ni, na) = (first(FOOTNOTE_REF), first(XREF_MARK), first(INDEX_MARK), first(ANCHOR_MARK));
- let no = first(crate::anchored::OBJECT_MARK);
+ let (no, ne, nn) = (first(crate::anchored::OBJECT_MARK), first(ENDNOTE_REF), first(NOTE_MARK));
for (k, o) in src.objects.iter().enumerate() {
if let Some(slot) = self.objects.get_mut(no + k) {
*slot = o.clone();
@@ -89,6 +100,19 @@ impl Story {
*slot = Arc::new(crate::notes::Footnote { id, text: n.text.clone() });
}
}
+ // Notes keep the ids made for their new markers, so a pasted copy never shares an id.
+ for (k, n) in src.endnotes.iter().enumerate() {
+ if let Some(slot) = self.endnotes.get_mut(ne + k) {
+ let id = slot.id;
+ *slot = Arc::new(crate::notes::Footnote { id, text: n.text.clone() });
+ }
+ }
+ for (k, n) in src.editorial.iter().enumerate() {
+ if let Some(slot) = self.editorial.get_mut(nn + k) {
+ let id = slot.id;
+ *slot = Arc::new(crate::endnotes::EditorialNote { id, ..(**n).clone() });
+ }
+ }
for (k, x) in src.xrefs.iter().enumerate() {
if let Some(slot) = self.xrefs.get_mut(nx + k) {
*slot = x.clone();
@@ -128,6 +152,12 @@ impl Story {
}
}
+/// 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::*;
@@ -159,4 +189,65 @@ mod tests {
assert_eq!(part.notes.len(), 1);
assert!(a.extract(2..8).notes.is_empty());
}
+
+ #[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();
+ }
+ }
}
diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs
index e7190acb4..9771ce43a 100644
--- a/crates/engine/src/cmd/edit.rs
+++ b/crates/engine/src/cmd/edit.rs
@@ -74,7 +74,7 @@ fn paste_text(s: &mut Session, text: Option<String>, formatted: bool) -> Result<
}
}
let use_clip = formatted && clip.as_ref().is_some_and(|(_, p)| text.as_ref().is_none_or(|x| x.replace("\r\n", "\n") == *p));
- // Footnotes, cross-references and tables only paste into story text, not cells or footnotes.
+ // Notes, cross-references and tables only paste into story text, not cells or footnotes.
if use_clip && let Some((slice, _)) = clip {
let slice = if t.cell.is_some() { strip_objects(&slice) } else { (*slice).clone() };
return s.edit(|d, sel| {
@@ -93,7 +93,13 @@ fn paste_text(s: &mut Session, text: Option<String>, formatted: bool) -> Result<
fn strip_objects(st: &designcraft_doc::Story) -> designcraft_doc::Story {
let mut out = st.clone();
- let marks = [designcraft_doc::FOOTNOTE_REF, designcraft_doc::XREF_MARK, designcraft_doc::TABLE_ANCHOR];
+ let marks = [
+ designcraft_doc::FOOTNOTE_REF,
+ designcraft_doc::ENDNOTE_REF,
+ designcraft_doc::NOTE_MARK,
+ designcraft_doc::XREF_MARK,
+ designcraft_doc::TABLE_ANCHOR,
+ ];
while let Some(i) = out.text.find(marks) {
let n = out.text[i..].chars().next().map_or(1, char::len_utf8);
out.delete(i..i + n);