fix: support tabs (#698)

* fix: support tabs

* fix

* fix

* fix

* fix
main
bokuweb 2024-04-01 11:02:48 +09:00 committed by GitHub
parent e886c72c58
commit 75e6b77d4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 115 additions and 6 deletions

2
Cargo.lock generated
View File

@ -143,7 +143,7 @@ checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499"
[[package]]
name = "docx-rs"
version = "0.4.11"
version = "0.4.13"
dependencies = [
"base64",
"image",

View File

@ -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<i32>,

View File

@ -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<TabValueType>,
pub leader: Option<TabLeaderType>,

View File

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

View File

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

View File

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

View File

@ -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";
export type TabLeaderType = "dot" | "heavy" | "hyphen" | "middleDot" | "none" | "underscore";

View File

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

View File

@ -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 = <T extends wasm.Paragraph | wasm.Style>(
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;
};

View File

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

View File

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

View File

@ -233,6 +233,16 @@ impl Paragraph {
self
}
pub fn add_tab(
mut self,
val: Option<docx_rs::TabValueType>,
leader: Option<docx_rs::TabLeaderType>,
pos: Option<usize>,
) -> 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

View File

@ -114,6 +114,19 @@ impl Style {
self
}
pub fn add_tab(
mut self,
val: Option<docx_rs::TabValueType>,
leader: Option<docx_rs::TabLeaderType>,
pos: Option<usize>,
) -> 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,