From 75e6b77d4a60be15953567502a7781ec93068b7d Mon Sep 17 00:00:00 2001 From: bokuweb Date: Mon, 1 Apr 2024 11:02:48 +0900 Subject: [PATCH] fix: support tabs (#698) * fix: support tabs * fix * fix * fix * fix --- Cargo.lock | 2 +- docx-core/src/documents/elements/paragraph.rs | 7 +++ docx-core/src/documents/elements/tab.rs | 2 + docx-core/src/types/tab_leader_type.rs | 2 + docx-core/src/types/tab_value_type.rs | 2 + docx-wasm/js/json/bindings/Tab.ts | 1 - docx-wasm/js/json/bindings/TabLeaderType.ts | 3 +- docx-wasm/js/json/bindings/TabValueType.ts | 1 - docx-wasm/js/paragraph-property.ts | 63 +++++++++++++++++++ docx-wasm/js/paragraph.ts | 13 ++++ docx-wasm/package.json | 2 +- docx-wasm/src/paragraph.rs | 10 +++ docx-wasm/src/style.rs | 13 ++++ 13 files changed, 115 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e982bf8..b7a51bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -143,7 +143,7 @@ checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499" [[package]] name = "docx-rs" -version = "0.4.11" +version = "0.4.13" dependencies = [ "base64", "image", diff --git a/docx-core/src/documents/elements/paragraph.rs b/docx-core/src/documents/elements/paragraph.rs index 6950916..56290a6 100644 --- a/docx-core/src/documents/elements/paragraph.rs +++ b/docx-core/src/documents/elements/paragraph.rs @@ -245,6 +245,13 @@ impl Paragraph { self } + pub fn tabs(mut self, tabs: &[Tab]) -> Self { + for tab in tabs { + self.property = self.property.add_tab(tab.clone()); + } + self + } + pub fn indent( mut self, left: Option, diff --git a/docx-core/src/documents/elements/tab.rs b/docx-core/src/documents/elements/tab.rs index f10a92b..46c6ca2 100644 --- a/docx-core/src/documents/elements/tab.rs +++ b/docx-core/src/documents/elements/tab.rs @@ -5,6 +5,8 @@ use crate::types::*; use crate::xml_builder::*; #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Default)] +#[cfg_attr(feature = "wasm", derive(ts_rs::TS))] +#[cfg_attr(feature = "wasm", ts(export))] pub struct Tab { pub val: Option, pub leader: Option, diff --git a/docx-core/src/types/tab_leader_type.rs b/docx-core/src/types/tab_leader_type.rs index 27eb55a..7cefd20 100644 --- a/docx-core/src/types/tab_leader_type.rs +++ b/docx-core/src/types/tab_leader_type.rs @@ -8,6 +8,8 @@ use wasm_bindgen::prelude::*; use super::errors; #[cfg_attr(feature = "wasm", wasm_bindgen)] +#[cfg_attr(feature = "wasm", derive(ts_rs::TS))] +#[cfg_attr(feature = "wasm", ts(export))] #[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq)] #[serde(rename_all = "camelCase")] pub enum TabLeaderType { diff --git a/docx-core/src/types/tab_value_type.rs b/docx-core/src/types/tab_value_type.rs index 26e4c22..6c4d617 100644 --- a/docx-core/src/types/tab_value_type.rs +++ b/docx-core/src/types/tab_value_type.rs @@ -8,6 +8,8 @@ use wasm_bindgen::prelude::*; use super::errors; #[cfg_attr(feature = "wasm", wasm_bindgen)] +#[cfg_attr(feature = "wasm", derive(ts_rs::TS))] +#[cfg_attr(feature = "wasm", ts(export))] #[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq)] #[serde(rename_all = "camelCase")] pub enum TabValueType { diff --git a/docx-wasm/js/json/bindings/Tab.ts b/docx-wasm/js/json/bindings/Tab.ts index 475e4e4..b5c6127 100644 --- a/docx-wasm/js/json/bindings/Tab.ts +++ b/docx-wasm/js/json/bindings/Tab.ts @@ -1,4 +1,3 @@ -// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. import type { TabLeaderType } from "./TabLeaderType"; import type { TabValueType } from "./TabValueType"; diff --git a/docx-wasm/js/json/bindings/TabLeaderType.ts b/docx-wasm/js/json/bindings/TabLeaderType.ts index 6765792..16bee91 100644 --- a/docx-wasm/js/json/bindings/TabLeaderType.ts +++ b/docx-wasm/js/json/bindings/TabLeaderType.ts @@ -1,3 +1,2 @@ -// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. -export type TabLeaderType = "dot" | "heavy" | "hyphen" | "middledot" | "none" | "underscore"; \ No newline at end of file +export type TabLeaderType = "dot" | "heavy" | "hyphen" | "middleDot" | "none" | "underscore"; \ No newline at end of file diff --git a/docx-wasm/js/json/bindings/TabValueType.ts b/docx-wasm/js/json/bindings/TabValueType.ts index d4d0671..3fc08a6 100644 --- a/docx-wasm/js/json/bindings/TabValueType.ts +++ b/docx-wasm/js/json/bindings/TabValueType.ts @@ -1,3 +1,2 @@ -// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. export type TabValueType = "bar" | "center" | "clear" | "decimal" | "end" | "right" | "num" | "start" | "left"; \ No newline at end of file diff --git a/docx-wasm/js/paragraph-property.ts b/docx-wasm/js/paragraph-property.ts index 935a758..d3b6658 100644 --- a/docx-wasm/js/paragraph-property.ts +++ b/docx-wasm/js/paragraph-property.ts @@ -2,6 +2,7 @@ import { RunProperty, createDefaultRunProperty } from "./run"; import * as wasm from "./pkg"; import { TextAlignmentType } from "./json/bindings/TextAlignmentType"; +import { Tab } from "./json/bindings/Tab"; export type AlignmentType = | "center" @@ -73,6 +74,7 @@ export type ParagraphProperty = { paragraphPropertyChange?: ParagraphPropertyChange; outlineLvl?: number | null; adjustRightInd?: number; + tabs?: Tab[]; }; export const createDefaultParagraphProperty = (): ParagraphProperty => { @@ -338,5 +340,66 @@ export const setParagraphProperty = ( target = target.outline_lvl(property.outlineLvl) as T; } + if (property.tabs) { + for (const tab of property.tabs) { + let val: wasm.TabValueType | undefined; + let leader: wasm.TabLeaderType | undefined; + switch (tab.val) { + case "bar": + val = wasm.TabValueType.Bar; + break; + case "bar": + val = wasm.TabValueType.Bar; + break; + case "center": + val = wasm.TabValueType.Center; + break; + case "clear": + val = wasm.TabValueType.Clear; + break; + case "decimal": + val = wasm.TabValueType.Decimal; + break; + case "end": + val = wasm.TabValueType.End; + break; + case "right": + val = wasm.TabValueType.Right; + break; + case "num": + val = wasm.TabValueType.Num; + break; + case "start": + val = wasm.TabValueType.Start; + break; + case "left": + val = wasm.TabValueType.Left; + break; + } + + switch (tab.leader) { + case "dot": + leader = wasm.TabLeaderType.Dot; + break; + case "heavy": + leader = wasm.TabLeaderType.Heavy; + break; + case "hyphen": + leader = wasm.TabLeaderType.Hyphen; + break; + case "middleDot": + leader = wasm.TabLeaderType.MiddleDot; + break; + case "none": + leader = wasm.TabLeaderType.None; + break; + case "underscore": + leader = wasm.TabLeaderType.None; + break; + } + target = target.add_tab(val, leader, tab.pos ?? undefined) as T; + } + } + return target; }; diff --git a/docx-wasm/js/paragraph.ts b/docx-wasm/js/paragraph.ts index 1f25d3f..295adab 100644 --- a/docx-wasm/js/paragraph.ts +++ b/docx-wasm/js/paragraph.ts @@ -15,6 +15,8 @@ import { Comment } from "./comment"; import { CommentEnd } from "./comment-end"; import { Hyperlink } from "./hyperlink"; import { TextAlignmentType } from "./json/bindings/TextAlignmentType"; +import { TabValueType } from "./json/bindings/TabValueType"; +import { TabLeaderType } from "./json/bindings/TabLeaderType"; export type ParagraphChild = | Run @@ -71,6 +73,17 @@ export class Paragraph { return this; } + tabs( + tabs: { + val: TabValueType | null; + leader: TabLeaderType | null; + pos: number | null; + }[] + ) { + this.property.tabs = tabs; + return this; + } + align(type: AlignmentType) { this.property.align = type; return this; diff --git a/docx-wasm/package.json b/docx-wasm/package.json index 5fb12cf..cd38ff4 100644 --- a/docx-wasm/package.json +++ b/docx-wasm/package.json @@ -1,6 +1,6 @@ { "name": "docx-wasm", - "version": "0.4.13", + "version": "0.4.14-beta2", "main": "dist/node/index.js", "browser": "dist/web/index.js", "author": "bokuweb ", diff --git a/docx-wasm/src/paragraph.rs b/docx-wasm/src/paragraph.rs index 7c244aa..6c091d6 100644 --- a/docx-wasm/src/paragraph.rs +++ b/docx-wasm/src/paragraph.rs @@ -233,6 +233,16 @@ impl Paragraph { self } + pub fn add_tab( + mut self, + val: Option, + leader: Option, + pos: Option, + ) -> Self { + self.0 = self.0.add_tab(docx_rs::Tab { val, leader, pos }); + self + } + pub fn paragraph_property_change(mut self, p: ParagraphPropertyChange) -> Self { self.0.property = self.0.property.paragraph_property_change(p.take()); self diff --git a/docx-wasm/src/style.rs b/docx-wasm/src/style.rs index e111d24..f28362b 100644 --- a/docx-wasm/src/style.rs +++ b/docx-wasm/src/style.rs @@ -114,6 +114,19 @@ impl Style { self } + pub fn add_tab( + mut self, + val: Option, + leader: Option, + pos: Option, + ) -> Self { + self.0.paragraph_property = + self.0 + .paragraph_property + .add_tab(docx_rs::Tab { val, leader, pos }); + self + } + pub fn indent( mut self, left: i32,