2019-12-11 07:12:22 +02:00
|
|
|
use super::*;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
#[derive(Debug)]
|
2020-01-24 12:44:43 +02:00
|
|
|
pub struct Docx(docx_rs::Docx);
|
2019-12-11 07:12:22 +02:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub fn createDocx() -> Docx {
|
2020-01-24 12:44:43 +02:00
|
|
|
Docx(docx_rs::Docx::new())
|
2019-12-11 07:12:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl Docx {
|
|
|
|
pub fn add_paragraph(mut self, p: Paragraph) -> Self {
|
|
|
|
self.0 = self.0.add_paragraph(p.take());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-09-02 12:23:53 +03:00
|
|
|
pub fn add_bookmark_start(mut self, id: usize, name: &str) -> Self {
|
|
|
|
self.0 = self.0.add_bookmark_start(id, name);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_bookmark_end(mut self, id: usize) -> Docx {
|
|
|
|
self.0 = self.0.add_bookmark_end(id);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:12:22 +02:00
|
|
|
pub fn add_table(mut self, t: Table) -> Docx {
|
|
|
|
self.0.document = self.0.document.add_table(t.take());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-02-11 17:21:26 +02:00
|
|
|
pub fn add_abstract_numbering(mut self, num: AbstractNumbering) -> Docx {
|
|
|
|
self.0.numberings = self.0.numberings.add_abstract_numbering(num.take());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:12:22 +02:00
|
|
|
pub fn add_numbering(mut self, num: Numbering) -> Docx {
|
|
|
|
self.0.numberings = self.0.numberings.add_numbering(num.take());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-08-19 18:53:35 +03:00
|
|
|
pub fn doc_id(mut self, id: &str) -> Docx {
|
|
|
|
self.0 = self.0.doc_id(id);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-10-09 14:30:55 +03:00
|
|
|
pub fn page_size(mut self, w: u32, h: u32) -> Docx {
|
|
|
|
self.0 = self.0.page_size(w, h);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn page_margin(mut self, margin: docx_rs::PageMargin) -> Docx {
|
|
|
|
self.0 = self.0.page_margin(margin);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:51:45 +03:00
|
|
|
pub fn build(&mut self, has_numberings: bool) -> Result<Vec<u8>, JsValue> {
|
2019-12-11 07:12:22 +02:00
|
|
|
let buf = Vec::new();
|
|
|
|
let mut cur = std::io::Cursor::new(buf);
|
2020-05-15 09:51:45 +03:00
|
|
|
if has_numberings {
|
|
|
|
self.0.document_rels.has_numberings = true;
|
|
|
|
}
|
2019-12-11 07:12:22 +02:00
|
|
|
let res = self.0.build().pack(&mut cur);
|
|
|
|
if res.is_err() {
|
|
|
|
return Err(format!("{:?}", res).into());
|
|
|
|
}
|
|
|
|
Ok(cur.into_inner())
|
|
|
|
}
|
|
|
|
}
|