fix: read comment in ins (#222)

* fix: read comment in ins

* 0.0.135

* 0.0.136
main
bokuweb 2021-01-06 20:31:53 +09:00 committed by GitHub
parent 856b2b6660
commit 17c669bdde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 87 additions and 13 deletions

View File

@ -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

View File

@ -10,6 +10,8 @@ use crate::xml_builder::*;
pub enum InsertChild {
Run(Box<Run>),
Delete(Delete),
CommentStart(Box<CommentRangeStart>),
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<String>) -> Insert {
self.author = author.into();
self

View File

@ -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);
}
}
}
}
}
}
}

View File

@ -11,18 +11,33 @@ impl ElementReader for Insert {
r: &mut EventReader<R>,
attrs: &[OwnedAttribute],
) -> Result<Self, ReaderError> {
let mut children: Vec<InsertChild> = 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);
}
XMLElement::Delete => {
children.push(InsertChild::Delete(Delete::read(r, attrs)?))
}
continue;
}
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);
}
}

View File

@ -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;
};

View File

@ -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 <bokuweb12@gmail.com>",