From e182f0e2d352b599c3dc2f5cb1c691f0b050aed5 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Mon, 18 Mar 2024 23:27:28 +0900 Subject: [PATCH] feat: Support adjust_right_ind (#691) * feat: Support adjust_right_ind * beta6 * fix: update spec * fix --- .../documents/elements/adjust_right_ind.rs | 29 +++++++++++++++ docx-core/src/documents/elements/mod.rs | 2 ++ .../documents/elements/paragraph_property.rs | 16 ++++++--- docx-core/src/reader/paragraph_property.rs | 8 +++++ docx-core/src/reader/xml_element.rs | 2 ++ docx-core/src/xml_builder/elements.rs | 2 +- docx-core/src/xml_builder/macros.rs | 35 +++++++++++-------- docx-wasm/.npmignore | 0 docx-wasm/Cargo.toml | 3 ++ docx-wasm/js/json/paragraph.ts | 3 ++ docx-wasm/js/paragraph-property.ts | 10 ++++++ docx-wasm/js/paragraph.ts | 11 ++++++ docx-wasm/package.json | 10 +++--- docx-wasm/src/paragraph.rs | 5 +++ docx-wasm/src/style.rs | 5 +++ .../test/__snapshots__/index.test.js.snap | 26 ++++++++++++++ 16 files changed, 143 insertions(+), 24 deletions(-) create mode 100644 docx-core/src/documents/elements/adjust_right_ind.rs create mode 100644 docx-wasm/.npmignore diff --git a/docx-core/src/documents/elements/adjust_right_ind.rs b/docx-core/src/documents/elements/adjust_right_ind.rs new file mode 100644 index 0000000..0718c19 --- /dev/null +++ b/docx-core/src/documents/elements/adjust_right_ind.rs @@ -0,0 +1,29 @@ +use serde::{Serialize, Serializer}; + +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone, PartialEq)] +pub struct AdjustRightInd(pub isize); + +impl AdjustRightInd { + pub fn new(val: isize) -> AdjustRightInd { + AdjustRightInd(val) + } +} + +impl BuildXML for AdjustRightInd { + fn build(&self) -> Vec { + let b = XMLBuilder::new(); + b.adjust_right_ind(self.0).build() + } +} + +impl Serialize for AdjustRightInd { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_i64(self.0 as i64) + } +} diff --git a/docx-core/src/documents/elements/mod.rs b/docx-core/src/documents/elements/mod.rs index 4fb2d2a..5079967 100644 --- a/docx-core/src/documents/elements/mod.rs +++ b/docx-core/src/documents/elements/mod.rs @@ -1,6 +1,7 @@ mod a_graphic; mod a_graphic_data; mod abstract_numbering; +mod adjust_right_ind; mod based_on; mod bold; mod bold_cs; @@ -130,6 +131,7 @@ mod zoom; pub use a_graphic::*; pub use a_graphic_data::*; pub use abstract_numbering::*; +pub use adjust_right_ind::*; pub use based_on::*; pub use bold::*; pub use bold_cs::*; diff --git a/docx-core/src/documents/elements/paragraph_property.rs b/docx-core/src/documents/elements/paragraph_property.rs index db56a0f..e208027 100644 --- a/docx-core/src/documents/elements/paragraph_property.rs +++ b/docx-core/src/documents/elements/paragraph_property.rs @@ -33,9 +33,6 @@ pub struct ParagraphProperty { #[serde(skip_serializing_if = "Option::is_none")] pub section_property: Option, pub tabs: Vec, - // read only - #[serde(skip_serializing_if = "Option::is_none")] - pub(crate) div_id: Option, #[serde(skip_serializing_if = "Option::is_none")] pub paragraph_property_change: Option, #[serde(skip_serializing_if = "Option::is_none")] @@ -44,6 +41,11 @@ pub struct ParagraphProperty { pub frame_property: Option, #[serde(skip_serializing_if = "Option::is_none")] pub text_alignment: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub adjust_right_ind: Option, + // read only + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) div_id: Option, } // 17.3.1.26 @@ -152,6 +154,11 @@ impl ParagraphProperty { self } + pub fn adjust_right_ind(mut self, s: isize) -> Self { + self.adjust_right_ind = Some(AdjustRightInd::new(s)); + self + } + pub(crate) fn hanging_chars(mut self, chars: i32) -> Self { if let Some(indent) = self.indent { self.indent = Some(indent.hanging_chars(chars)); @@ -200,7 +207,8 @@ fn inner_build(p: &ParagraphProperty) -> Vec { .add_optional_child(&p.outline_lvl) .add_optional_child(&p.paragraph_property_change) .add_optional_child(&p.borders) - .add_optional_child(&p.text_alignment); + .add_optional_child(&p.text_alignment) + .add_optional_child(&p.adjust_right_ind); if let Some(v) = p.keep_next { if v { diff --git a/docx-core/src/reader/paragraph_property.rs b/docx-core/src/reader/paragraph_property.rs index a80d7d6..ffc4b60 100644 --- a/docx-core/src/reader/paragraph_property.rs +++ b/docx-core/src/reader/paragraph_property.rs @@ -56,6 +56,14 @@ impl ElementReader for ParagraphProperty { } continue; } + XMLElement::AdjustRightInd => { + if let Some(val) = read_val(&attributes) { + if let Ok(v) = isize::from_str(&val) { + p = p.adjust_right_ind(v); + } + } + continue; + } XMLElement::ParagraphStyle => { p = p.style(&attributes[0].value); continue; diff --git a/docx-core/src/reader/xml_element.rs b/docx-core/src/reader/xml_element.rs index 732b852..b222898 100644 --- a/docx-core/src/reader/xml_element.rs +++ b/docx-core/src/reader/xml_element.rs @@ -161,6 +161,7 @@ pub enum XMLElement { PageNumType, FrameProperty, TextAlignment, + AdjustRightInd, H, HAnchor, HSpace, @@ -407,6 +408,7 @@ impl FromStr for XMLElement { "pgNumType" => Ok(XMLElement::PageNumType), "framePr" => Ok(XMLElement::FrameProperty), "textAlignment" => Ok(XMLElement::TextAlignment), + "adjustRightInd" => Ok(XMLElement::AdjustRightInd), "h" => Ok(XMLElement::H), "hAnchor" => Ok(XMLElement::HAnchor), "hSpace" => Ok(XMLElement::HSpace), diff --git a/docx-core/src/xml_builder/elements.rs b/docx-core/src/xml_builder/elements.rs index c64d466..27a9684 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -165,7 +165,7 @@ impl XMLBuilder { closed_with_usize!(sz, "w:sz"); // i.e. closed_with_usize!(sz_cs, "w:szCs"); - + closed_with_isize!(adjust_right_ind, "w:adjustRightInd"); closed_with_str!(text_alignment, "w:textAlignment"); closed!(field_character, "w:fldChar", "w:fldCharType", "w:dirty"); diff --git a/docx-core/src/xml_builder/macros.rs b/docx-core/src/xml_builder/macros.rs index 5d87cd0..7514c5c 100644 --- a/docx-core/src/xml_builder/macros.rs +++ b/docx-core/src/xml_builder/macros.rs @@ -522,6 +522,17 @@ macro_rules! closed_with_usize { }; } +macro_rules! closed_with_isize { + ($name: ident, $el_name: expr) => { + pub(crate) fn $name(mut self, val: isize) -> Self { + self.writer + .write(XmlEvent::start_element($el_name).attr("w:val", &format!("{}", val))) + .expect("should write to buf"); + self.close() + } + }; +} + macro_rules! closed_w_with_type_el { ($name: ident, $el_name: expr) => { pub(crate) fn $name(mut self, w: i32, t: WidthType) -> Self { @@ -562,20 +573,16 @@ macro_rules! closed_border_el { macro_rules! closed_paragraph_border_el { ($name: ident, $ el_name: expr) => { - pub(crate) fn $name<'a>( - mut self, - val: &str, - space: &str, - size: &str, - color: &str, - ) -> Self { - self.writer.write( - XmlEvent::start_element($el_name) - .attr("w:val", val) - .attr("w:space", space) - .attr("w:sz", size) - .attr("w:color", color) - ).expect(EXPECT_MESSAGE); + pub(crate) fn $name<'a>(mut self, val: &str, space: &str, size: &str, color: &str) -> Self { + self.writer + .write( + XmlEvent::start_element($el_name) + .attr("w:val", val) + .attr("w:space", space) + .attr("w:sz", size) + .attr("w:color", color), + ) + .expect(EXPECT_MESSAGE); self.close() } }; diff --git a/docx-wasm/.npmignore b/docx-wasm/.npmignore new file mode 100644 index 0000000..e69de29 diff --git a/docx-wasm/Cargo.toml b/docx-wasm/Cargo.toml index 2fef13e..9ef054d 100644 --- a/docx-wasm/Cargo.toml +++ b/docx-wasm/Cargo.toml @@ -13,3 +13,6 @@ crate-type = ["cdylib"] wasm-bindgen = "0.2.78" console_error_panic_hook = "0.1.7" docx-rs= { path = "../docx-core", features = ["wasm"] } + +[profile.release] +lto = true \ No newline at end of file diff --git a/docx-wasm/js/json/paragraph.ts b/docx-wasm/js/json/paragraph.ts index 38a8738..56457b4 100644 --- a/docx-wasm/js/json/paragraph.ts +++ b/docx-wasm/js/json/paragraph.ts @@ -7,6 +7,7 @@ import { } from ".."; import { LineSpacingJSON } from "./line_spacing"; import { FrameProperty as FramePropertyJSON } from "./bindings/FrameProperty"; +import { TextAlignmentType } from "./bindings/TextAlignmentType"; export { FrameProperty as FramePropertyJSON } from "./bindings/FrameProperty"; @@ -64,6 +65,8 @@ export type ParagraphPropertyJSON = { | "start" | "end" | "unsupported"; + textAlignment?: TextAlignmentType; + adjustRightInd?: number; indent?: IndentJSON | null; lineSpacing?: LineSpacingJSON | null; divId?: string | null; diff --git a/docx-wasm/js/paragraph-property.ts b/docx-wasm/js/paragraph-property.ts index f05985e..935a758 100644 --- a/docx-wasm/js/paragraph-property.ts +++ b/docx-wasm/js/paragraph-property.ts @@ -72,6 +72,7 @@ export type ParagraphProperty = { widowControl: boolean; paragraphPropertyChange?: ParagraphPropertyChange; outlineLvl?: number | null; + adjustRightInd?: number; }; export const createDefaultParagraphProperty = (): ParagraphProperty => { @@ -165,6 +166,11 @@ export class ParagraphPropertyChange { return this; } + adjustRightInd(v: number) { + this._property.adjustRightInd = v; + return this; + } + style(id: string) { this._property.styleId = id; return this; @@ -246,6 +252,10 @@ export const setParagraphProperty = ( target = target.text_alignment(textAlignment) as T; } + if (property.adjustRightInd != null) { + target = target.adjust_right_ind(property.adjustRightInd) as T; + } + if (typeof property.indent !== "undefined") { const { indent } = property; let kind; diff --git a/docx-wasm/js/paragraph.ts b/docx-wasm/js/paragraph.ts index 8dfbeeb..1f25d3f 100644 --- a/docx-wasm/js/paragraph.ts +++ b/docx-wasm/js/paragraph.ts @@ -14,6 +14,7 @@ import { BookmarkEnd } from "./bookmark-end"; import { Comment } from "./comment"; import { CommentEnd } from "./comment-end"; import { Hyperlink } from "./hyperlink"; +import { TextAlignmentType } from "./json/bindings/TextAlignmentType"; export type ParagraphChild = | Run @@ -75,6 +76,16 @@ export class Paragraph { return this; } + textAlignment(type: TextAlignmentType) { + this.property.textAlignment = type; + return this; + } + + adjustRightInd(v: number) { + this.property.adjustRightInd = v; + return this; + } + style(id: string) { this.property.styleId = id; return this; diff --git a/docx-wasm/package.json b/docx-wasm/package.json index 0b84c29..632f5cc 100644 --- a/docx-wasm/package.json +++ b/docx-wasm/package.json @@ -1,14 +1,14 @@ { "name": "docx-wasm", - "version": "0.4.12-beta5", + "version": "0.4.12-beta9", "main": "dist/node/index.js", "browser": "dist/web/index.js", "author": "bokuweb ", "license": "MIT", "scripts": { "wasm-pack:dev": "wasm-pack build --out-dir js/pkg", - "wasm-pack:web": "wasm-pack build --release --out-dir dist/web/pkg", - "wasm-pack:node": "wasm-pack build --release --out-dir dist/node/pkg --target nodejs", + "wasm-pack:web": "wasm-pack build --release --out-dir dist/web/pkg && rm dist/web/pkg/.gitignore", + "wasm-pack:node": "wasm-pack build --release --out-dir dist/node/pkg --target nodejs && rm dist/node/pkg/.gitignore", "wasm-pack": "run-s wasm-pack:*", "tsc:web": "tsc -p tsconfig.web.json --sourcemap", "tsc:node": "tsc -p tsconfig.node.json --sourcemap", @@ -44,8 +44,8 @@ }, "files": [ "dist", - "dist/web/pkg/*", - "dist/node/pkg/*", + "dist/web/pkg", + "dist/node/pkg", "js/*" ], "module": "dist/web/index.js", diff --git a/docx-wasm/src/paragraph.rs b/docx-wasm/src/paragraph.rs index dc0bab0..7c244aa 100644 --- a/docx-wasm/src/paragraph.rs +++ b/docx-wasm/src/paragraph.rs @@ -136,6 +136,11 @@ impl Paragraph { self } + pub fn adjust_right_ind(mut self, v: isize) -> Paragraph { + self.0.property = self.0.property.adjust_right_ind(v); + self + } + pub fn outline_lvl(mut self, level: usize) -> Paragraph { self.0.property = self.0.property.outline_lvl(level); self diff --git a/docx-wasm/src/style.rs b/docx-wasm/src/style.rs index 8beb8d6..e111d24 100644 --- a/docx-wasm/src/style.rs +++ b/docx-wasm/src/style.rs @@ -109,6 +109,11 @@ impl Style { self } + pub fn adjust_right_ind(mut self, v: isize) -> Self { + self.0.paragraph_property = self.0.paragraph_property.adjust_right_ind(v); + self + } + pub fn indent( mut self, left: i32, diff --git a/docx-wasm/test/__snapshots__/index.test.js.snap b/docx-wasm/test/__snapshots__/index.test.js.snap index c98ff87..d29799e 100644 --- a/docx-wasm/test/__snapshots__/index.test.js.snap +++ b/docx-wasm/test/__snapshots__/index.test.js.snap @@ -3470,6 +3470,7 @@ Object { "name": "Level 1", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "indent": Object { "end": null, "firstLineChars": null, @@ -3558,6 +3559,7 @@ Object { "name": "Level 2", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "indent": Object { "end": null, "firstLineChars": null, @@ -34417,6 +34419,7 @@ Object { "name": "heading 3", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "indent": Object { "end": null, "firstLineChars": null, @@ -96362,6 +96365,7 @@ Object { "name": "Normal", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "alignment": "both", "lineSpacing": Object { "line": 360, @@ -100950,6 +100954,7 @@ Object { "name": "List Number", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "indent": Object { "end": null, "firstLineChars": 0, @@ -101061,6 +101066,7 @@ Object { "name": "Reference2", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "alignment": "both", "indent": Object { "end": null, @@ -101324,6 +101330,7 @@ Object { "name": "Sentence indent2", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "alignment": "both", "indent": Object { "end": null, @@ -101493,6 +101500,7 @@ Object { "name": "Definition_sentence", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "alignment": "both", "indent": Object { "end": null, @@ -101664,6 +101672,7 @@ Object { "name": "List Number 2", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "alignment": "both", "lineSpacing": Object { "before": 120, @@ -101753,6 +101762,7 @@ Object { "name": "Note1", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "alignment": "both", "lineSpacing": Object { "before": 120, @@ -101842,6 +101852,7 @@ Object { "name": "Reference1", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "indent": Object { "end": null, "firstLineChars": null, @@ -101945,6 +101956,7 @@ Object { "name": "Sentence indent1", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "alignment": "both", "indent": Object { "end": null, @@ -102039,6 +102051,7 @@ Object { "name": "Bullet1", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "alignment": "both", "indent": Object { "end": null, @@ -102137,6 +102150,7 @@ Object { "name": "List Number 3", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "keepNext": true, "lineSpacing": Object { "before": 240, @@ -102226,6 +102240,7 @@ Object { "name": "Note2", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "alignment": "both", "lineSpacing": Object { "before": 120, @@ -102316,6 +102331,7 @@ Object { "name": "Note3", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "alignment": "both", "lineSpacing": Object { "before": 120, @@ -102405,6 +102421,7 @@ Object { "name": "Bullet2", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "alignment": "both", "indent": Object { "end": null, @@ -102516,6 +102533,7 @@ Object { "name": "Sentence indent3", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "alignment": "both", "indent": Object { "end": null, @@ -103562,6 +103580,7 @@ Object { "name": "表紙の日付他", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "alignment": "right", "keepLines": true, "keepNext": true, @@ -104958,6 +104977,7 @@ Object { "name": "macro", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "runProperty": Object {}, "tabs": Array [ Object { @@ -109644,6 +109664,7 @@ Object { "name": "Default", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "runProperty": Object {}, "tabs": Array [], }, @@ -109800,6 +109821,7 @@ Object { "name": "Table Bullet1", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "lineSpacing": Object { "before": 20, "line": 200, @@ -143496,6 +143518,7 @@ Object { "name": "Char", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "alignment": "left", "indent": Object { "end": 0, @@ -143619,6 +143642,7 @@ Object { "name": "Char1 Char Char Znak Znak1", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "indent": Object { "end": 0, "firstLineChars": null, @@ -143736,6 +143760,7 @@ Object { "name": "Char1", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "indent": Object { "end": 0, "firstLineChars": null, @@ -167653,6 +167678,7 @@ Object { "name": "Table Grid", "next": null, "paragraphProperty": Object { + "adjustRightInd": 0, "alignment": "both", "lineSpacing": Object { "line": 340,