-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Support table position * fix: table pr * fix: wasm for table position * fix: reader for position property * fix: snaps * fix * fix
- Loading branch information
Showing
23 changed files
with
438 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
docx-core/src/documents/elements/table_position_property.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use serde::Serialize; | ||
|
||
use crate::documents::BuildXML; | ||
use crate::xml_builder::*; | ||
|
||
/// https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.tablepositionproperties?view=openxml-3.0.1 | ||
#[derive(Debug, Clone, PartialEq, Serialize, Default)] | ||
#[serde(rename_all = "camelCase")] | ||
#[cfg_attr(feature = "wasm", derive(ts_rs::TS), ts(export))] | ||
pub struct TablePositionProperty { | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub left_from_text: Option<i32>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub right_from_text: Option<i32>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub vertical_anchor: Option<String>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub horizontal_anchor: Option<String>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub position_x_alignment: Option<String>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub position_y_alignment: Option<String>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub position_x: Option<i32>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub position_y: Option<i32>, | ||
} | ||
|
||
impl TablePositionProperty { | ||
pub fn new() -> TablePositionProperty { | ||
Default::default() | ||
} | ||
|
||
pub fn left_from_text(mut self, v: i32) -> Self { | ||
self.left_from_text = Some(v); | ||
self | ||
} | ||
|
||
pub fn right_from_text(mut self, v: i32) -> Self { | ||
self.right_from_text = Some(v); | ||
self | ||
} | ||
|
||
pub fn vertical_anchor(mut self, v: impl Into<String>) -> Self { | ||
self.vertical_anchor = Some(v.into()); | ||
self | ||
} | ||
|
||
pub fn horizontal_anchor(mut self, v: impl Into<String>) -> Self { | ||
self.horizontal_anchor = Some(v.into()); | ||
self | ||
} | ||
|
||
pub fn position_x_alignment(mut self, v: impl Into<String>) -> Self { | ||
self.position_x_alignment = Some(v.into()); | ||
self | ||
} | ||
|
||
pub fn position_y_alignment(mut self, v: impl Into<String>) -> Self { | ||
self.position_y_alignment = Some(v.into()); | ||
self | ||
} | ||
|
||
pub fn position_x(mut self, v: i32) -> Self { | ||
self.position_x = Some(v); | ||
self | ||
} | ||
|
||
pub fn position_y(mut self, v: i32) -> Self { | ||
self.position_y = Some(v); | ||
self | ||
} | ||
} | ||
|
||
impl BuildXML for TablePositionProperty { | ||
fn build(&self) -> Vec<u8> { | ||
XMLBuilder::new().table_position_property(self).build() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use super::*; | ||
#[cfg(test)] | ||
use pretty_assertions::assert_eq; | ||
use std::str; | ||
|
||
#[test] | ||
fn test_default() { | ||
let b = TablePositionProperty::new().build(); | ||
assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:tblpPr />"#); | ||
} | ||
|
||
#[test] | ||
fn test_some_attrs() { | ||
let b = TablePositionProperty::new() | ||
.left_from_text(142) | ||
.right_from_text(142) | ||
.vertical_anchor("text") | ||
.horizontal_anchor("margin") | ||
.position_x_alignment("right") | ||
.position_y(511) | ||
.build(); | ||
assert_eq!( | ||
str::from_utf8(&b).unwrap(), | ||
r#"<w:tblpPr w:leftFromText="142" w:rightFromText="142" w:vertAnchor="text" w:horzAnchor="margin" w:tblpXSpec="right" w:tblpY="511" />"# | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::io::Read; | ||
use std::str::FromStr; | ||
|
||
use xml::attribute::OwnedAttribute; | ||
use xml::reader::EventReader; | ||
|
||
use super::*; | ||
|
||
impl ElementReader for TablePositionProperty { | ||
fn read<R: Read>( | ||
_r: &mut EventReader<R>, | ||
attrs: &[OwnedAttribute], | ||
) -> Result<Self, ReaderError> { | ||
let mut property = TablePositionProperty::new(); | ||
for a in attrs { | ||
let local_name = &a.name.local_name; | ||
match local_name.as_str() { | ||
"leftFromText" => { | ||
if let Ok(v) = i32::from_str(&a.value) { | ||
property = property.left_from_text(v); | ||
} | ||
} | ||
"rightFromText" => { | ||
if let Ok(v) = i32::from_str(&a.value) { | ||
property = property.right_from_text(v); | ||
} | ||
} | ||
"vertAnchor" => { | ||
property = property.vertical_anchor(a.value.clone()); | ||
} | ||
"horzAnchor" => { | ||
property = property.horizontal_anchor(a.value.clone()); | ||
} | ||
"tblpXSpec" => { | ||
property = property.position_x_alignment(a.value.clone()); | ||
} | ||
"tblpYSpec" => { | ||
property = property.position_y_alignment(a.value.clone()); | ||
} | ||
"tblpX" => { | ||
if let Ok(v) = i32::from_str(&a.value) { | ||
property = property.position_x(v); | ||
} | ||
} | ||
"tblpY" => { | ||
if let Ok(v) = i32::from_str(&a.value) { | ||
property = property.position_y(v); | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
Ok(property) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
docx-core/tests/snapshots/lib__reader__read_table_merged_libre_office.snap
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
docx-core/tests/snapshots/reader__read_table_merged_libre_office.snap
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
export interface TablePositionProperty { leftFromText?: number, rightFromText?: number, verticalAnchor?: string, horizontalAnchor?: string, positionXAlignment?: string, positionYAlignment?: string, positionX?: number, positionY?: number, } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.