From bf385f752d7bd31bea921d4f56e1c19e7dedb879 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Wed, 17 Jul 2024 16:28:29 +0900 Subject: [PATCH] faet: Support snap to grid (#742) --- docx-core/src/documents/elements/paragraph.rs | 5 +++++ .../src/documents/elements/paragraph_property.rs | 13 +++++++++++++ docx-core/src/reader/paragraph_property.rs | 5 +++++ docx-core/src/reader/xml_element.rs | 2 ++ docx-core/src/xml_builder/elements.rs | 1 + docx-wasm/js/paragraph-property.ts | 5 +++++ docx-wasm/src/paragraph.rs | 5 +++++ docx-wasm/src/style.rs | 5 +++++ 8 files changed, 41 insertions(+) diff --git a/docx-core/src/documents/elements/paragraph.rs b/docx-core/src/documents/elements/paragraph.rs index 00d5b12..71d744d 100644 --- a/docx-core/src/documents/elements/paragraph.rs +++ b/docx-core/src/documents/elements/paragraph.rs @@ -226,6 +226,11 @@ impl Paragraph { self } + pub fn snap_to_grid(mut self, v: bool) -> Self { + self.property = self.property.snap_to_grid(v); + self + } + pub fn keep_next(mut self, v: bool) -> Self { self.property = self.property.keep_next(v); self diff --git a/docx-core/src/documents/elements/paragraph_property.rs b/docx-core/src/documents/elements/paragraph_property.rs index e208027..519368e 100644 --- a/docx-core/src/documents/elements/paragraph_property.rs +++ b/docx-core/src/documents/elements/paragraph_property.rs @@ -43,6 +43,8 @@ pub struct ParagraphProperty { pub text_alignment: Option, #[serde(skip_serializing_if = "Option::is_none")] pub adjust_right_ind: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub snap_to_grid: Option, // read only #[serde(skip_serializing_if = "Option::is_none")] pub(crate) div_id: Option, @@ -99,6 +101,11 @@ impl ParagraphProperty { self } + pub fn snap_to_grid(mut self, v: bool) -> Self { + self.snap_to_grid = Some(v); + self + } + pub fn keep_next(mut self, v: bool) -> Self { self.keep_next = Some(v); self @@ -210,6 +217,12 @@ fn inner_build(p: &ParagraphProperty) -> Vec { .add_optional_child(&p.text_alignment) .add_optional_child(&p.adjust_right_ind); + if let Some(v) = p.snap_to_grid { + if v { + b = b.snap_to_grid() + } + } + if let Some(v) = p.keep_next { if v { b = b.keep_next() diff --git a/docx-core/src/reader/paragraph_property.rs b/docx-core/src/reader/paragraph_property.rs index ffc4b60..11d6a95 100644 --- a/docx-core/src/reader/paragraph_property.rs +++ b/docx-core/src/reader/paragraph_property.rs @@ -94,6 +94,11 @@ impl ElementReader for ParagraphProperty { } continue; } + XMLElement::SnapToGrid => { + if read_bool(&attributes) { + p.snap_to_grid = Some(true); + } + } XMLElement::KeepNext => { if read_bool(&attributes) { p.keep_next = Some(true); diff --git a/docx-core/src/reader/xml_element.rs b/docx-core/src/reader/xml_element.rs index e6ae6b0..ddf100a 100644 --- a/docx-core/src/reader/xml_element.rs +++ b/docx-core/src/reader/xml_element.rs @@ -52,6 +52,7 @@ pub enum XMLElement { Justification, OutlineLvl, Insert, + SnapToGrid, KeepNext, KeepLines, PageBreakBefore, @@ -400,6 +401,7 @@ impl FromStr for XMLElement { "marBottom" => Ok(XMLElement::MarginBottom), "sym" => Ok(XMLElement::Sym), "webSettings" => Ok(XMLElement::WebSettings), + "snapToGrid" => Ok(XMLElement::SnapToGrid), "keepNext" => Ok(XMLElement::KeepNext), "keepLines" => Ok(XMLElement::KeepLines), "pageBreakBefore" => Ok(XMLElement::PageBreakBefore), diff --git a/docx-core/src/xml_builder/elements.rs b/docx-core/src/xml_builder/elements.rs index 2de6100..3ea4aa9 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -167,6 +167,7 @@ impl XMLBuilder { // i.e. closed_with_usize!(sz_cs, "w:szCs"); closed_with_isize!(adjust_right_ind, "w:adjustRightInd"); + closed!(snap_to_grid, "w:snapToGrid"); closed_with_str!(text_alignment, "w:textAlignment"); closed!(field_character, "w:fldChar", "w:fldCharType", "w:dirty"); diff --git a/docx-wasm/js/paragraph-property.ts b/docx-wasm/js/paragraph-property.ts index d24aa92..8b52a83 100644 --- a/docx-wasm/js/paragraph-property.ts +++ b/docx-wasm/js/paragraph-property.ts @@ -82,6 +82,7 @@ export type ParagraphProperty = { widowControl: boolean; paragraphPropertyChange?: ParagraphPropertyChange; outlineLvl?: number | null; + snapToGrid?: boolean; adjustRightInd?: number; tabs?: Tab[]; frameProperty?: FrameProperty; @@ -334,6 +335,10 @@ export const setParagraphProperty = ( target = target.keep_lines(true) as T; } + if (property.snapToGrid != null) { + target = target.snap_to_grid(!!property.snapToGrid) as T; + } + if (property.keepNext) { target = target.keep_next(true) as T; } diff --git a/docx-wasm/src/paragraph.rs b/docx-wasm/src/paragraph.rs index 426f3d6..0dc265e 100644 --- a/docx-wasm/src/paragraph.rs +++ b/docx-wasm/src/paragraph.rs @@ -208,6 +208,11 @@ impl Paragraph { self } + pub fn snap_to_grid(mut self, v: bool) -> Self { + self.0 = self.0.snap_to_grid(v); + self + } + pub fn keep_lines(mut self, v: bool) -> Self { self.0 = self.0.keep_lines(v); self diff --git a/docx-wasm/src/style.rs b/docx-wasm/src/style.rs index 199893e..b4d94ec 100644 --- a/docx-wasm/src/style.rs +++ b/docx-wasm/src/style.rs @@ -159,6 +159,11 @@ impl Style { self } + pub fn snap_to_grid(mut self, v: bool) -> Self { + self.0.paragraph_property = self.0.paragraph_property.snap_to_grid(v); + self + } + pub fn keep_next(mut self, v: bool) -> Self { self.0.paragraph_property = self.0.paragraph_property.keep_next(v); self