From e45e0431e99329de1660f68d102c6d7ae41b0c2a Mon Sep 17 00:00:00 2001 From: bokuweb Date: Fri, 18 Dec 2020 16:09:48 +0900 Subject: [PATCH] Fix comment rw (#215) * fix: comment * 0.0.125 --- docx-core/src/documents/document.rs | 58 +++++++++++++++++++++++------ docx-core/src/reader/document.rs | 17 +++++++++ docx-core/src/reader/paragraph.rs | 6 ++- docx-wasm/js/json/document.ts | 3 ++ docx-wasm/package.json | 2 +- docx-wasm/test/index.test.js | 4 +- 6 files changed, 76 insertions(+), 14 deletions(-) diff --git a/docx-core/src/documents/document.rs b/docx-core/src/documents/document.rs index d310cf7..5ba2e19 100644 --- a/docx-core/src/documents/document.rs +++ b/docx-core/src/documents/document.rs @@ -19,6 +19,8 @@ pub enum DocumentChild { Table(Table), BookmarkStart(BookmarkStart), BookmarkEnd(BookmarkEnd), + CommentStart(Box), + CommentEnd(CommentRangeEnd), } impl Serialize for DocumentChild { @@ -51,6 +53,18 @@ impl Serialize for DocumentChild { t.serialize_field("data", c)?; t.end() } + DocumentChild::CommentStart(ref r) => { + let mut t = serializer.serialize_struct("CommentRangeStart", 2)?; + t.serialize_field("type", "commentRangeStart")?; + t.serialize_field("data", r)?; + t.end() + } + DocumentChild::CommentEnd(ref r) => { + let mut t = serializer.serialize_struct("CommentRangeEnd", 2)?; + t.serialize_field("type", "commentRangeEnd")?; + t.serialize_field("data", r)?; + t.end() + } } } } @@ -98,6 +112,19 @@ impl Document { self } + pub fn add_comment_start(mut self, comment: Comment) -> Self { + self.children.push(DocumentChild::CommentStart(Box::new( + CommentRangeStart::new(comment), + ))); + self + } + + pub fn add_comment_end(mut self, id: usize) -> Self { + self.children + .push(DocumentChild::CommentEnd(CommentRangeEnd::new(id))); + self + } + pub fn page_size(mut self, size: PageSize) -> Self { self.section_property = self.section_property.page_size(size); self @@ -114,21 +141,30 @@ impl Document { } } +impl BuildXML for DocumentChild { + fn build(&self) -> Vec { + match self { + DocumentChild::Paragraph(v) => v.build(), + DocumentChild::Table(v) => v.build(), + DocumentChild::BookmarkStart(v) => v.build(), + DocumentChild::BookmarkEnd(v) => v.build(), + DocumentChild::CommentStart(v) => v.build(), + DocumentChild::CommentEnd(v) => v.build(), + } + } +} + impl BuildXML for Document { fn build(&self) -> Vec { - let mut b = XMLBuilder::new() + XMLBuilder::new() .declaration(Some(true)) .open_document() - .open_body(); - for c in &self.children { - match c { - DocumentChild::Paragraph(p) => b = b.add_child(p), - DocumentChild::Table(t) => b = b.add_child(t), - DocumentChild::BookmarkStart(s) => b = b.add_child(s), - DocumentChild::BookmarkEnd(e) => b = b.add_child(e), - } - } - b.add_child(&self.section_property).close().close().build() + .open_body() + .add_children(&self.children) + .add_child(&self.section_property) + .close() + .close() + .build() } } diff --git a/docx-core/src/reader/document.rs b/docx-core/src/reader/document.rs index bb7ecd8..5bbfe8d 100644 --- a/docx-core/src/reader/document.rs +++ b/docx-core/src/reader/document.rs @@ -38,6 +38,23 @@ impl FromXML for Document { doc = doc.add_bookmark_end(e.id); continue; } + XMLElement::CommentRangeStart => { + if let Some(id) = read(&attributes, "id") { + if let Ok(id) = usize::from_str(&id) { + let comment = Comment::new(id); + doc = doc.add_comment_start(comment); + } + } + continue; + } + XMLElement::CommentRangeEnd => { + if let Some(id) = read(&attributes, "id") { + if let Ok(id) = usize::from_str(&id) { + doc = doc.add_comment_end(id); + } + } + continue; + } XMLElement::SectionProperty => { let e = SectionProperty::read(&mut parser, &attributes)?; doc = doc.default_section_property(e); diff --git a/docx-core/src/reader/paragraph.rs b/docx-core/src/reader/paragraph.rs index c16d9f5..0d880ef 100644 --- a/docx-core/src/reader/paragraph.rs +++ b/docx-core/src/reader/paragraph.rs @@ -74,7 +74,11 @@ impl ElementReader for Paragraph { continue; } XMLElement::CommentRangeEnd => { - p = p.add_comment_end(usize::from_str(&attributes[0].value)?); + if let Some(id) = read(&attributes, "id") { + if let Ok(id) = usize::from_str(&id) { + p = p.add_comment_end(id); + } + } continue; } XMLElement::Indent => { diff --git a/docx-wasm/js/json/document.ts b/docx-wasm/js/json/document.ts index 7cc90b4..db5520c 100644 --- a/docx-wasm/js/json/document.ts +++ b/docx-wasm/js/json/document.ts @@ -1,10 +1,13 @@ import { ParagraphJSON, BookmarkStartJSON, BookmarkEndJSON } from "./paragraph"; import { TableJSON } from "./table"; import { SectionPropertyJSON } from "./section-property"; +import { CommentRangeStartJSON, CommentRangeEndJSON } from ".."; export type DocumentChildJSON = | ParagraphJSON | TableJSON + | CommentRangeStartJSON + | CommentRangeEndJSON | BookmarkStartJSON | BookmarkEndJSON; diff --git a/docx-wasm/package.json b/docx-wasm/package.json index 78d0ebe..494b88e 100644 --- a/docx-wasm/package.json +++ b/docx-wasm/package.json @@ -1,6 +1,6 @@ { "name": "docx-wasm", - "version": "0.0.123", + "version": "0.0.125", "main": "dist/node/index.js", "browser": "dist/web/index.js", "author": "bokuweb ", diff --git a/docx-wasm/test/index.test.js b/docx-wasm/test/index.test.js index 5768045..17cf3c4 100644 --- a/docx-wasm/test/index.test.js +++ b/docx-wasm/test/index.test.js @@ -22,7 +22,9 @@ describe("reader", () => { }); test("should read extended comments docx", () => { - const buf = readFileSync("../fixtures/extended_comments/extended_comments.docx"); + const buf = readFileSync( + "../fixtures/extended_comments/extended_comments.docx" + ); const json = w.readDocx(buf); expect(json).toMatchSnapshot(); });