feat: Support adjust_right_ind (#691)

* feat: Support adjust_right_ind

* beta6

* fix: update spec

* fix
main
bokuweb 2024-03-18 23:27:28 +09:00 committed by GitHub
parent 13b8d9bc44
commit e182f0e2d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 143 additions and 24 deletions

View File

@ -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<u8> {
let b = XMLBuilder::new();
b.adjust_right_ind(self.0).build()
}
}
impl Serialize for AdjustRightInd {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_i64(self.0 as i64)
}
}

View File

@ -1,6 +1,7 @@
mod a_graphic; mod a_graphic;
mod a_graphic_data; mod a_graphic_data;
mod abstract_numbering; mod abstract_numbering;
mod adjust_right_ind;
mod based_on; mod based_on;
mod bold; mod bold;
mod bold_cs; mod bold_cs;
@ -130,6 +131,7 @@ mod zoom;
pub use a_graphic::*; pub use a_graphic::*;
pub use a_graphic_data::*; pub use a_graphic_data::*;
pub use abstract_numbering::*; pub use abstract_numbering::*;
pub use adjust_right_ind::*;
pub use based_on::*; pub use based_on::*;
pub use bold::*; pub use bold::*;
pub use bold_cs::*; pub use bold_cs::*;

View File

@ -33,9 +33,6 @@ pub struct ParagraphProperty {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub section_property: Option<SectionProperty>, pub section_property: Option<SectionProperty>,
pub tabs: Vec<Tab>, pub tabs: Vec<Tab>,
// read only
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) div_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub paragraph_property_change: Option<ParagraphPropertyChange>, pub paragraph_property_change: Option<ParagraphPropertyChange>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
@ -44,6 +41,11 @@ pub struct ParagraphProperty {
pub frame_property: Option<FrameProperty>, pub frame_property: Option<FrameProperty>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub text_alignment: Option<TextAlignment>, pub text_alignment: Option<TextAlignment>,
#[serde(skip_serializing_if = "Option::is_none")]
pub adjust_right_ind: Option<AdjustRightInd>,
// read only
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) div_id: Option<String>,
} }
// 17.3.1.26 // 17.3.1.26
@ -152,6 +154,11 @@ impl ParagraphProperty {
self 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 { pub(crate) fn hanging_chars(mut self, chars: i32) -> Self {
if let Some(indent) = self.indent { if let Some(indent) = self.indent {
self.indent = Some(indent.hanging_chars(chars)); self.indent = Some(indent.hanging_chars(chars));
@ -200,7 +207,8 @@ fn inner_build(p: &ParagraphProperty) -> Vec<u8> {
.add_optional_child(&p.outline_lvl) .add_optional_child(&p.outline_lvl)
.add_optional_child(&p.paragraph_property_change) .add_optional_child(&p.paragraph_property_change)
.add_optional_child(&p.borders) .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 let Some(v) = p.keep_next {
if v { if v {

View File

@ -56,6 +56,14 @@ impl ElementReader for ParagraphProperty {
} }
continue; 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 => { XMLElement::ParagraphStyle => {
p = p.style(&attributes[0].value); p = p.style(&attributes[0].value);
continue; continue;

View File

@ -161,6 +161,7 @@ pub enum XMLElement {
PageNumType, PageNumType,
FrameProperty, FrameProperty,
TextAlignment, TextAlignment,
AdjustRightInd,
H, H,
HAnchor, HAnchor,
HSpace, HSpace,
@ -407,6 +408,7 @@ impl FromStr for XMLElement {
"pgNumType" => Ok(XMLElement::PageNumType), "pgNumType" => Ok(XMLElement::PageNumType),
"framePr" => Ok(XMLElement::FrameProperty), "framePr" => Ok(XMLElement::FrameProperty),
"textAlignment" => Ok(XMLElement::TextAlignment), "textAlignment" => Ok(XMLElement::TextAlignment),
"adjustRightInd" => Ok(XMLElement::AdjustRightInd),
"h" => Ok(XMLElement::H), "h" => Ok(XMLElement::H),
"hAnchor" => Ok(XMLElement::HAnchor), "hAnchor" => Ok(XMLElement::HAnchor),
"hSpace" => Ok(XMLElement::HSpace), "hSpace" => Ok(XMLElement::HSpace),

View File

@ -165,7 +165,7 @@ impl XMLBuilder {
closed_with_usize!(sz, "w:sz"); closed_with_usize!(sz, "w:sz");
// i.e. <w:szCs ... > // i.e. <w:szCs ... >
closed_with_usize!(sz_cs, "w:szCs"); closed_with_usize!(sz_cs, "w:szCs");
closed_with_isize!(adjust_right_ind, "w:adjustRightInd");
closed_with_str!(text_alignment, "w:textAlignment"); closed_with_str!(text_alignment, "w:textAlignment");
closed!(field_character, "w:fldChar", "w:fldCharType", "w:dirty"); closed!(field_character, "w:fldChar", "w:fldCharType", "w:dirty");

View File

@ -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 { macro_rules! closed_w_with_type_el {
($name: ident, $el_name: expr) => { ($name: ident, $el_name: expr) => {
pub(crate) fn $name(mut self, w: i32, t: WidthType) -> Self { 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 { macro_rules! closed_paragraph_border_el {
($name: ident, $ el_name: expr) => { ($name: ident, $ el_name: expr) => {
pub(crate) fn $name<'a>( pub(crate) fn $name<'a>(mut self, val: &str, space: &str, size: &str, color: &str) -> Self {
mut self, self.writer
val: &str, .write(
space: &str, XmlEvent::start_element($el_name)
size: &str, .attr("w:val", val)
color: &str, .attr("w:space", space)
) -> Self { .attr("w:sz", size)
self.writer.write( .attr("w:color", color),
XmlEvent::start_element($el_name) )
.attr("w:val", val) .expect(EXPECT_MESSAGE);
.attr("w:space", space)
.attr("w:sz", size)
.attr("w:color", color)
).expect(EXPECT_MESSAGE);
self.close() self.close()
} }
}; };

View File

View File

@ -13,3 +13,6 @@ crate-type = ["cdylib"]
wasm-bindgen = "0.2.78" wasm-bindgen = "0.2.78"
console_error_panic_hook = "0.1.7" console_error_panic_hook = "0.1.7"
docx-rs= { path = "../docx-core", features = ["wasm"] } docx-rs= { path = "../docx-core", features = ["wasm"] }
[profile.release]
lto = true

View File

@ -7,6 +7,7 @@ import {
} from ".."; } from "..";
import { LineSpacingJSON } from "./line_spacing"; import { LineSpacingJSON } from "./line_spacing";
import { FrameProperty as FramePropertyJSON } from "./bindings/FrameProperty"; import { FrameProperty as FramePropertyJSON } from "./bindings/FrameProperty";
import { TextAlignmentType } from "./bindings/TextAlignmentType";
export { FrameProperty as FramePropertyJSON } from "./bindings/FrameProperty"; export { FrameProperty as FramePropertyJSON } from "./bindings/FrameProperty";
@ -64,6 +65,8 @@ export type ParagraphPropertyJSON = {
| "start" | "start"
| "end" | "end"
| "unsupported"; | "unsupported";
textAlignment?: TextAlignmentType;
adjustRightInd?: number;
indent?: IndentJSON | null; indent?: IndentJSON | null;
lineSpacing?: LineSpacingJSON | null; lineSpacing?: LineSpacingJSON | null;
divId?: string | null; divId?: string | null;

View File

@ -72,6 +72,7 @@ export type ParagraphProperty = {
widowControl: boolean; widowControl: boolean;
paragraphPropertyChange?: ParagraphPropertyChange; paragraphPropertyChange?: ParagraphPropertyChange;
outlineLvl?: number | null; outlineLvl?: number | null;
adjustRightInd?: number;
}; };
export const createDefaultParagraphProperty = (): ParagraphProperty => { export const createDefaultParagraphProperty = (): ParagraphProperty => {
@ -165,6 +166,11 @@ export class ParagraphPropertyChange {
return this; return this;
} }
adjustRightInd(v: number) {
this._property.adjustRightInd = v;
return this;
}
style(id: string) { style(id: string) {
this._property.styleId = id; this._property.styleId = id;
return this; return this;
@ -246,6 +252,10 @@ export const setParagraphProperty = <T extends wasm.Paragraph | wasm.Style>(
target = target.text_alignment(textAlignment) as T; 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") { if (typeof property.indent !== "undefined") {
const { indent } = property; const { indent } = property;
let kind; let kind;

View File

@ -14,6 +14,7 @@ import { BookmarkEnd } from "./bookmark-end";
import { Comment } from "./comment"; import { Comment } from "./comment";
import { CommentEnd } from "./comment-end"; import { CommentEnd } from "./comment-end";
import { Hyperlink } from "./hyperlink"; import { Hyperlink } from "./hyperlink";
import { TextAlignmentType } from "./json/bindings/TextAlignmentType";
export type ParagraphChild = export type ParagraphChild =
| Run | Run
@ -75,6 +76,16 @@ export class Paragraph {
return this; return this;
} }
textAlignment(type: TextAlignmentType) {
this.property.textAlignment = type;
return this;
}
adjustRightInd(v: number) {
this.property.adjustRightInd = v;
return this;
}
style(id: string) { style(id: string) {
this.property.styleId = id; this.property.styleId = id;
return this; return this;

View File

@ -1,14 +1,14 @@
{ {
"name": "docx-wasm", "name": "docx-wasm",
"version": "0.4.12-beta5", "version": "0.4.12-beta9",
"main": "dist/node/index.js", "main": "dist/node/index.js",
"browser": "dist/web/index.js", "browser": "dist/web/index.js",
"author": "bokuweb <bokuweb12@gmail.com>", "author": "bokuweb <bokuweb12@gmail.com>",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"wasm-pack:dev": "wasm-pack build --out-dir js/pkg", "wasm-pack:dev": "wasm-pack build --out-dir js/pkg",
"wasm-pack:web": "wasm-pack build --release --out-dir dist/web/pkg", "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", "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:*", "wasm-pack": "run-s wasm-pack:*",
"tsc:web": "tsc -p tsconfig.web.json --sourcemap", "tsc:web": "tsc -p tsconfig.web.json --sourcemap",
"tsc:node": "tsc -p tsconfig.node.json --sourcemap", "tsc:node": "tsc -p tsconfig.node.json --sourcemap",
@ -44,8 +44,8 @@
}, },
"files": [ "files": [
"dist", "dist",
"dist/web/pkg/*", "dist/web/pkg",
"dist/node/pkg/*", "dist/node/pkg",
"js/*" "js/*"
], ],
"module": "dist/web/index.js", "module": "dist/web/index.js",

View File

@ -136,6 +136,11 @@ impl Paragraph {
self 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 { pub fn outline_lvl(mut self, level: usize) -> Paragraph {
self.0.property = self.0.property.outline_lvl(level); self.0.property = self.0.property.outline_lvl(level);
self self

View File

@ -109,6 +109,11 @@ impl Style {
self 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( pub fn indent(
mut self, mut self,
left: i32, left: i32,

View File

@ -3470,6 +3470,7 @@ Object {
"name": "Level 1", "name": "Level 1",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"indent": Object { "indent": Object {
"end": null, "end": null,
"firstLineChars": null, "firstLineChars": null,
@ -3558,6 +3559,7 @@ Object {
"name": "Level 2", "name": "Level 2",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"indent": Object { "indent": Object {
"end": null, "end": null,
"firstLineChars": null, "firstLineChars": null,
@ -34417,6 +34419,7 @@ Object {
"name": "heading 3", "name": "heading 3",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"indent": Object { "indent": Object {
"end": null, "end": null,
"firstLineChars": null, "firstLineChars": null,
@ -96362,6 +96365,7 @@ Object {
"name": "Normal", "name": "Normal",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"alignment": "both", "alignment": "both",
"lineSpacing": Object { "lineSpacing": Object {
"line": 360, "line": 360,
@ -100950,6 +100954,7 @@ Object {
"name": "List Number", "name": "List Number",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"indent": Object { "indent": Object {
"end": null, "end": null,
"firstLineChars": 0, "firstLineChars": 0,
@ -101061,6 +101066,7 @@ Object {
"name": "Reference2", "name": "Reference2",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"alignment": "both", "alignment": "both",
"indent": Object { "indent": Object {
"end": null, "end": null,
@ -101324,6 +101330,7 @@ Object {
"name": "Sentence indent2", "name": "Sentence indent2",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"alignment": "both", "alignment": "both",
"indent": Object { "indent": Object {
"end": null, "end": null,
@ -101493,6 +101500,7 @@ Object {
"name": "Definition_sentence", "name": "Definition_sentence",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"alignment": "both", "alignment": "both",
"indent": Object { "indent": Object {
"end": null, "end": null,
@ -101664,6 +101672,7 @@ Object {
"name": "List Number 2", "name": "List Number 2",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"alignment": "both", "alignment": "both",
"lineSpacing": Object { "lineSpacing": Object {
"before": 120, "before": 120,
@ -101753,6 +101762,7 @@ Object {
"name": "Note1", "name": "Note1",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"alignment": "both", "alignment": "both",
"lineSpacing": Object { "lineSpacing": Object {
"before": 120, "before": 120,
@ -101842,6 +101852,7 @@ Object {
"name": "Reference1", "name": "Reference1",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"indent": Object { "indent": Object {
"end": null, "end": null,
"firstLineChars": null, "firstLineChars": null,
@ -101945,6 +101956,7 @@ Object {
"name": "Sentence indent1", "name": "Sentence indent1",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"alignment": "both", "alignment": "both",
"indent": Object { "indent": Object {
"end": null, "end": null,
@ -102039,6 +102051,7 @@ Object {
"name": "Bullet1", "name": "Bullet1",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"alignment": "both", "alignment": "both",
"indent": Object { "indent": Object {
"end": null, "end": null,
@ -102137,6 +102150,7 @@ Object {
"name": "List Number 3", "name": "List Number 3",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"keepNext": true, "keepNext": true,
"lineSpacing": Object { "lineSpacing": Object {
"before": 240, "before": 240,
@ -102226,6 +102240,7 @@ Object {
"name": "Note2", "name": "Note2",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"alignment": "both", "alignment": "both",
"lineSpacing": Object { "lineSpacing": Object {
"before": 120, "before": 120,
@ -102316,6 +102331,7 @@ Object {
"name": "Note3", "name": "Note3",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"alignment": "both", "alignment": "both",
"lineSpacing": Object { "lineSpacing": Object {
"before": 120, "before": 120,
@ -102405,6 +102421,7 @@ Object {
"name": "Bullet2", "name": "Bullet2",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"alignment": "both", "alignment": "both",
"indent": Object { "indent": Object {
"end": null, "end": null,
@ -102516,6 +102533,7 @@ Object {
"name": "Sentence indent3", "name": "Sentence indent3",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"alignment": "both", "alignment": "both",
"indent": Object { "indent": Object {
"end": null, "end": null,
@ -103562,6 +103580,7 @@ Object {
"name": "表紙の日付他", "name": "表紙の日付他",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"alignment": "right", "alignment": "right",
"keepLines": true, "keepLines": true,
"keepNext": true, "keepNext": true,
@ -104958,6 +104977,7 @@ Object {
"name": "macro", "name": "macro",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"runProperty": Object {}, "runProperty": Object {},
"tabs": Array [ "tabs": Array [
Object { Object {
@ -109644,6 +109664,7 @@ Object {
"name": "Default", "name": "Default",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"runProperty": Object {}, "runProperty": Object {},
"tabs": Array [], "tabs": Array [],
}, },
@ -109800,6 +109821,7 @@ Object {
"name": "Table Bullet1", "name": "Table Bullet1",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"lineSpacing": Object { "lineSpacing": Object {
"before": 20, "before": 20,
"line": 200, "line": 200,
@ -143496,6 +143518,7 @@ Object {
"name": "Char", "name": "Char",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"alignment": "left", "alignment": "left",
"indent": Object { "indent": Object {
"end": 0, "end": 0,
@ -143619,6 +143642,7 @@ Object {
"name": "Char1 Char Char Znak Znak1", "name": "Char1 Char Char Znak Znak1",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"indent": Object { "indent": Object {
"end": 0, "end": 0,
"firstLineChars": null, "firstLineChars": null,
@ -143736,6 +143760,7 @@ Object {
"name": "Char1", "name": "Char1",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"indent": Object { "indent": Object {
"end": 0, "end": 0,
"firstLineChars": null, "firstLineChars": null,
@ -167653,6 +167678,7 @@ Object {
"name": "Table Grid", "name": "Table Grid",
"next": null, "next": null,
"paragraphProperty": Object { "paragraphProperty": Object {
"adjustRightInd": 0,
"alignment": "both", "alignment": "both",
"lineSpacing": Object { "lineSpacing": Object {
"line": 340, "line": 340,