Fix frame property (#706)

* fix

* fix
main
bokuweb 2024-04-24 19:38:22 +09:00 committed by GitHub
parent 83139b233e
commit dcc524e6c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 59 additions and 40 deletions

View File

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

View File

@ -9,6 +9,8 @@ pub struct PageNum {
pub instr: InstrPAGE,
#[serde(skip_serializing_if = "Option::is_none")]
pub frame_property: Option<FrameProperty>,
#[serde(skip_serializing_if = "Option::is_none")]
pub paragraph_property: Option<ParagraphProperty>,
}
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<u8> {
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());
}

View File

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

View File

@ -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";

View File

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

View File

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

View File

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