From 11e09de5cdb34ce23f158a07b16044706a5c15c3 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Fri, 22 Nov 2024 10:58:49 +0900 Subject: [PATCH] Add toc ppr (#785) * feat: add ppr in toc * feat: add frame property * fix: add rpr * fix: add toc paragraphProperty * support toc paragraph property * fix: some properties * 0.4.18-rc20 * fix: toc style * chore: rc21 * fix * fix: toc structure * fix: toc styles --- .../documents/elements/paragraph_property.rs | 5 + .../elements/paragraph_property_default.rs | 2 +- .../src/documents/elements/run_property.rs | 5 + .../documents/elements/table_of_contents.rs | 9 + docx-core/src/documents/mod.rs | 23 +- docx-core/src/documents/paragraph_id.rs | 3 +- docx-core/src/documents/styles.rs | 4 +- docx-wasm/js/builder.ts | 16 +- docx-wasm/js/doc-defaults.ts | 19 +- docx-wasm/js/index.ts | 41 +- docx-wasm/js/json/run.ts | 2 +- docx-wasm/js/level.ts | 26 +- docx-wasm/js/paragraph-property.ts | 331 +++++++++++++-- docx-wasm/js/paragraph.ts | 39 +- docx-wasm/js/run-property.ts | 383 ++++++++++++++++++ docx-wasm/js/run.ts | 296 ++------------ docx-wasm/js/style.ts | 94 ++--- docx-wasm/js/styles.ts | 2 +- docx-wasm/js/table-cell.ts | 2 +- docx-wasm/js/table-of-contents.ts | 16 + docx-wasm/package.json | 2 +- docx-wasm/src/frame_property.rs | 79 ++++ docx-wasm/src/lib.rs | 6 + docx-wasm/src/paragraph.rs | 65 +-- docx-wasm/src/paragraph_property.rs | 183 +++++++++ docx-wasm/src/run_property.rs | 114 ++++++ docx-wasm/src/style.rs | 18 +- docx-wasm/src/table_of_contents.rs | 5 + .../test/__snapshots__/index.test.js.snap | 8 + docx-wasm/test/index.test.js | 74 ++++ 30 files changed, 1380 insertions(+), 492 deletions(-) create mode 100644 docx-wasm/js/run-property.ts create mode 100644 docx-wasm/src/frame_property.rs create mode 100644 docx-wasm/src/paragraph_property.rs create mode 100644 docx-wasm/src/run_property.rs diff --git a/docx-core/src/documents/elements/paragraph_property.rs b/docx-core/src/documents/elements/paragraph_property.rs index ed1792e4a..c07d28cc3 100644 --- a/docx-core/src/documents/elements/paragraph_property.rs +++ b/docx-core/src/documents/elements/paragraph_property.rs @@ -157,6 +157,11 @@ impl ParagraphProperty { self } + pub fn run_property(mut self, s: RunProperty) -> Self { + self.run_property = s; + self + } + pub fn text_alignment(mut self, s: TextAlignmentType) -> Self { self.text_alignment = Some(TextAlignment::new(s)); self diff --git a/docx-core/src/documents/elements/paragraph_property_default.rs b/docx-core/src/documents/elements/paragraph_property_default.rs index 5f7dce48d..294b464c9 100644 --- a/docx-core/src/documents/elements/paragraph_property_default.rs +++ b/docx-core/src/documents/elements/paragraph_property_default.rs @@ -21,7 +21,7 @@ impl ParagraphPropertyDefault { self } - pub(crate) fn paragraph_property(mut self, p: ParagraphProperty) -> Self { + pub fn paragraph_property(mut self, p: ParagraphProperty) -> Self { self.paragraph_property = p; self } diff --git a/docx-core/src/documents/elements/run_property.rs b/docx-core/src/documents/elements/run_property.rs index a8ccc95b7..e6200d173 100644 --- a/docx-core/src/documents/elements/run_property.rs +++ b/docx-core/src/documents/elements/run_property.rs @@ -145,6 +145,11 @@ impl RunProperty { self } + pub fn character_spacing(mut self, v: i32) -> RunProperty { + self.character_spacing = Some(CharacterSpacing::new(v)); + self + } + pub fn text_border(mut self, b: TextBorder) -> Self { self.text_border = Some(b); self diff --git a/docx-core/src/documents/elements/table_of_contents.rs b/docx-core/src/documents/elements/table_of_contents.rs index 2e5e341ec..c403f245f 100644 --- a/docx-core/src/documents/elements/table_of_contents.rs +++ b/docx-core/src/documents/elements/table_of_contents.rs @@ -61,6 +61,8 @@ pub struct TableOfContents { pub after_contents: Vec, #[serde(skip_serializing_if = "Option::is_none")] pub delete: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub paragraph_property: Option, } impl TableOfContents { @@ -149,6 +151,11 @@ impl TableOfContents { self.without_sdt = true; self } + + pub fn paragraph_property(mut self, p: ParagraphProperty) -> Self { + self.paragraph_property = Some(p); + self + } } impl BuildXML for TableOfContents { @@ -199,9 +206,11 @@ impl BuildXML for TableOfContents { .add_field_char(FieldCharType::Separate, false), ) }; + b = b.add_child(&p1)?; 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 { diff --git a/docx-core/src/documents/mod.rs b/docx-core/src/documents/mod.rs index 7be938e69..5644f3828 100644 --- a/docx-core/src/documents/mod.rs +++ b/docx-core/src/documents/mod.rs @@ -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 { @@ -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(), diff --git a/docx-core/src/documents/paragraph_id.rs b/docx-core/src/documents/paragraph_id.rs index 5ec3152eb..1571c8106 100644 --- a/docx-core/src/documents/paragraph_id.rs +++ b/docx-core/src/documents/paragraph_id.rs @@ -7,8 +7,7 @@ static PARA_ID: AtomicUsize = AtomicUsize::new(1); pub fn generate_para_id() -> String { use std::sync::atomic::Ordering; - let id = PARA_ID.load(Ordering::Relaxed); - PARA_ID.store(id.wrapping_add(1), Ordering::Relaxed); + let id = PARA_ID.fetch_add(1, Ordering::Relaxed); format!("{:08x}", id) } diff --git a/docx-core/src/documents/styles.rs b/docx-core/src/documents/styles.rs index ee496b0f2..1f6d730b4 100644 --- a/docx-core/src/documents/styles.rs +++ b/docx-core/src/documents/styles.rs @@ -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