Skip to content

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

TOC-2: preserve manual bookmark ordering on refresh

moppeeerec179d5e6966
diff --git a/ROADMAP.md b/ROADMAP.md
index 71aa1b62b..255c941d7 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -346,7 +346,7 @@ list) · [roadmap.md](docs/roadmap.md) (milestones, plan) · [parity-checklist.m
## Progress log
-- **2026-10-18** · Table of Contents gains Create PDF Bookmarks: included headings become nested page destinations, regenerated on update without replacing manual bookmarks. The setting survives native and IDML files. No readiness percentage change.
+- **2026-10-18** · Table of Contents gains Create PDF Bookmarks: included headings become nested page destinations, regenerated in their existing outline position on update without replacing or moving surrounding manual bookmarks. The setting survives native and IDML files. No readiness percentage change.
Newest first.
diff --git a/crates/engine/src/cmd/interactive.rs b/crates/engine/src/cmd/interactive.rs
index 2d918a01f..c49425d7a 100644
--- a/crates/engine/src/cmd/interactive.rs
+++ b/crates/engine/src/cmd/interactive.rs
@@ -6,6 +6,7 @@ use serde_json::{Value, json};
use super::{CommandSpec, bad, cmd, has_doc, str_param};
use crate::{Result, Session};
+/// Register commands for editing hyperlinks and manual bookmarks.
pub fn specs() -> Vec<CommandSpec> {
vec![
cmd!(
diff --git a/crates/engine/src/cmd/style.rs b/crates/engine/src/cmd/style.rs
index 642992e76..5e22142cb 100644
--- a/crates/engine/src/cmd/style.rs
+++ b/crates/engine/src/cmd/style.rs
@@ -2967,6 +2967,7 @@ mod delete_tests {
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
}
+ /// Deleting a paragraph style also redirects TOC style references to its replacement.
#[test]
fn deleting_a_paragraph_style_points_every_use_at_the_replacement() {
let mut s = Session::new();
diff --git a/crates/engine/src/cmd/toc.rs b/crates/engine/src/cmd/toc.rs
index 0515df478..d3697c677 100644
--- a/crates/engine/src/cmd/toc.rs
+++ b/crates/engine/src/cmd/toc.rs
@@ -12,6 +12,7 @@ use serde_json::{Value, json};
use super::{CommandSpec, bad, cmd, has_doc, str_param};
use crate::{Result, Session};
+/// Register TOC generation and refresh commands.
pub fn specs() -> Vec<CommandSpec> {
vec![
cmd!(
@@ -302,9 +303,10 @@ fn fill(d: &mut Document, toc: &Toc, width: Option<f64>) -> Result<usize> {
st.format_paras(r.start..r.start, |f| *f = ParaFormat { style: name.clone(), ..Default::default() });
}
}
+ let insertion = d.bookmarks.iter().position(|b| b.generated_by_toc).unwrap_or(d.bookmarks.len());
d.bookmarks.retain(|b| !b.generated_by_toc);
if toc.create_bookmarks {
- d.bookmarks.extend(bookmarks(&found));
+ d.bookmarks.splice(insertion..insertion, bookmarks(&found));
}
Ok(found.len())
}
@@ -312,6 +314,7 @@ fn fill(d: &mut Document, toc: &Toc, width: Option<f64>) -> Result<usize> {
/// Levels are validated to 1–9. Recursion follows strictly increasing levels, including when
/// a document skips a level; each destination is the physical page, not its section label.
fn bookmarks(found: &[Found]) -> Vec<designcraft_doc::Bookmark> {
+ /// Collect descendants until the next sibling or ancestor entry.
fn children(iter: &mut std::iter::Peekable<std::slice::Iter<'_, Found>>, parent: u8) -> Vec<designcraft_doc::Bookmark> {
let mut out = Vec::new();
while iter.peek().is_some_and(|f| f.level > parent) {
@@ -323,6 +326,7 @@ fn bookmarks(found: &[Found]) -> Vec<designcraft_doc::Bookmark> {
children(&mut found.iter().peekable(), 0)
}
+/// Validate the TOC settings and generate its frame, text, and outline in one edit.
fn generate(s: &mut Session, p: &Value) -> Result<Value> {
let entries = entries_param(p)?;
if entries.is_empty() {
@@ -645,6 +649,7 @@ mod tests {
assert!(toc_runs(&s).iter().any(|(t, st)| t == "1" && st == "Folio"), "{:?}", toc_runs(&s));
}
+ /// Refresh preserves manual outline positions, nested destinations, storage, and undo.
#[test]
fn toc_bookmarks_refresh_nested_page_destinations_and_preserve_manual_bookmarks() {
let (mut s, page) = doc_with(&[("First", "Head"), ("Child", "Sub"), ("Last", "Head")]);
@@ -660,12 +665,13 @@ mod tests {
assert_eq!(d.bookmarks[1].children[0].name, "Child");
assert!(d.bookmarks[1].children[0].generated_by_toc);
let sid = d.stories.iter().find(|(_, st)| st.text.starts_with("First")).map(|(sid, _)| *sid).unwrap();
+ s.execute("bookmark.add", &json!({"name": "Manual after", "page": page})).unwrap();
s.execute("text.select", &json!({"story": sid.0, "anchor": 0, "focus": 5})).unwrap();
s.execute("text.insert", &json!({"text": "Changed"})).unwrap();
assert_eq!(s.doc().unwrap().doc.bookmarks[1].name, "First", "refresh is explicit");
s.execute("toc.update", &json!({})).unwrap();
let refreshed = s.doc().unwrap().doc.bookmarks.clone();
- assert_eq!(refreshed[1].name, "Changed");
+ assert_eq!(refreshed.iter().map(|b| b.name.as_str()).collect::<Vec<_>>(), ["Manual", "Changed", "Last", "Manual after"]);
s.execute("toc.update", &json!({})).unwrap();
assert_eq!(s.doc().unwrap().doc.bookmarks, refreshed, "updates do not duplicate bookmarks");
// Native storage retains both the option and ownership; old bookmarks default to manual.
@@ -678,8 +684,7 @@ mod tests {
let mut disabled = p;
disabled["createBookmarks"] = json!(false);
s.execute("toc.generate", &disabled).unwrap();
- assert_eq!(s.doc().unwrap().doc.bookmarks.len(), 1);
- assert_eq!(s.doc().unwrap().doc.bookmarks[0].name, "Manual");
+ assert_eq!(s.doc().unwrap().doc.bookmarks.iter().map(|b| b.name.as_str()).collect::<Vec<_>>(), ["Manual", "Manual after"]);
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(s.doc().unwrap().doc.bookmarks, refreshed);
}
diff --git a/crates/engine/src/tests_idml.rs b/crates/engine/src/tests_idml.rs
index 7afb2acd5..7c8fe8609 100644
--- a/crates/engine/src/tests_idml.rs
+++ b/crates/engine/src/tests_idml.rs
@@ -500,6 +500,7 @@ fn type_para_sets_a_drop_cap_that_survives_idml() {
assert_eq!(p.nested_styles, Some(vec![designcraft_doc::NestedStyle::drop_cap("Initial")]));
}
+/// Imported TOC definitions retain their settings and remain refreshable.
#[test]
fn table_of_contents_definition_round_trips_through_idml_and_updates() {
let mut s = Session::new();
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index 2a5cd7b24..c4f8823c7 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -7959,6 +7959,7 @@ mod tests {
assert_eq!(opts(&app), before);
}
+ /// The bookmarks checkbox persists through generation and reopening the dialog.
#[test]
fn toc_bookmarks_checkbox_sets_and_reopens_the_persisted_option() {
let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
diff --git a/docs/agents.md b/docs/agents.md
index 2358235ec..532811664 100644
--- a/docs/agents.md
+++ b/docs/agents.md
@@ -58,7 +58,8 @@ is a PNG of the page; another extension is an error, and nothing runs.
`toc.generate` accepts `createBookmarks: true` to generate page-level PDF bookmarks from the
included paragraph styles and their levels. `toc.update` refreshes their titles, hierarchy and
-destinations. Manual bookmarks stay intact; disabling the option removes only the generated
+destinations. Refresh keeps the generated outline at its previous position, preserving manual
+bookmarks before and after it; disabling the option removes only the generated
bookmarks. Omitting the parameter keeps the existing TOC setting (false for a new command-created
TOC). The Table of Contents dialog exposes Create PDF Bookmarks and defaults it on for a new TOC.