Skip to content

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

Text: notes insert into a story whose markers lack payloads

aiongg6cf1933a13a8
diff --git a/crates/doc/src/endnotes.rs b/crates/doc/src/endnotes.rs
index c5a3ab887..d61721cb0 100644
--- a/crates/doc/src/endnotes.rs
+++ b/crates/doc/src/endnotes.rs
@@ -87,15 +87,18 @@ impl Story {
Some(Arc::make_mut(n))
}
- /// Insert an endnote reference at `pos` with the given text. Returns the endnote id.
- pub fn insert_endnote(&mut self, pos: usize, text: &str, para: ParaFormat) -> u64 {
+ /// Insert an endnote reference at `pos` with the given text. Returns the endnote id, or
+ /// `None` if the story has no endnote for the new reference.
+ pub fn insert_endnote(&mut self, pos: usize, text: &str, para: ParaFormat) -> Option<u64> {
let pos = crate::story::floor_char_boundary(&self.text, pos.min(self.len()));
let fmt = self.char_format_at(pos).clone();
self.insert_with(pos, &ENDNOTE_REF.to_string(), fmt);
+ // A file may hold fewer endnotes than references; give every reference one.
+ self.fix_endnotes();
let k = self.endnotes_before(pos);
- let note = Arc::make_mut(&mut self.endnotes[k]);
+ let note = Arc::make_mut(self.endnotes.get_mut(k)?);
note.text = Story::with_text(StoryId(0), text, para);
- note.id
+ Some(note.id)
}
/// Insert empty endnotes for `n` new reference characters before which `k` references sit.
diff --git a/crates/engine/src/cmd/editorial.rs b/crates/engine/src/cmd/editorial.rs
index 6cee420ed..3ebce6b0c 100644
--- a/crates/engine/src/cmd/editorial.rs
+++ b/crates/engine/src/cmd/editorial.rs
@@ -23,8 +23,11 @@ pub fn specs() -> Vec<CommandSpec> {
let story = d.story_mut(ts.story).ok_or_else(|| bad("note.new", "no such story"))?;
let fmt = story.char_format_at(at).clone();
story.insert_with(at, &NOTE_MARK.to_string(), fmt);
+ // A file may hold fewer notes than anchors; give every anchor one.
+ story.fix_marks();
let k = story.text[..at].matches(NOTE_MARK).count();
- let n = Arc::make_mut(&mut story.editorial[k]);
+ let n = story.editorial.get_mut(k).ok_or_else(|| bad("note.new", "the story's notes don't match their anchors"))?;
+ let n = Arc::make_mut(n);
(n.text, n.author) = (text, author);
let id = n.id;
if let Some(t) = &mut sel.text {
@@ -132,4 +135,25 @@ mod tests {
assert_eq!(s.doc().unwrap().doc.stories[&sid].text, "Draft (checked) copy.");
assert!(s.doc().unwrap().doc.stories[&sid].editorial.is_empty());
}
+
+ #[test]
+ fn notes_insert_into_a_story_whose_markers_lack_payloads() {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ let r = s.execute("frame.create", &json!({"rect": [72, 72, 400, 300], "content": "text", "text": "Draft copy."})).unwrap();
+ let sid = designcraft_doc::StoryId(r["story"].as_u64().unwrap());
+ // A damaged file: a note anchor and an endnote reference without payloads.
+ let st = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).story_mut(sid).unwrap();
+ st.insert(0, &format!("{}{}", designcraft_doc::NOTE_MARK, designcraft_doc::ENDNOTE_REF));
+ st.editorial.clear();
+ st.endnotes.clear();
+ let end = st.len();
+ s.execute("text.select", &json!({"story": r["story"], "anchor": end, "focus": end})).unwrap();
+ s.execute("note.new", &json!({"text": "check this"})).unwrap();
+ s.execute("endnote.insert", &json!({"text": "An endnote."})).unwrap();
+ let st = &s.doc().unwrap().doc.stories[&sid];
+ st.check().unwrap();
+ assert_eq!(st.editorial[1].text, "check this");
+ assert_eq!(st.endnotes[1].text.text, "An endnote.");
+ }
}
diff --git a/crates/engine/src/cmd/endnotes.rs b/crates/engine/src/cmd/endnotes.rs
index bdf9ca695..10cb44c79 100644
--- a/crates/engine/src/cmd/endnotes.rs
+++ b/crates/engine/src/cmd/endnotes.rs
@@ -121,7 +121,9 @@ fn insert(s: &mut Session, p: &Value) -> Result<Value> {
}
let style = d.endnote_options.para_style.clone();
let story = d.story_mut(ts.story).ok_or_else(|| bad(ID, "no such story"))?;
- let id = story.insert_endnote(at, &text, ParaFormat { style, ..Default::default() });
+ let id = story
+ .insert_endnote(at, &text, ParaFormat { style, ..Default::default() })
+ .ok_or_else(|| bad(ID, "the story's endnotes don't match its references"))?;
let caret = at + designcraft_doc::ENDNOTE_REF.len_utf8();
if let Some(t) = &mut sel.text {
(t.anchor, t.focus) = (caret, caret);