From 9fbd323ce74177107f60587c2cf720855c441e88 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Wed, 6 Mar 2024 19:13:11 +0900 Subject: [PATCH] feat: Support pageNumType (#684) * fix: support page num type writer * fix: read page num type * fix: add json type * fix: add page num type writer * 0.4.12-beta0 * fix * fix --- docx-core/src/documents/document.rs | 5 +++ docx-core/src/documents/elements/mod.rs | 2 + .../src/documents/elements/page_num_type.rs | 42 +++++++++++++++++++ .../documents/elements/section_property.rs | 12 +++++- docx-core/src/documents/mod.rs | 5 +++ docx-core/src/reader/mod.rs | 5 ++- docx-core/src/reader/page_num_type.rs | 27 ++++++++++++ docx-core/src/reader/section_property.rs | 5 +++ docx-core/src/reader/xml_element.rs | 2 + docx-core/src/xml_builder/elements.rs | 20 ++++++++- .../snapshots/lib__reader__read_bookmark.snap | 2 +- .../snapshots/lib__reader__read_comment.snap | 2 +- .../lib__reader__read_decoration.snap | 2 +- .../snapshots/lib__reader__read_from_doc.snap | 2 +- .../snapshots/lib__reader__read_hello.snap | 2 +- ..._reader__read_highlight_and_underline.snap | 2 +- .../snapshots/lib__reader__read_history.snap | 2 +- .../lib__reader__read_insert_table.snap | 2 +- .../lib__reader__read_numbering.snap | 2 +- .../lib__reader__read_tab_and_break.snap | 2 +- ...eader__read_table_merged_libre_office.snap | 2 +- .../snapshots/reader__read_bookmark.snap | 2 +- .../tests/snapshots/reader__read_comment.snap | 2 +- .../snapshots/reader__read_decoration.snap | 2 +- .../snapshots/reader__read_from_doc.snap | 2 +- .../tests/snapshots/reader__read_hello.snap | 2 +- .../reader__read_highlight_and_underline.snap | 2 +- .../tests/snapshots/reader__read_history.snap | 2 +- .../snapshots/reader__read_insert_table.snap | 2 +- .../snapshots/reader__read_numbering.snap | 2 +- .../snapshots/reader__read_tab_and_break.snap | 2 +- ...eader__read_table_merged_libre_office.snap | 2 +- docx-wasm/js/index.ts | 6 +++ docx-wasm/js/json/bindings/PageNumType.ts | 2 + docx-wasm/js/json/section-property.ts | 4 ++ docx-wasm/js/section-property.ts | 7 ++++ docx-wasm/package.json | 2 +- docx-wasm/src/doc.rs | 5 +++ docx-wasm/src/lib.rs | 2 + docx-wasm/src/page_num_type.rs | 23 ++++++++++ .../test/__snapshots__/index.test.js.snap | 11 +++++ 41 files changed, 205 insertions(+), 26 deletions(-) create mode 100644 docx-core/src/documents/elements/page_num_type.rs create mode 100644 docx-core/src/reader/page_num_type.rs create mode 100644 docx-wasm/js/json/bindings/PageNumType.ts create mode 100644 docx-wasm/src/page_num_type.rs diff --git a/docx-core/src/documents/document.rs b/docx-core/src/documents/document.rs index 43302bb..95660f0 100644 --- a/docx-core/src/documents/document.rs +++ b/docx-core/src/documents/document.rs @@ -228,6 +228,11 @@ impl Document { self.section_property.text_direction = direction; self } + + pub fn page_num_type(mut self, p: PageNumType) -> Self { + self.section_property = self.section_property.page_num_type(p); + self + } } impl BuildXML for DocumentChild { diff --git a/docx-core/src/documents/elements/mod.rs b/docx-core/src/documents/elements/mod.rs index e40db51..f5b079e 100644 --- a/docx-core/src/documents/elements/mod.rs +++ b/docx-core/src/documents/elements/mod.rs @@ -61,6 +61,7 @@ mod numbering_id; mod numbering_property; mod outline_lvl; mod page_margin; +mod page_num_type; mod page_size; mod paragraph; mod paragraph_borders; @@ -185,6 +186,7 @@ pub use numbering_id::*; pub use numbering_property::*; pub use outline_lvl::*; pub use page_margin::*; +pub use page_num_type::*; pub use page_size::*; pub use paragraph::*; pub use paragraph_borders::*; diff --git a/docx-core/src/documents/elements/page_num_type.rs b/docx-core/src/documents/elements/page_num_type.rs new file mode 100644 index 0000000..23d52a2 --- /dev/null +++ b/docx-core/src/documents/elements/page_num_type.rs @@ -0,0 +1,42 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; +use serde::Serialize; + +#[derive(Debug, Clone, PartialEq, Serialize, Default)] +#[cfg_attr(feature = "wasm", derive(ts_rs::TS))] +#[cfg_attr(feature = "wasm", ts(export))] +#[serde(rename_all = "camelCase")] +pub struct PageNumType { + #[serde(skip_serializing_if = "Option::is_none")] + pub start: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub chap_style: Option, +} + +impl PageNumType { + pub fn new() -> Self { + Default::default() + } + + pub fn start(self, s: u32) -> Self { + Self { + start: Some(s), + ..self + } + } + + pub fn chap_style(self, s: impl Into) -> Self { + Self { + chap_style: Some(s.into()), + ..self + } + } +} + +impl BuildXML for PageNumType { + fn build(&self) -> Vec { + XMLBuilder::new() + .page_num_type(self.start, self.chap_style.clone()) + .build() + } +} diff --git a/docx-core/src/documents/elements/section_property.rs b/docx-core/src/documents/elements/section_property.rs index 8d70b8e..f51b325 100644 --- a/docx-core/src/documents/elements/section_property.rs +++ b/docx-core/src/documents/elements/section_property.rs @@ -43,6 +43,8 @@ pub struct SectionProperty { pub even_footer: Option