Fix comment rw (#215)

* fix: comment

* 0.0.125
main
bokuweb 2020-12-18 16:09:48 +09:00 committed by GitHub
parent d4b9e0b5e2
commit e45e0431e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 76 additions and 14 deletions

View File

@ -19,6 +19,8 @@ pub enum DocumentChild {
Table(Table), Table(Table),
BookmarkStart(BookmarkStart), BookmarkStart(BookmarkStart),
BookmarkEnd(BookmarkEnd), BookmarkEnd(BookmarkEnd),
CommentStart(Box<CommentRangeStart>),
CommentEnd(CommentRangeEnd),
} }
impl Serialize for DocumentChild { impl Serialize for DocumentChild {
@ -51,6 +53,18 @@ impl Serialize for DocumentChild {
t.serialize_field("data", c)?; t.serialize_field("data", c)?;
t.end() 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 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 { pub fn page_size(mut self, size: PageSize) -> Self {
self.section_property = self.section_property.page_size(size); self.section_property = self.section_property.page_size(size);
self self
@ -114,21 +141,30 @@ impl Document {
} }
} }
impl BuildXML for DocumentChild {
fn build(&self) -> Vec<u8> {
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 { impl BuildXML for Document {
fn build(&self) -> Vec<u8> { fn build(&self) -> Vec<u8> {
let mut b = XMLBuilder::new() XMLBuilder::new()
.declaration(Some(true)) .declaration(Some(true))
.open_document() .open_document()
.open_body(); .open_body()
for c in &self.children { .add_children(&self.children)
match c { .add_child(&self.section_property)
DocumentChild::Paragraph(p) => b = b.add_child(p), .close()
DocumentChild::Table(t) => b = b.add_child(t), .close()
DocumentChild::BookmarkStart(s) => b = b.add_child(s), .build()
DocumentChild::BookmarkEnd(e) => b = b.add_child(e),
}
}
b.add_child(&self.section_property).close().close().build()
} }
} }

View File

@ -38,6 +38,23 @@ impl FromXML for Document {
doc = doc.add_bookmark_end(e.id); doc = doc.add_bookmark_end(e.id);
continue; 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 => { XMLElement::SectionProperty => {
let e = SectionProperty::read(&mut parser, &attributes)?; let e = SectionProperty::read(&mut parser, &attributes)?;
doc = doc.default_section_property(e); doc = doc.default_section_property(e);

View File

@ -74,7 +74,11 @@ impl ElementReader for Paragraph {
continue; continue;
} }
XMLElement::CommentRangeEnd => { 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; continue;
} }
XMLElement::Indent => { XMLElement::Indent => {

View File

@ -1,10 +1,13 @@
import { ParagraphJSON, BookmarkStartJSON, BookmarkEndJSON } from "./paragraph"; import { ParagraphJSON, BookmarkStartJSON, BookmarkEndJSON } from "./paragraph";
import { TableJSON } from "./table"; import { TableJSON } from "./table";
import { SectionPropertyJSON } from "./section-property"; import { SectionPropertyJSON } from "./section-property";
import { CommentRangeStartJSON, CommentRangeEndJSON } from "..";
export type DocumentChildJSON = export type DocumentChildJSON =
| ParagraphJSON | ParagraphJSON
| TableJSON | TableJSON
| CommentRangeStartJSON
| CommentRangeEndJSON
| BookmarkStartJSON | BookmarkStartJSON
| BookmarkEndJSON; | BookmarkEndJSON;

View File

@ -1,6 +1,6 @@
{ {
"name": "docx-wasm", "name": "docx-wasm",
"version": "0.0.123", "version": "0.0.125",
"main": "dist/node/index.js", "main": "dist/node/index.js",
"browser": "dist/web/index.js", "browser": "dist/web/index.js",
"author": "bokuweb <bokuweb12@gmail.com>", "author": "bokuweb <bokuweb12@gmail.com>",

View File

@ -22,7 +22,9 @@ describe("reader", () => {
}); });
test("should read extended comments docx", () => { 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); const json = w.readDocx(buf);
expect(json).toMatchSnapshot(); expect(json).toMatchSnapshot();
}); });