From 17c669bdde44da9b8c338550b84c13ae74631aa4 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Wed, 6 Jan 2021 20:31:53 +0900 Subject: [PATCH] fix: read comment in ins (#222) * fix: read comment in ins * 0.0.135 * 0.0.136 --- .github/workflows/ci.yml | 2 +- docx-core/src/documents/elements/insert.rs | 29 ++++++++++++++++++++ docx-core/src/documents/mod.rs | 29 ++++++++++++++++++++ docx-core/src/reader/insert.rs | 31 +++++++++++++++------- docx-wasm/js/json/paragraph.ts | 7 ++++- docx-wasm/package.json | 2 +- 6 files changed, 87 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index df53929..3c38e9b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -88,7 +88,7 @@ jobs: - uses: actions-rs/toolchain@v1 with: profile: minimal - toolchain: stable + toolchain: 1.45.1 override: true - run: rustup component add clippy - uses: actions-rs/cargo@v1 diff --git a/docx-core/src/documents/elements/insert.rs b/docx-core/src/documents/elements/insert.rs index 7d16e0b..6932f5f 100644 --- a/docx-core/src/documents/elements/insert.rs +++ b/docx-core/src/documents/elements/insert.rs @@ -10,6 +10,8 @@ use crate::xml_builder::*; pub enum InsertChild { Run(Box), Delete(Delete), + CommentStart(Box), + CommentEnd(CommentRangeEnd), } impl BuildXML for InsertChild { @@ -17,6 +19,8 @@ impl BuildXML for InsertChild { match self { InsertChild::Run(v) => v.build(), InsertChild::Delete(v) => v.build(), + InsertChild::CommentStart(v) => v.build(), + InsertChild::CommentEnd(v) => v.build(), } } } @@ -39,6 +43,18 @@ impl Serialize for InsertChild { t.serialize_field("data", r)?; t.end() } + InsertChild::CommentStart(ref r) => { + let mut t = serializer.serialize_struct("CommentRangeStart", 2)?; + t.serialize_field("type", "commentRangeStart")?; + t.serialize_field("data", r)?; + t.end() + } + InsertChild::CommentEnd(ref r) => { + let mut t = serializer.serialize_struct("CommentRangeEnd", 2)?; + t.serialize_field("type", "commentRangeEnd")?; + t.serialize_field("data", r)?; + t.end() + } } } } @@ -96,6 +112,19 @@ impl Insert { self } + pub fn add_comment_start(mut self, comment: Comment) -> Self { + self.children.push(InsertChild::CommentStart(Box::new( + CommentRangeStart::new(comment), + ))); + self + } + + pub fn add_comment_end(mut self, id: usize) -> Self { + self.children + .push(InsertChild::CommentEnd(CommentRangeEnd::new(id))); + self + } + pub fn author(mut self, author: impl Into) -> Insert { self.author = author.into(); self diff --git a/docx-core/src/documents/mod.rs b/docx-core/src/documents/mod.rs index 4f72587..b8235c0 100644 --- a/docx-core/src/documents/mod.rs +++ b/docx-core/src/documents/mod.rs @@ -431,6 +431,19 @@ impl Docx { c.as_mut().comment(comment); } } + if let ParagraphChild::Insert(ref mut insert) = child { + for child in &mut insert.children { + if let InsertChild::CommentStart(ref mut c) = child { + let comment_id = c.get_id(); + if let Some(comment) = + comments.iter().find(|c| c.id() == comment_id) + { + let comment = comment.clone(); + c.as_mut().comment(comment); + } + } + } + } } } DocumentChild::Table(table) => { @@ -449,6 +462,22 @@ impl Docx { c.as_mut().comment(comment); } } + if let ParagraphChild::Insert(ref mut insert) = child { + for child in &mut insert.children { + if let InsertChild::CommentStart(ref mut c) = + child + { + let comment_id = c.get_id(); + if let Some(comment) = comments + .iter() + .find(|c| c.id() == comment_id) + { + let comment = comment.clone(); + c.as_mut().comment(comment); + } + } + } + } } } } diff --git a/docx-core/src/reader/insert.rs b/docx-core/src/reader/insert.rs index 62c8b72..dbb13c7 100644 --- a/docx-core/src/reader/insert.rs +++ b/docx-core/src/reader/insert.rs @@ -11,18 +11,33 @@ impl ElementReader for Insert { r: &mut EventReader, attrs: &[OwnedAttribute], ) -> Result { - let mut children: Vec = vec![]; + let mut ins = Insert::new_with_empty(); loop { let e = r.next(); match e { - Ok(XmlEvent::StartElement { name, .. }) => { + Ok(XmlEvent::StartElement { + name, attributes, .. + }) => { let e = XMLElement::from_str(&name.local_name).unwrap(); match e { - XMLElement::Run => { - children.push(InsertChild::Run(Box::new(Run::read(r, attrs)?))) + XMLElement::Run => ins = ins.add_run(Run::read(r, attrs)?), + XMLElement::Delete => ins = ins.add_delete(Delete::read(r, attrs)?), + XMLElement::CommentRangeStart => { + if let Some(id) = read(&attributes, "id") { + if let Ok(id) = usize::from_str(&id) { + let comment = Comment::new(id); + ins = ins.add_comment_start(comment); + } + } + continue; } - XMLElement::Delete => { - children.push(InsertChild::Delete(Delete::read(r, attrs)?)) + XMLElement::CommentRangeEnd => { + if let Some(id) = read(&attributes, "id") { + if let Ok(id) = usize::from_str(&id) { + ins = ins.add_comment_end(id); + } + } + continue; } _ => {} } @@ -30,7 +45,6 @@ impl ElementReader for Insert { Ok(XmlEvent::EndElement { name, .. }) => { let e = XMLElement::from_str(&name.local_name).unwrap(); if e == XMLElement::Insert { - let mut ins = Insert::new_with_empty(); for attr in attrs { let local_name = &attr.name.local_name; if local_name == "author" { @@ -39,9 +53,6 @@ impl ElementReader for Insert { ins = ins.date(&attr.value); } } - for c in children.into_iter() { - ins = ins.add_child(c); - } return Ok(ins); } } diff --git a/docx-wasm/js/json/paragraph.ts b/docx-wasm/js/json/paragraph.ts index 5793aa9..a4596e0 100644 --- a/docx-wasm/js/json/paragraph.ts +++ b/docx-wasm/js/json/paragraph.ts @@ -37,7 +37,12 @@ export type ParagraphJSON = { export type InsertJSON = { type: "insert"; data: { - children: (DeleteJSON | RunJSON)[]; + children: ( + | DeleteJSON + | RunJSON + | CommentRangeStartJSON + | CommentRangeEndJSON + )[]; author: string; data: string; }; diff --git a/docx-wasm/package.json b/docx-wasm/package.json index bb715b2..99dd708 100644 --- a/docx-wasm/package.json +++ b/docx-wasm/package.json @@ -1,6 +1,6 @@ { "name": "docx-wasm", - "version": "0.0.134", + "version": "0.0.136", "main": "dist/node/index.js", "browser": "dist/web/index.js", "author": "bokuweb ",