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),
BookmarkStart(BookmarkStart),
BookmarkEnd(BookmarkEnd),
CommentStart(Box<CommentRangeStart>),
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<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 {
fn build(&self) -> Vec<u8> {
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()
}
}

View File

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

View File

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

View File

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

View File

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

View File

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