From dbc7db91d8afdda59af415f07b23347948bc71b3 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Mon, 4 Mar 2024 22:20:59 +0900 Subject: [PATCH] fix: add page num type writer --- docx-core/src/documents/document.rs | 5 +++++ docx-core/src/documents/mod.rs | 5 +++++ docx-wasm/js/index.ts | 6 ++++++ docx-wasm/js/section-property.ts | 7 +++++++ docx-wasm/src/doc.rs | 5 +++++ docx-wasm/src/lib.rs | 2 ++ docx-wasm/src/page_num_type.rs | 23 +++++++++++++++++++++++ 7 files changed, 53 insertions(+) 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 43302bb8a..95660f0b0 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/mod.rs b/docx-core/src/documents/mod.rs index 5b5769e36..cee8bb0ac 100644 --- a/docx-core/src/documents/mod.rs +++ b/docx-core/src/documents/mod.rs @@ -505,6 +505,11 @@ impl Docx { self } + pub fn page_num_type(mut self, p: PageNumType) -> Self { + self.document = self.document.page_num_type(p); + self + } + pub fn build(mut self) -> XMLDocx { self.reset(); diff --git a/docx-wasm/js/index.ts b/docx-wasm/js/index.ts index 69a81e4a9..f76472daf 100644 --- a/docx-wasm/js/index.ts +++ b/docx-wasm/js/index.ts @@ -517,6 +517,12 @@ export class Docx { } } + if (this.sectionProperty._pageTypeNum) { + const { start, chapStyle } = this.sectionProperty._pageTypeNum; + const p = wasm.createPageNumType(start, chapStyle); + docx = docx.page_num_type(p); + } + if (this.sectionProperty._docGrid) { const { gridType, charSpace, linePitch } = this.sectionProperty._docGrid; let type = wasm.DocGridType.Default; diff --git a/docx-wasm/js/section-property.ts b/docx-wasm/js/section-property.ts index a20a87e47..9f4f46e37 100644 --- a/docx-wasm/js/section-property.ts +++ b/docx-wasm/js/section-property.ts @@ -1,6 +1,7 @@ import { DocGridType } from "."; import { Footer } from "./footer"; import { Header } from "./header"; +import { PageNumType } from "./json/bindings/PageNumType"; export type DocGrid = { gridType: DocGridType; @@ -21,6 +22,7 @@ export class SectionProperty { _footer: Footer | null = null; _firstFooter: Footer | null = null; _evenFooter: Footer | null = null; + _pageTypeNum: PageNumType | null = null; pageSize(w: number, h: number) { this._pageSize.w = w; @@ -72,6 +74,11 @@ export class SectionProperty { this._evenFooter = footer; return this; } + + pageTypeNum(start?: number, chapStyle?: string) { + this._pageTypeNum = { start, chapStyle }; + return this; + } } export type PageOrientationType = "landscape" | "portrait"; diff --git a/docx-wasm/src/doc.rs b/docx-wasm/src/doc.rs index 673f49d3d..b1b88282a 100644 --- a/docx-wasm/src/doc.rs +++ b/docx-wasm/src/doc.rs @@ -178,6 +178,11 @@ impl Docx { self } + pub fn page_num_type(mut self, p: PageNumType) -> Self { + self.0 = self.0.page_num_type(p.take()); + self + } + pub fn doc_grid( mut self, grid_type: docx_rs::DocGridType, diff --git a/docx-wasm/src/lib.rs b/docx-wasm/src/lib.rs index 2c5c3c8d0..5c3f9c27e 100644 --- a/docx-wasm/src/lib.rs +++ b/docx-wasm/src/lib.rs @@ -12,6 +12,7 @@ mod level_override; mod line_spacing; mod numbering; mod page_margin; +mod page_num_type; mod paragraph; mod pic; mod reader; @@ -40,6 +41,7 @@ pub use level_override::*; pub use line_spacing::*; pub use numbering::*; pub use page_margin::*; +pub use page_num_type::*; pub use paragraph::*; pub use pic::*; pub use reader::*; diff --git a/docx-wasm/src/page_num_type.rs b/docx-wasm/src/page_num_type.rs new file mode 100644 index 000000000..8ab45784c --- /dev/null +++ b/docx-wasm/src/page_num_type.rs @@ -0,0 +1,23 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +#[derive(Debug)] +pub struct PageNumType(docx_rs::PageNumType); + +#[wasm_bindgen(js_name = createPageNumType)] +pub fn create_page_num_type(start: Option, chap_style: Option) -> PageNumType { + let mut p = docx_rs::PageNumType::new(); + if let Some(start) = start { + p = p.start(start); + } + if let Some(chap_style) = chap_style { + p = p.chap_style(chap_style); + } + PageNumType(p) +} + +impl PageNumType { + pub fn take(self) -> docx_rs::PageNumType { + self.0 + } +}