impl: Add header writer with js (#371)
parent
d8f5b774d5
commit
59fb7c113f
|
@ -0,0 +1,14 @@
|
||||||
|
import { Paragraph } from "./paragraph";
|
||||||
|
|
||||||
|
export class Header {
|
||||||
|
hasNumberings = false;
|
||||||
|
children: Paragraph[] = [];
|
||||||
|
|
||||||
|
addParagraph(p: Paragraph) {
|
||||||
|
if (p.hasNumberings) {
|
||||||
|
this.hasNumberings = true;
|
||||||
|
}
|
||||||
|
this.children.push(p);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,6 +20,7 @@ import { DocProps } from "./doc-props";
|
||||||
import { Styles } from "./styles";
|
import { Styles } from "./styles";
|
||||||
import { WebExtension } from "./webextension";
|
import { WebExtension } from "./webextension";
|
||||||
import { Footer } from "./footer";
|
import { Footer } from "./footer";
|
||||||
|
import { Header } from "./header";
|
||||||
import {
|
import {
|
||||||
SectionProperty,
|
SectionProperty,
|
||||||
PageMargin,
|
PageMargin,
|
||||||
|
@ -151,6 +152,11 @@ export class Docx {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
header(f: Header) {
|
||||||
|
this.sectionProperty._header = f;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
footer(f: Footer) {
|
footer(f: Footer) {
|
||||||
this.sectionProperty._footer = f;
|
this.sectionProperty._footer = f;
|
||||||
return this;
|
return this;
|
||||||
|
@ -849,6 +855,14 @@ export class Docx {
|
||||||
docx = docx.add_doc_var(v.name, v.val);
|
docx = docx.add_doc_var(v.name, v.val);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (this.sectionProperty._header) {
|
||||||
|
let header = wasm.createHeader();
|
||||||
|
this.sectionProperty._header.children.forEach((c) => {
|
||||||
|
header = header.add_paragraph(this.buildParagraph(c));
|
||||||
|
});
|
||||||
|
docx = docx.header(header);
|
||||||
|
}
|
||||||
|
|
||||||
if (this.sectionProperty._footer) {
|
if (this.sectionProperty._footer) {
|
||||||
let footer = wasm.createFooter();
|
let footer = wasm.createFooter();
|
||||||
this.sectionProperty._footer.children.forEach((c) => {
|
this.sectionProperty._footer.children.forEach((c) => {
|
||||||
|
@ -1008,4 +1022,5 @@ export * from "./level";
|
||||||
export * from "./tab";
|
export * from "./tab";
|
||||||
export * from "./json";
|
export * from "./json";
|
||||||
export * from "./webextension";
|
export * from "./webextension";
|
||||||
|
export * from "./header";
|
||||||
export * from "./footer";
|
export * from "./footer";
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { DocGridType } from ".";
|
import { DocGridType } from ".";
|
||||||
import { Footer } from "./footer";
|
import { Footer } from "./footer";
|
||||||
|
import { Header } from "./header";
|
||||||
|
|
||||||
export type DocGrid = {
|
export type DocGrid = {
|
||||||
gridType: DocGridType;
|
gridType: DocGridType;
|
||||||
|
@ -17,6 +18,7 @@ export class SectionProperty {
|
||||||
gridType: "lines",
|
gridType: "lines",
|
||||||
linePitch: 360,
|
linePitch: 360,
|
||||||
};
|
};
|
||||||
|
_header: Header | null = null;
|
||||||
_footer: Footer | null = null;
|
_footer: Footer | null = null;
|
||||||
|
|
||||||
pageSize(w: number, h: number) {
|
pageSize(w: number, h: number) {
|
||||||
|
@ -40,6 +42,11 @@ export class SectionProperty {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
header(header: Header) {
|
||||||
|
this._header = header;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
footer(footer: Footer) {
|
footer(footer: Footer) {
|
||||||
this._footer = footer;
|
this._footer = footer;
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -73,6 +73,11 @@ impl Docx {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn header(mut self, header: Header) -> Self {
|
||||||
|
self.0 = self.0.header(header.take());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn footer(mut self, footer: Footer) -> Self {
|
pub fn footer(mut self, footer: Footer) -> Self {
|
||||||
self.0 = self.0.footer(footer.take());
|
self.0 = self.0.footer(footer.take());
|
||||||
self
|
self
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
use super::*;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Header(docx_rs::Header);
|
||||||
|
|
||||||
|
#[wasm_bindgen(js_name = createHeader)]
|
||||||
|
pub fn create_footer() -> Header {
|
||||||
|
Header(docx_rs::Header::new())
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Header {
|
||||||
|
pub fn take(self) -> docx_rs::Header {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
impl Header {
|
||||||
|
pub fn add_paragraph(mut self, p: Paragraph) -> Self {
|
||||||
|
self.0 = self.0.add_paragraph(p.take());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_table(mut self, t: Table) -> Self {
|
||||||
|
self.0 = self.0.add_table(t.take());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ mod comment;
|
||||||
mod delete;
|
mod delete;
|
||||||
mod doc;
|
mod doc;
|
||||||
mod footer;
|
mod footer;
|
||||||
|
mod header;
|
||||||
mod insert;
|
mod insert;
|
||||||
mod level;
|
mod level;
|
||||||
mod level_override;
|
mod level_override;
|
||||||
|
@ -26,6 +27,7 @@ pub use comment::*;
|
||||||
pub use delete::*;
|
pub use delete::*;
|
||||||
pub use doc::*;
|
pub use doc::*;
|
||||||
pub use footer::*;
|
pub use footer::*;
|
||||||
|
pub use header::*;
|
||||||
pub use insert::*;
|
pub use insert::*;
|
||||||
pub use level::*;
|
pub use level::*;
|
||||||
pub use level_override::*;
|
pub use level_override::*;
|
||||||
|
|
|
@ -23751,6 +23751,25 @@ exports[`writer should write footer for default section 3`] = `
|
||||||
<w:ftr 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\\" 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\\" mc:Ignorable=\\"w14 wp14\\"><w:p w14:paraId=\\"00000002\\"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr><w:rFonts /></w:rPr><w:t xml:space=\\"preserve\\">Hello Footer</w:t></w:r></w:p></w:ftr>"
|
<w:ftr 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\\" 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\\" mc:Ignorable=\\"w14 wp14\\"><w:p w14:paraId=\\"00000002\\"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr><w:rFonts /></w:rPr><w:t xml:space=\\"preserve\\">Hello Footer</w:t></w:r></w:p></w:ftr>"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`writer should write header for default section 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\\" />
|
||||||
|
<Relationship Id=\\"rIdHeader1\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header\\" Target=\\"header1.xml\\" />
|
||||||
|
</Relationships>"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`writer should write header for default section 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=\\"00000001\\"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr><w:rFonts /></w:rPr><w:t xml:space=\\"preserve\\">World </w:t></w:r></w:p><w:sectPr><w:pgSz w:w=\\"11906\\" w:h=\\"16838\\" /><w:pgMar w:top=\\"1985\\" w:right=\\"1701\\" w:bottom=\\"1701\\" w:left=\\"1701\\" w:header=\\"851\\" w:footer=\\"992\\" w:gutter=\\"0\\" /><w:cols w:space=\\"425\\" /><w:docGrid w:type=\\"lines\\" w:linePitch=\\"360\\" /><w:headerReference w:type=\\"default\\" r:id=\\"rIdHeader1\\" /></w:sectPr></w:body>
|
||||||
|
</w:document>"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`writer should write hello 1`] = `
|
exports[`writer should write hello 1`] = `
|
||||||
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
|
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
|
||||||
<Relationships xmlns=\\"http://schemas.openxmlformats.org/package/2006/relationships\\">
|
<Relationships xmlns=\\"http://schemas.openxmlformats.org/package/2006/relationships\\">
|
||||||
|
|
|
@ -384,4 +384,18 @@ describe("writer", () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("should write header for default section", () => {
|
||||||
|
const p1 = new w.Paragraph().addRun(new w.Run().addText("Hello Header"));
|
||||||
|
const p2 = new w.Paragraph().addRun(new w.Run().addText("World "));
|
||||||
|
const header = new w.Header().addParagraph(p1);
|
||||||
|
const buffer = new w.Docx().header(header).addParagraph(p2).build();
|
||||||
|
writeFileSync("../output/header.docx", buffer);
|
||||||
|
const z = new Zip(Buffer.from(buffer));
|
||||||
|
for (const e of z.getEntries()) {
|
||||||
|
if (e.entryName.match(/document.xml|footer1.xml/)) {
|
||||||
|
expect(z.readAsText(e)).toMatchSnapshot();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue