2019-12-11 07:12:22 +02:00
|
|
|
use super::*;
|
2023-06-23 07:41:36 +03:00
|
|
|
use docx_rs::CharacterSpacingValues;
|
2019-12-11 07:12:22 +02:00
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
2022-07-22 05:46:07 +03:00
|
|
|
extern crate console_error_panic_hook;
|
|
|
|
|
2019-12-11 07:12:22 +02:00
|
|
|
#[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 {
|
2022-07-22 05:46:07 +03:00
|
|
|
use std::panic;
|
|
|
|
panic::set_hook(Box::new(console_error_panic_hook::hook));
|
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
|
|
|
|
}
|
|
|
|
|
2022-01-06 09:13:45 +02:00
|
|
|
pub fn add_table_of_contents(mut self, t: TableOfContents) -> Self {
|
|
|
|
self.0 = self.0.add_table_of_contents(t.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
|
|
|
|
}
|
|
|
|
|
2021-02-25 03:27:59 +02:00
|
|
|
pub fn created_at(mut self, date: &str) -> Self {
|
|
|
|
self.0.doc_props = self.0.doc_props.created_at(date);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn updated_at(mut self, date: &str) -> Self {
|
|
|
|
self.0.doc_props = self.0.doc_props.updated_at(date);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-06-23 12:39:01 +03:00
|
|
|
pub fn custom_property(mut self, name: &str, item: &str) -> Self {
|
|
|
|
self.0.doc_props = self.0.doc_props.custom_property(name, item);
|
|
|
|
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-12-15 08:38:17 +02:00
|
|
|
pub fn add_doc_var(mut self, name: &str, val: &str) -> Docx {
|
|
|
|
self.0 = self.0.add_doc_var(name, val);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-03-26 06:37:01 +02:00
|
|
|
pub fn default_tab_stop(mut self, stop: usize) -> Docx {
|
|
|
|
self.0 = self.0.default_tab_stop(stop);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:27:55 +03:00
|
|
|
pub fn set_adjust_line_height_in_table(mut self) -> Self {
|
|
|
|
self.0.settings = self.0.settings.adjust_line_height_in_table();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-06-23 07:41:36 +03:00
|
|
|
pub fn character_spacing_control(mut self, v: CharacterSpacingValues) -> Self {
|
|
|
|
self.0.settings = self.0.settings.character_spacing_control(v);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-11-25 17:06:16 +02:00
|
|
|
pub fn header(mut self, header: Header) -> Self {
|
|
|
|
self.0 = self.0.header(header.take());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-11-27 12:12:06 +02:00
|
|
|
pub fn first_header(mut self, header: Header) -> Self {
|
|
|
|
self.0 = self.0.first_header(header.take());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn even_header(mut self, header: Header) -> Self {
|
|
|
|
self.0 = self.0.even_header(header.take());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
pub fn footer(mut self, footer: Footer) -> Self {
|
|
|
|
self.0 = self.0.footer(footer.take());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-11-27 12:12:06 +02:00
|
|
|
pub fn first_footer(mut self, footer: Footer) -> Self {
|
|
|
|
self.0 = self.0.first_footer(footer.take());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn even_footer(mut self, footer: Footer) -> Self {
|
|
|
|
self.0 = self.0.even_footer(footer.take());
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-03-24 09:51:11 +02:00
|
|
|
pub fn page_orient(mut self, o: docx_rs::PageOrientationType) -> Docx {
|
|
|
|
self.0 = self.0.page_orient(o);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-10-12 18:04:09 +03:00
|
|
|
pub fn page_margin(mut self, margin: PageMargin) -> Docx {
|
|
|
|
self.0 = self.0.page_margin(margin.take());
|
2020-10-09 14:30:55 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-01-06 09:13:45 +02:00
|
|
|
pub fn add_style(mut self, s: Style) -> Self {
|
|
|
|
self.0.styles = self.0.styles.add_style(s.take());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-12-14 08:01:23 +02:00
|
|
|
pub fn default_size(mut self, size: usize) -> Self {
|
|
|
|
self.0.styles = self.0.styles.default_size(size);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn default_spacing(mut self, spacing: i32) -> Self {
|
|
|
|
self.0.styles = self.0.styles.default_spacing(spacing);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn default_fonts(mut self, font: RunFonts) -> Self {
|
|
|
|
self.0.styles = self.0.styles.default_fonts(font.take());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-06-30 16:37:13 +03:00
|
|
|
pub fn taskpanes(mut self) -> Self {
|
|
|
|
self.0 = self.0.taskpanes();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn web_extension(mut self, ext: WebExtension) -> Self {
|
|
|
|
self.0 = self.0.web_extension(ext.take());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-07-13 12:46:15 +03:00
|
|
|
pub fn add_custom_item(mut self, id: &str, xml: &str) -> Self {
|
|
|
|
self.0 = self.0.add_custom_item(id, xml);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-03-19 18:38:22 +02:00
|
|
|
pub fn doc_grid(
|
|
|
|
mut self,
|
|
|
|
grid_type: docx_rs::DocGridType,
|
|
|
|
line_pitch: Option<usize>,
|
2022-06-15 17:43:19 +03:00
|
|
|
char_space: Option<isize>,
|
2021-03-19 18:38:22 +02:00
|
|
|
) -> Self {
|
|
|
|
let mut doc_grid = docx_rs::DocGrid::with_empty().grid_type(grid_type);
|
|
|
|
if let Some(line_pitch) = line_pitch {
|
|
|
|
doc_grid = doc_grid.line_pitch(line_pitch);
|
|
|
|
}
|
|
|
|
if let Some(char_space) = char_space {
|
|
|
|
doc_grid = doc_grid.char_space(char_space);
|
|
|
|
}
|
|
|
|
self.0.document = self.0.document.doc_grid(doc_grid);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-01-02 19:18:04 +02: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())
|
|
|
|
}
|
2020-12-21 11:44:31 +02:00
|
|
|
|
2020-12-21 13:52:15 +02:00
|
|
|
pub fn json_with_update_comments(&mut self) -> String {
|
|
|
|
self.0.json_with_update_comments()
|
2020-12-21 11:44:31 +02:00
|
|
|
}
|
2019-12-11 07:12:22 +02:00
|
|
|
}
|