Skip to content

Commit

Permalink
fix: toc styles
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb committed Nov 21, 2024
1 parent ed82f43 commit bf43b56
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 44 deletions.
2 changes: 1 addition & 1 deletion docx-core/src/documents/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ mod tests {
let b = Document::new().add_table_of_contents(toc).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" mc:Ignorable="w14 wp14"><w:body><w:sdt><w:sdtPr><w:rPr /></w:sdtPr><w:sdtContent><w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType="begin" w:dirty="true" /><w:instrText>TOC \o &quot;1-3&quot;</w:instrText><w:fldChar w:fldCharType="separate" w:dirty="false" /></w:r><w:r><w:rPr /><w:fldChar w:fldCharType="end" w:dirty="false" /></w:r></w:p></w:sdtContent></w:sdt><w:sectPr><w:pgSz w:w="11906" w:h="16838" /><w:pgMar w:top="1985" w:right="1701" w:bottom="1701" w:left="1701" w:header="851" w:footer="992" w:gutter="0" /><w:cols w:space="425" w:num="1" /></w:sectPr></w:body></w:document>"#
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" mc:Ignorable="w14 wp14"><w:body><w:sdt><w:sdtPr><w:rPr /></w:sdtPr><w:sdtContent><w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType="begin" w:dirty="true" /><w:instrText>TOC \o &quot;1-3&quot;</w:instrText><w:fldChar w:fldCharType="separate" w:dirty="false" /></w:r></w:p><w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType="end" w:dirty="false" /></w:r></w:p></w:sdtContent></w:sdt><w:sectPr><w:pgSz w:w="11906" w:h="16838" /><w:pgMar w:top="1985" w:right="1701" w:bottom="1701" w:left="1701" w:header="851" w:footer="992" w:gutter="0" /><w:cols w:space="425" w:num="1" /></w:sectPr></w:body></w:document>"#
);
}

Expand Down
57 changes: 31 additions & 26 deletions docx-core/src/documents/elements/table_of_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,42 +189,47 @@ impl BuildXML for TableOfContents {
}
}

let mut p1 = if let Some(ref del) = self.delete {
let p1 = if let Some(ref del) = self.delete {
Paragraph::new().add_delete(
Delete::new()
.author(&del.author)
.date(&del.date)
.add_run(
Run::new()
.add_field_char(FieldCharType::Begin, true)
.add_delete_instr_text(DeleteInstrText::TOC(self.instr.clone()))
.add_field_char(FieldCharType::Separate, false),
)
.add_run(Run::new().add_field_char(FieldCharType::End, false)),
)
} else {
Paragraph::new()
.add_run(
Delete::new().author(&del.author).date(&del.date).add_run(
Run::new()
.add_field_char(FieldCharType::Begin, true)
.add_instr_text(InstrText::TOC(self.instr.clone()))
.add_delete_instr_text(DeleteInstrText::TOC(self.instr.clone()))
.add_field_char(FieldCharType::Separate, false),
)
.add_run(Run::new().add_field_char(FieldCharType::End, false))
),
)
} else {
Paragraph::new().add_run(
Run::new()
.add_field_char(FieldCharType::Begin, true)
.add_instr_text(InstrText::TOC(self.instr.clone()))
.add_field_char(FieldCharType::Separate, false),
)
};

if let Some(ref paragraph_property) = self.paragraph_property {
p1.property = paragraph_property.clone();
}

b = b.add_child(&p1)?;

if !self.after_contents.is_empty() {
let p2 = Paragraph::new().add_run(Run::new().add_field_char(FieldCharType::End, false));

if self.after_contents.is_empty() {
b = b.add_child(&p2)?;
} else {
for (i, c) in self.after_contents.iter().enumerate() {
match c {
TocContent::Paragraph(p) => {
// Merge paragraph
b = b.add_child(&p)?;
if i == 0 {
let mut new_p = p.clone();
new_p.children.insert(
0,
ParagraphChild::Run(Box::new(
Run::new().add_field_char(FieldCharType::End, false),
)),
);
b = b.add_child(&new_p)?
} else {
b = b.add_child(&p)?;
}
}
TocContent::Table(t) => {
// insert empty line for table
Expand Down Expand Up @@ -319,7 +324,7 @@ mod tests {
let b = TableOfContents::new().heading_styles_range(1, 3).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:sdt><w:sdtPr><w:rPr /></w:sdtPr><w:sdtContent><w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType="begin" w:dirty="true" /><w:instrText>TOC \o &quot;1-3&quot;</w:instrText><w:fldChar w:fldCharType="separate" w:dirty="false" /></w:r><w:r><w:rPr /><w:fldChar w:fldCharType="end" w:dirty="false" /></w:r></w:p></w:sdtContent></w:sdt>"#
r#"<w:sdt><w:sdtPr><w:rPr /></w:sdtPr><w:sdtContent><w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType="begin" w:dirty="true" /><w:instrText>TOC \o &quot;1-3&quot;</w:instrText><w:fldChar w:fldCharType="separate" w:dirty="false" /></w:r></w:p><w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType="end" w:dirty="false" /></w:r></w:p></w:sdtContent></w:sdt>"#
);
}

Expand All @@ -331,7 +336,7 @@ mod tests {
.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType="begin" w:dirty="true" /><w:instrText>TOC \o &quot;1-3&quot;</w:instrText><w:fldChar w:fldCharType="separate" w:dirty="false" /></w:r><w:r><w:rPr /><w:fldChar w:fldCharType="end" w:dirty="false" /></w:r></w:p>"#
r#"<w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType="begin" w:dirty="true" /><w:instrText>TOC \o &quot;1-3&quot;</w:instrText><w:fldChar w:fldCharType="separate" w:dirty="false" /></w:r></w:p><w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType="end" w:dirty="false" /></w:r></w:p>"#
);
}

Expand Down
23 changes: 16 additions & 7 deletions docx-core/src/documents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,13 +541,7 @@ impl Docx {
})
.collect();

if !tocs.is_empty() {
for i in 1..=9 {
self.styles = self
.styles
.add_style(crate::documents::preset_styles::toc(i));
}
}
let has_toc = !tocs.is_empty();

for (i, toc) in tocs {
if toc.items.is_empty() && toc.auto {
Expand Down Expand Up @@ -611,6 +605,21 @@ impl Docx {
self.document_rels.has_footnotes = true;
}

if has_toc {
for i in 1..=9 {
if !self
.styles
.styles
.iter()
.any(|s| s.name == Name::new(format!("toc {}", i)))
{
self.styles = self
.styles
.add_style(crate::documents::preset_styles::toc(i));
}
}
}

XMLDocx {
content_type: self.content_type.build(),
rels: self.rels.build(),
Expand Down
4 changes: 2 additions & 2 deletions docx-core/src/documents/styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::xml_builder::*;
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Styles {
doc_defaults: DocDefaults,
styles: Vec<Style>,
pub doc_defaults: DocDefaults,
pub styles: Vec<Style>,
}

impl Styles {
Expand Down
2 changes: 1 addition & 1 deletion docx-wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "docx-wasm",
"version": "0.4.18-rc25",
"version": "0.4.18-rc26",
"main": "dist/node/index.js",
"browser": "dist/web/index.js",
"author": "bokuweb <bokuweb12@gmail.com>",
Expand Down
Loading

0 comments on commit bf43b56

Please sign in to comment.