From 425e2bce08e79ea03668297ea53f34fcfe93354f Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 13 Oct 2020 00:04:09 +0900 Subject: [PATCH] Add page margin test (#175) * fix: page margin * fix: test * 0.0.109 --- docx-core/examples/page_margin.rs | 7 --- docx-core/src/types/page_margin.rs | 3 - docx-wasm/js/index.ts | 17 +++--- docx-wasm/package.json | 2 +- docx-wasm/src/doc.rs | 4 +- docx-wasm/src/lib.rs | 5 +- docx-wasm/src/page_margin.rs | 59 +++++++++++++++++++ .../test/__snapshots__/index.test.js.snap | 27 +++++++++ docx-wasm/test/index.test.js | 15 +++++ 9 files changed, 117 insertions(+), 22 deletions(-) create mode 100644 docx-wasm/src/page_margin.rs diff --git a/docx-core/examples/page_margin.rs b/docx-core/examples/page_margin.rs index d440bd3..5adb465 100644 --- a/docx-core/examples/page_margin.rs +++ b/docx-core/examples/page_margin.rs @@ -5,13 +5,6 @@ pub fn main() -> Result<(), DocxError> { let file = std::fs::File::create(&path).unwrap(); Docx::new() .add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello"))) - .page_margin( - PageMargin::new() - .top(3200) - .footer(3200) - .left(3200) - .right(3200), - ) .build() .pack(file)?; Ok(()) diff --git a/docx-core/src/types/page_margin.rs b/docx-core/src/types/page_margin.rs index c8f1007..6b6c9a4 100644 --- a/docx-core/src/types/page_margin.rs +++ b/docx-core/src/types/page_margin.rs @@ -1,8 +1,5 @@ use serde::Serialize; -use wasm_bindgen::prelude::*; - -#[wasm_bindgen] #[derive(Debug, Clone, PartialEq, Serialize)] #[serde(rename_all = "camelCase")] pub struct PageMargin { diff --git a/docx-wasm/js/index.ts b/docx-wasm/js/index.ts index e9f3dd6..0274311 100644 --- a/docx-wasm/js/index.ts +++ b/docx-wasm/js/index.ts @@ -554,14 +554,15 @@ export class Docx { footer, gutter, } = this.sectionProperty._pageMargin; - const margin = new wasm.PageMargin(); - margin.top = top; - margin.left = left; - margin.right = right; - margin.bottom = bottom; - margin.header = header; - margin.footer = footer; - margin.gutter = gutter; + const margin = wasm + .createPageMargin() + .top(top) + .left(left) + .right(right) + .bottom(bottom) + .header(header) + .footer(footer) + .gutter(gutter); docx = docx.page_margin(margin); } diff --git a/docx-wasm/package.json b/docx-wasm/package.json index 28446b7..ea4931d 100644 --- a/docx-wasm/package.json +++ b/docx-wasm/package.json @@ -1,6 +1,6 @@ { "name": "docx-wasm", - "version": "0.0.108", + "version": "0.0.109", "main": "dist/node/index.js", "browser": "dist/web/index.js", "author": "bokuweb ", diff --git a/docx-wasm/src/doc.rs b/docx-wasm/src/doc.rs index 82b1181..5f12251 100644 --- a/docx-wasm/src/doc.rs +++ b/docx-wasm/src/doc.rs @@ -53,8 +53,8 @@ impl Docx { self } - pub fn page_margin(mut self, margin: docx_rs::PageMargin) -> Docx { - self.0 = self.0.page_margin(margin); + pub fn page_margin(mut self, margin: PageMargin) -> Docx { + self.0 = self.0.page_margin(margin.take()); self } diff --git a/docx-wasm/src/lib.rs b/docx-wasm/src/lib.rs index ea2ddff..8b86af7 100644 --- a/docx-wasm/src/lib.rs +++ b/docx-wasm/src/lib.rs @@ -7,6 +7,7 @@ mod insert; mod level; mod level_override; mod numbering; +mod page_margin; mod paragraph; mod reader; mod run; @@ -25,10 +26,12 @@ pub use insert::*; pub use level::*; pub use level_override::*; pub use numbering::*; +pub use page_margin::*; pub use paragraph::*; pub use reader::*; pub use run::*; pub use run_fonts::*; pub use table::*; pub use table_cell::*; -pub use table_cell_border::*;pub use table_row::*; +pub use table_cell_border::*; +pub use table_row::*; diff --git a/docx-wasm/src/page_margin.rs b/docx-wasm/src/page_margin.rs new file mode 100644 index 0000000..509ba77 --- /dev/null +++ b/docx-wasm/src/page_margin.rs @@ -0,0 +1,59 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +#[derive(Debug)] +pub struct PageMargin(docx_rs::PageMargin); + +#[wasm_bindgen(js_name = createPageMargin)] +pub fn create_page_margin() -> PageMargin { + PageMargin(docx_rs::PageMargin::new()) +} + +impl PageMargin { + pub fn take(self) -> docx_rs::PageMargin { + self.0 + } +} + +#[wasm_bindgen] +impl PageMargin { + pub fn top(self, v: u32) -> PageMargin { + PageMargin(docx_rs::PageMargin { top: v, ..self.0 }) + } + + pub fn left(self, v: u32) -> PageMargin { + PageMargin(docx_rs::PageMargin { left: v, ..self.0 }) + } + + pub fn bottom(self, v: u32) -> PageMargin { + PageMargin(docx_rs::PageMargin { + bottom: v, + ..self.0 + }) + } + + pub fn right(self, v: u32) -> PageMargin { + PageMargin(docx_rs::PageMargin { right: v, ..self.0 }) + } + + pub fn header(self, v: u32) -> PageMargin { + PageMargin(docx_rs::PageMargin { + header: v, + ..self.0 + }) + } + + pub fn footer(self, v: u32) -> PageMargin { + PageMargin(docx_rs::PageMargin { + footer: v, + ..self.0 + }) + } + + pub fn gutter(self, v: u32) -> PageMargin { + PageMargin(docx_rs::PageMargin { + gutter: v, + ..self.0 + }) + } +} diff --git a/docx-wasm/test/__snapshots__/index.test.js.snap b/docx-wasm/test/__snapshots__/index.test.js.snap index ebc6c83..f2c8f9d 100644 --- a/docx-wasm/test/__snapshots__/index.test.js.snap +++ b/docx-wasm/test/__snapshots__/index.test.js.snap @@ -3730,6 +3730,33 @@ exports[`writer should write lvlOverride with level 3`] = ` " `; +exports[`writer should write page margin 1`] = ` +" + + + + + + +" +`; + +exports[`writer should write page margin 2`] = ` +" + + Hello world!! + + +" +`; + +exports[`writer should write page margin 3`] = ` +" + + +" +`; + exports[`writer should write page size 1`] = ` " diff --git a/docx-wasm/test/index.test.js b/docx-wasm/test/index.test.js index 05984c5..cc306c0 100644 --- a/docx-wasm/test/index.test.js +++ b/docx-wasm/test/index.test.js @@ -63,4 +63,19 @@ describe("writer", () => { } } }); + + test("should write page margin", () => { + const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!")); + const buf = new w.Docx() + .addParagraph(p) + .pageMargin({ top: 1000, left: 2000 }) + .build(); + writeFileSync("aa.docx", buf); + const z = new Zip(Buffer.from(buf)); + for (const e of z.getEntries()) { + if (e.entryName.match(/document.xml|numbering.xml/)) { + expect(z.readAsText(e)).toMatchSnapshot(); + } + } + }); });