Skip to content

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

Merge #456: Wrap saved story revision counters consistently

aiongg9ed4aba3da65
diff --cc crates/doc/src/edit.rs
index 2a83e5e17,e532823a5..3a224d039
--- a/crates/doc/src/edit.rs
+++ b/crates/doc/src/edit.rs
@@@ -311,101 -272,12 +311,101 @@@ impl Document
let st = self.story_mut(fs).ok_or(DocError::NoStory(fs))?;
let at = st.frames.iter().position(|f| *f == from).map_or(st.frames.len(), |p| p + 1);
st.frames.insert(at, to);
- st.rev += 1;
++ st.rev = st.rev.wrapping_add(1);
let it = self.item_mut(to).ok_or(DocError::NoItem(to))?;
it.content = Content::Text(TextFrame { story: fs, options: TextFrameOptions::default() });
- }
- if let Some(st) = self.story_mut(fs) {
+ return Ok(());
+ };
+ let mut other = Arc::unwrap_or_clone(self.stories.remove(&ts).ok_or(DocError::NoStory(ts))?);
+ // Frames of the other story from `to` on follow along; frames before it are left as an empty new story.
+ let pos_to = other.frames.iter().position(|f| *f == to).unwrap_or(0);
+ let after = other.frames.split_off(pos_to);
+ let before = std::mem::take(&mut other.frames);
+ let (vertical, direction) = (other.vertical, other.direction);
+ {
+ let st = self.story_mut(fs).ok_or(DocError::NoStory(fs))?;
+ // The story keeps its own direction, even when it was empty and takes the other's text.
+ let own = (st.vertical, st.direction);
+ st.append_story(other);
+ (st.vertical, st.direction) = own;
+ let at = st.frames.iter().position(|f| *f == from).map_or(st.frames.len(), |p| p + 1);
+ for (k, f) in after.iter().enumerate() {
+ st.frames.insert(at + k, *f);
+ }
- st.rev += 1;
+ st.rev = st.rev.wrapping_add(1);
}
+ for f in &after {
+ if let Some(t) = self.item_mut(*f).and_then(Item::text_frame_mut) {
+ t.story = fs;
+ }
+ }
+ if !before.is_empty() {
+ let nid = StoryId(self.alloc());
+ let mut ns = Story::new(nid);
+ ns.frames = before.clone();
+ ns.vertical = vertical;
+ ns.direction = direction;
+ for f in &before {
+ if let Some(t) = self.item_mut(*f).and_then(Item::text_frame_mut) {
+ t.story = nid;
+ }
+ }
+ self.stories.insert(nid, Arc::new(ns));
+ }
+ Ok(())
+ }
+
+ /// [`Document::thread`], except that an empty `from` alone in its story, threaded in front of
+ /// the first frame of `to`'s story, joins that story: the story with the text keeps its id
+ /// (and whatever refers to it).
+ pub fn thread_keeping_story(&mut self, from: ItemId, to: ItemId) -> Result<()> {
+ let fs = self.frame_story(from)?;
+ let from_empty = fs.is_none_or(|fs| self.story(fs).is_some_and(|s| s.text.is_empty() && s.frames == [from]));
+ let ts = self.frame_story(to)?;
+ let to_first = ts.and_then(|ts| self.story(ts)).is_some_and(|s| s.frames.first() == Some(&to));
+ let (Some(ts), true, true) = (ts, from_empty, to_first && from != to) else { return self.thread(from, to) };
+ if let Some(fs) = fs {
+ self.stories.remove(&fs);
+ }
+ let st = self.story_mut(ts).ok_or(DocError::NoStory(ts))?;
+ st.frames.insert(0, from);
- st.rev += 1;
++ st.rev = st.rev.wrapping_add(1);
+ let it = self.item_mut(from).ok_or(DocError::NoItem(from))?;
+ match it.text_frame_mut() {
+ Some(t) => t.story = ts,
+ None => it.content = Content::Text(TextFrame { story: ts, options: TextFrameOptions::default() }),
+ }
+ Ok(())
+ }
+
+ /// Can a user thread `from` → `to`? Both are text frames or empty frames (not lines), on
+ /// document spreads or on the same parent spread, not in one story already, and no frame
+ /// threads into `to` yet. [`Document::thread`] itself is less strict (import and autoflow).
+ pub fn check_thread(&self, from: ItemId, to: ItemId) -> Result<()> {
+ if from == to {
+ return Err(DocError::Invalid("cannot thread a frame to itself".into()));
+ }
+ for id in [from, to] {
+ let it = self.item(id).ok_or(DocError::NoItem(id))?;
+ if !matches!(it.content, Content::Text(_) | Content::Unassigned) || it.shape == Shape::GraphicLine {
+ return Err(DocError::Invalid(format!("{id} is not a text frame or an empty frame")));
+ }
+ }
+ let (Some(fl), Some(tl)) = (self.find(from), self.find(to)) else { return Err(DocError::NoItem(to)) };
+ let same_place = match (fl.spread, tl.spread) {
+ (SpreadRef::Doc(_), SpreadRef::Doc(_)) => true,
+ (a, b) => a == b,
+ };
+ if !same_place {
+ return Err(DocError::Invalid("frames on a parent spread thread only to frames on the same parent".into()));
+ }
+ let story = |id: ItemId| self.item(id).and_then(|i| i.text_frame()).map(|t| t.story);
+ if story(from).is_some() && story(from) == story(to) {
+ return Err(DocError::Invalid("the frames are already in the same thread".into()));
+ }
+ if self.prev_frame(to).is_some() {
+ return Err(DocError::Invalid(format!("text already flows into {to} from another frame: break that thread first")));
+ }
Ok(())
}
@@@ -458,30 -309,6 +458,30 @@@
Ok(())
}
+ /// Take `frame` out of its thread: the frames before and after it join up and keep the text,
+ /// `frame` stays on the page with a new empty story.
+ pub fn remove_from_thread(&mut self, frame: ItemId) -> Result<()> {
+ let sid = self.item(frame).and_then(|i| i.text_frame()).map(|t| t.story).ok_or(DocError::NoItem(frame))?;
+ let (vertical, direction) = {
+ let st = self.story_mut(sid).ok_or(DocError::NoStory(sid))?;
+ if st.frames.len() < 2 {
+ return Err(DocError::Invalid(format!("frame {frame} is not threaded to another frame")));
+ }
+ st.frames.retain(|f| *f != frame);
- st.rev += 1;
++ st.rev = st.rev.wrapping_add(1);
+ (st.vertical, st.direction)
+ };
+ let nid = StoryId(self.alloc());
+ let mut ns = Story::new(nid);
+ ns.frames = vec![frame];
+ ns.vertical = vertical;
+ ns.direction = direction;
+ self.stories.insert(nid, Arc::new(ns));
+ let t = self.item_mut(frame).and_then(Item::text_frame_mut).ok_or(DocError::NoItem(frame))?;
+ t.story = nid;
+ Ok(())
+ }
+
/// Next / previous frame in a thread.
pub fn next_frame(&self, frame: ItemId) -> Option<ItemId> {
let st = self.story(self.item(frame)?.text_frame()?.story)?;
diff --cc crates/doc/src/slice.rs
index 6afbad109,7020f4cf1..e188276dd
--- a/crates/doc/src/slice.rs
+++ b/crates/doc/src/slice.rs
@@@ -123,64 -123,9 +123,64 @@@ 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);
+ }
}
#[cfg(test)]
diff --cc crates/engine/src/cmd/text.rs
index 45497a02d,ee09a59cf..2f8a3b345
--- a/crates/engine/src/cmd/text.rs
+++ b/crates/engine/src/cmd/text.rs
@@@ -1318,30 -854,6 +1318,30 @@@ pub(crate) fn format_chars(s: &mut Sess
})
}
+/// Character attributes `a` (and the overrides `cleared` removed) over each target's text.
+pub(crate) fn apply_char_attrs(d: &mut designcraft_doc::Document, targets: &[Target], a: &CharAttrs, cleared: &[String]) {
+ for t in targets {
+ let r = &t.range;
+ let Some(st) = d.text_story_mut(t.story, t.cell) else { continue };
+ if r.is_empty() {
+ // An empty story: its one (empty) run is the format typing takes.
+ if st.is_empty()
+ && let Some(run) = st.chars.first_mut()
+ {
+ run.format.over.merge(a);
- st.rev += 1;
++ st.rev = st.rev.wrapping_add(1);
+ }
+ continue;
+ }
+ st.format_chars(r.clone(), |f| {
+ f.over.merge(a);
+ for k in cleared {
+ let _ = f.over.set_json(k, &Value::Null);
+ }
+ });
+ }
+}
+
pub(crate) fn format_paras(s: &mut Session, attrs: &Value) -> Result<Value> {
let empty = serde_json::Map::new();
let obj = attrs.as_object().unwrap_or(&empty);