fix: support comment in nested table (#495)

* fix: support comment in nested table

* 0.0.268

Co-authored-by: bokuweb <bokuweb@bokuwebnombp.lan>
main
bokuweb 2022-06-14 18:36:54 +09:00 committed by GitHub
parent fc2760c31a
commit 604bdb85f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 137 additions and 149 deletions

View File

@ -592,31 +592,7 @@ impl Docx {
let para_id = child.id.clone(); let para_id = child.id.clone();
comment_map.insert(comment_id, para_id.clone()); comment_map.insert(comment_id, para_id.clone());
} }
// TODO: Support table // TODO: Support table in comment
}
}
fn push_comment_and_comment_extended(
&self,
comments: &mut Vec<Comment>,
comments_extended: &mut Vec<CommentExtended>,
comment_map: &HashMap<usize, String>,
c: &CommentRangeStart,
) {
let comment = c.get_comment();
for child in comment.children {
if let CommentChild::Paragraph(child) = child {
let para_id = child.id.clone();
comments.push(c.get_comment());
let comment_extended = CommentExtended::new(para_id);
if let Some(parent_comment_id) = comment.parent_comment_id {
let parent_para_id = comment_map.get(&parent_comment_id).unwrap().clone();
comments_extended.push(comment_extended.parent_paragraph_id(parent_para_id));
} else {
comments_extended.push(comment_extended);
}
}
// TODO: Support table
} }
} }
@ -643,34 +619,12 @@ impl Docx {
} }
} }
DocumentChild::Table(table) => { DocumentChild::Table(table) => {
for TableChild::TableRow(row) in &table.rows { collect_comments_in_table(
for TableRowChild::TableCell(cell) in &row.cells { table,
for content in &cell.children { &mut comments,
match content { &mut comments_extended,
TableCellContent::Paragraph(paragraph) => { &mut comment_map,
for child in &paragraph.children { );
if let ParagraphChild::CommentStart(c) = child {
self.insert_comment_to_map(&mut comment_map, c);
}
if let ParagraphChild::Hyperlink(h) = child {
for child in &h.children {
if let ParagraphChild::CommentStart(c) = child {
self.insert_comment_to_map(
&mut comment_map,
c,
);
}
}
}
}
}
TableCellContent::Table(_) => {
// TODO: correct comment
}
}
}
}
}
} }
_ => {} _ => {}
} }
@ -681,7 +635,7 @@ impl Docx {
DocumentChild::Paragraph(paragraph) => { DocumentChild::Paragraph(paragraph) => {
for child in &paragraph.children { for child in &paragraph.children {
if let ParagraphChild::CommentStart(c) = child { if let ParagraphChild::CommentStart(c) = child {
self.push_comment_and_comment_extended( push_comment_and_comment_extended(
&mut comments, &mut comments,
&mut comments_extended, &mut comments_extended,
&comment_map, &comment_map,
@ -691,7 +645,7 @@ impl Docx {
if let ParagraphChild::Hyperlink(h) = child { if let ParagraphChild::Hyperlink(h) = child {
for child in &h.children { for child in &h.children {
if let ParagraphChild::CommentStart(c) = child { if let ParagraphChild::CommentStart(c) = child {
self.push_comment_and_comment_extended( push_comment_and_comment_extended(
&mut comments, &mut comments,
&mut comments_extended, &mut comments_extended,
&comment_map, &comment_map,
@ -703,41 +657,12 @@ impl Docx {
} }
} }
DocumentChild::Table(table) => { DocumentChild::Table(table) => {
for TableChild::TableRow(row) in &table.rows { collect_comments_in_table(
for TableRowChild::TableCell(cell) in &row.cells { table,
for content in &cell.children { &mut comments,
match content { &mut comments_extended,
TableCellContent::Paragraph(paragraph) => { &mut comment_map,
for child in &paragraph.children { );
if let ParagraphChild::CommentStart(c) = child {
self.push_comment_and_comment_extended(
&mut comments,
&mut comments_extended,
&comment_map,
c,
);
}
if let ParagraphChild::Hyperlink(h) = child {
for child in &h.children {
if let ParagraphChild::CommentStart(c) = child {
self.push_comment_and_comment_extended(
&mut comments,
&mut comments_extended,
&comment_map,
c,
);
}
}
}
}
}
TableCellContent::Table(_) => {
// TODO: correct comment
}
}
}
}
}
} }
_ => {} _ => {}
} }
@ -755,6 +680,7 @@ impl Docx {
} }
// Traverse and clone comments from document and add to comments node. // Traverse and clone comments from document and add to comments node.
// reader only
pub(crate) fn store_comments(&mut self, comments: &[Comment]) { pub(crate) fn store_comments(&mut self, comments: &[Comment]) {
for child in &mut self.document.children { for child in &mut self.document.children {
match child { match child {
@ -795,64 +721,7 @@ impl Docx {
} }
} }
} }
DocumentChild::Table(table) => { DocumentChild::Table(table) => store_comments_in_table(table, comments),
for TableChild::TableRow(row) in &mut table.rows {
for TableRowChild::TableCell(cell) in &mut row.cells {
for content in &mut cell.children {
match content {
TableCellContent::Paragraph(paragraph) => {
for child in &mut paragraph.children {
if let ParagraphChild::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);
}
}
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);
}
}
}
}
if let ParagraphChild::Delete(ref mut delete) = child {
for child in &mut delete.children {
if let DeleteChild::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);
}
}
}
}
}
}
TableCellContent::Table(_) => {
// TODO: support comment
}
}
}
}
}
}
_ => {} _ => {}
} }
} }
@ -903,6 +772,102 @@ impl Docx {
} }
} }
fn collect_comments_in_table(
table: &Table,
comments: &mut Vec<Comment>,
comments_extended: &mut Vec<CommentExtended>,
comment_map: &mut HashMap<usize, String>,
) {
for TableChild::TableRow(row) in &table.rows {
for TableRowChild::TableCell(cell) in &row.cells {
for content in &cell.children {
match content {
TableCellContent::Paragraph(paragraph) => {
for child in &paragraph.children {
if let ParagraphChild::CommentStart(c) = child {
push_comment_and_comment_extended(
comments,
comments_extended,
comment_map,
c,
);
}
if let ParagraphChild::Hyperlink(h) = child {
for child in &h.children {
if let ParagraphChild::CommentStart(c) = child {
push_comment_and_comment_extended(
comments,
comments_extended,
comment_map,
c,
);
}
}
}
}
}
TableCellContent::Table(table) => {
collect_comments_in_table(table, comments, comments_extended, comment_map)
}
}
}
}
}
}
fn store_comments_in_table(table: &mut Table, comments: &[Comment]) {
for TableChild::TableRow(row) in &mut table.rows {
for TableRowChild::TableCell(cell) in &mut row.cells {
for content in &mut cell.children {
match content {
TableCellContent::Paragraph(paragraph) => {
for child in &mut paragraph.children {
if let ParagraphChild::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);
}
}
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);
}
}
}
}
if let ParagraphChild::Delete(ref mut delete) = child {
for child in &mut delete.children {
if let DeleteChild::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);
}
}
}
}
}
}
TableCellContent::Table(ref mut table) => {
store_comments_in_table(table, comments);
}
}
}
}
}
}
fn collect_images_from_paragraph( fn collect_images_from_paragraph(
paragraph: &mut Paragraph, paragraph: &mut Paragraph,
images: &mut Vec<(String, String)>, images: &mut Vec<(String, String)>,
@ -985,6 +950,29 @@ fn collect_images_from_paragraph(
} }
} }
fn push_comment_and_comment_extended(
comments: &mut Vec<Comment>,
comments_extended: &mut Vec<CommentExtended>,
comment_map: &HashMap<usize, String>,
c: &CommentRangeStart,
) {
let comment = c.get_comment();
for child in comment.children {
if let CommentChild::Paragraph(child) = child {
let para_id = child.id.clone();
comments.push(c.get_comment());
let comment_extended = CommentExtended::new(para_id);
if let Some(parent_comment_id) = comment.parent_comment_id {
let parent_para_id = comment_map.get(&parent_comment_id).unwrap().clone();
comments_extended.push(comment_extended.parent_paragraph_id(parent_para_id));
} else {
comments_extended.push(comment_extended);
}
}
// TODO: Support table in comment
}
}
fn collect_images_from_table( fn collect_images_from_table(
table: &mut Table, table: &mut Table,
images: &mut Vec<(String, String)>, images: &mut Vec<(String, String)>,

View File

@ -1,6 +1,6 @@
{ {
"name": "docx-wasm", "name": "docx-wasm",
"version": "0.0.267", "version": "0.0.268",
"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>",