diff --git a/CHANGELOG.md b/CHANGELOG.md index f8773be..e31215a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## @0.4.15 (12. Apr, 2024) +## @0.4.16 (24. Apr, 2024) + +- Fixed a `framePr` + +## @0.4.14 (12. Apr, 2024) - Remove `dbg!` - Support `caps` diff --git a/docx-core/src/documents/elements/page_num.rs b/docx-core/src/documents/elements/page_num.rs index c9210ff..c24f735 100644 --- a/docx-core/src/documents/elements/page_num.rs +++ b/docx-core/src/documents/elements/page_num.rs @@ -9,6 +9,8 @@ pub struct PageNum { pub instr: InstrPAGE, #[serde(skip_serializing_if = "Option::is_none")] pub frame_property: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub paragraph_property: Option, } impl Default for PageNum { @@ -16,6 +18,7 @@ impl Default for PageNum { Self { instr: InstrPAGE {}, frame_property: None, + paragraph_property: None, } } } @@ -121,6 +124,15 @@ impl PageNum { self } + pub fn align(mut self, alignment_type: AlignmentType) -> Self { + self.paragraph_property = Some( + self.paragraph_property + .unwrap_or_default() + .align(alignment_type), + ); + self + } + fn inner_build(&self) -> Vec { let p = StructuredDataTagProperty::new(); let mut b = XMLBuilder::new(); @@ -139,6 +151,10 @@ impl PageNum { .add_field_char(FieldCharType::End, false), ); + if let Some(ref pr) = self.paragraph_property { + p.property = pr.clone(); + } + if let Some(ref f) = self.frame_property { p.property.frame_property = Some(f.clone()); } diff --git a/docx-wasm/js/page-num.ts b/docx-wasm/js/page-num.ts index 5a23e03..cc494bb 100644 --- a/docx-wasm/js/page-num.ts +++ b/docx-wasm/js/page-num.ts @@ -1,3 +1,9 @@ +import { + AlignmentType, + ParagraphProperty, + createDefaultParagraphProperty, + createParagraphAlignment, +} from "./paragraph-property"; import * as wasm from "./pkg/docx_wasm"; export type FrameProperty = { @@ -17,6 +23,7 @@ export type FrameProperty = { export class PageNum { frameProperty: FrameProperty | null = null; + paragraphProperty: ParagraphProperty | null = null; height(h: number) { this.frameProperty = { ...this.frameProperty }; @@ -89,6 +96,14 @@ export class PageNum { return this; } + align(align: AlignmentType) { + this.paragraphProperty = { + ...createDefaultParagraphProperty(), + align, + }; + return this; + } + build() { let pageNum = wasm.createPageNum(); if (this.frameProperty?.h != null) { @@ -127,6 +142,12 @@ export class PageNum { if (this.frameProperty?.yAlign != null) { pageNum = pageNum.y_align(this.frameProperty.yAlign); } + if (this.paragraphProperty?.align != null) { + const align = createParagraphAlignment(this.paragraphProperty.align); + if (align) { + pageNum = pageNum.align(align); + } + } return pageNum; } } diff --git a/docx-wasm/js/paragraph-property.ts b/docx-wasm/js/paragraph-property.ts index d3b6658..095f609 100644 --- a/docx-wasm/js/paragraph-property.ts +++ b/docx-wasm/js/paragraph-property.ts @@ -3,15 +3,9 @@ import { RunProperty, createDefaultRunProperty } from "./run"; import * as wasm from "./pkg"; import { TextAlignmentType } from "./json/bindings/TextAlignmentType"; import { Tab } from "./json/bindings/Tab"; +import { AlignmentType } from "./json/bindings/AlignmentType"; -export type AlignmentType = - | "center" - | "left" - | "right" - | "both" - | "justified" - | "distribute" - | "end"; +export { AlignmentType } from "./json/bindings/AlignmentType"; export type SpecialIndentKind = "firstLine" | "hanging"; diff --git a/docx-wasm/package.json b/docx-wasm/package.json index 2881941..797dfe9 100644 --- a/docx-wasm/package.json +++ b/docx-wasm/package.json @@ -1,6 +1,6 @@ { "name": "docx-wasm", - "version": "0.4.15", + "version": "0.4.16", "main": "dist/node/index.js", "browser": "dist/web/index.js", "author": "bokuweb ", @@ -24,32 +24,5 @@ }, "resolutions": { "**/serialize-javascript": "6.0.1" - }, - "devDependencies": { - "@types/file-saver": "2.0.5", - "@wasm-tool/wasm-pack-plugin": "1.6.0", - "adm-zip": "0.5.10", - "cpy-cli": "4.2.0", - "file-saver": "2.0.5", - "html-webpack-plugin": "5.5.3", - "jest": "28.1.3", - "npm-run-all": "4.1.5", - "text-encoding": "0.7.0", - "ts-loader": "9.4.2", - "typescript": "4.9.3", - "webpack": "4.46.0", - "webpack-cli": "5.0.1", - "webpack-dev-server": "3.11.3", - "webpack-merge": "5.8.0" - }, - "files": [ - "dist", - "dist/web/pkg", - "dist/node/pkg", - "js/*" - ], - "module": "dist/web/index.js", - "types": "dist/web/index.d.ts", - "dependencies": {} -} - + } +} \ No newline at end of file diff --git a/docx-wasm/src/page_num.rs b/docx-wasm/src/page_num.rs index 6752abe..8698628 100644 --- a/docx-wasm/src/page_num.rs +++ b/docx-wasm/src/page_num.rs @@ -76,4 +76,15 @@ impl PageNum { self.0.frame_property = Some(self.0.frame_property.unwrap_or_default().height(n)); self } + + // TODO: add other pPr fields + pub fn align(mut self, alignment_type: docx_rs::AlignmentType) -> Self { + self.0.paragraph_property = Some( + self.0 + .paragraph_property + .unwrap_or_default() + .align(alignment_type), + ); + self + } } diff --git a/docx-wasm/test/index.test.js b/docx-wasm/test/index.test.js index 5836309..8c71923 100644 --- a/docx-wasm/test/index.test.js +++ b/docx-wasm/test/index.test.js @@ -1046,7 +1046,7 @@ describe("writer", () => { test("should write pageNum in header", () => { const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!")); - const page = new w.PageNum(); + const page = new w.PageNum().align('center'); const header = new w.Header().addParagraph(p).addPageNum(page); const buffer = new w.Docx().header(header).addParagraph(p).build();