Add page margin test (#175)

* fix: page margin

* fix: test

* 0.0.109
main
bokuweb 2020-10-13 00:04:09 +09:00 committed by GitHub
parent 7e89f9a50e
commit 425e2bce08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 117 additions and 22 deletions

View File

@ -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(())

View File

@ -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 {

View File

@ -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);
}

View File

@ -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 <bokuweb12@gmail.com>",

View File

@ -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
}

View File

@ -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::*;

View File

@ -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
})
}
}

View File

@ -3730,6 +3730,33 @@ exports[`writer should write lvlOverride with level 3`] = `
<w:abstractNumId w:val=\\"0\\" /><w:lvlOverride w:ilvl=\\"0\\"><w:lvl w:ilvl=\\"0\\"><w:start w:val=\\"3\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%1\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /></w:pPr></w:lvl></w:lvlOverride></w:num></w:numbering>"
`;
exports[`writer should write page margin 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<Relationships xmlns=\\"http://schemas.openxmlformats.org/package/2006/relationships\\">
<Relationship Id=\\"rId1\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\\" Target=\\"styles.xml\\" />
<Relationship Id=\\"rId2\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\\" Target=\\"fontTable.xml\\" />
<Relationship Id=\\"rId3\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\\" Target=\\"settings.xml\\" />
<Relationship Id=\\"rId4\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header\\" Target=\\"header1.xml\\" />
<Relationship Id=\\"rId5\\" Type=\\"http://schemas.microsoft.com/office/2011/relationships/commentsExtended\\" Target=\\"commentsExtended.xml\\" />
</Relationships>"
`;
exports[`writer should write page margin 2`] = `
"<?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:p w14:paraId=\\"00000020\\"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr><w:rFonts /></w:rPr><w:t xml:space=\\"preserve\\">Hello world!!</w:t></w:r></w:p><w:sectPr><w:pgSz w:w=\\"11906\\" w:h=\\"16838\\" /><w:pgMar w:top=\\"1000\\" w:right=\\"1701\\" w:bottom=\\"1701\\" w:left=\\"2000\\" w:header=\\"851\\" w:footer=\\"992\\" w:gutter=\\"0\\" /><w:headerReference w:type=\\"default\\" r:id=\\"rId4\\" /><w:cols w:space=\\"425\\" />
<w:docGrid w:type=\\"lines\\" w:linePitch=\\"360\\" />
</w:sectPr></w:body>
</w:document>"
`;
exports[`writer should write page margin 3`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?>
<w:numbering xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\" xmlns:w=\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\"><w:abstractNum w:abstractNumId=\\"1\\"><w:lvl w:ilvl=\\"0\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%1.\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"420\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr></w:lvl><w:lvl w:ilvl=\\"1\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"(%2)\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"840\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr></w:lvl><w:lvl w:ilvl=\\"2\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimalEnclosedCircle\\" /><w:lvlText w:val=\\"%3\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"1260\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr></w:lvl><w:lvl w:ilvl=\\"3\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%4.\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"1680\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr></w:lvl><w:lvl w:ilvl=\\"4\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"(%5)\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"2100\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr></w:lvl><w:lvl w:ilvl=\\"5\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimalEnclosedCircle\\" /><w:lvlText w:val=\\"%6\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"2520\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr></w:lvl><w:lvl w:ilvl=\\"6\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%7.\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"2940\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr></w:lvl><w:lvl w:ilvl=\\"7\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"(%8)\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"3360\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr></w:lvl><w:lvl w:ilvl=\\"8\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimalEnclosedCircle\\" /><w:lvlText w:val=\\"%9\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"3780\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr></w:lvl></w:abstractNum><w:num w:numId=\\"1\\">
<w:abstractNumId w:val=\\"1\\" />
</w:num></w:numbering>"
`;
exports[`writer should write page size 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<Relationships xmlns=\\"http://schemas.openxmlformats.org/package/2006/relationships\\">

View File

@ -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();
}
}
});
});