From 245da8dcf1786adf091bd923d1ac8100d66b01f0 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Thu, 30 Apr 2020 13:39:56 +0900 Subject: [PATCH] 0.0.63 (#66) * 0.0.63 * fix: ci --- .github/workflows/ci.yml | 25 +++++++ .../documents/elements/table_cell_borders.rs | 12 ++++ docx-wasm/js/index.ts | 72 ++++++++++++++----- docx-wasm/js/table-cell-borders.ts | 8 +++ docx-wasm/js/table-cell.ts | 16 +++-- docx-wasm/package.json | 2 +- docx-wasm/src/table_cell_border.rs | 17 +++++ 7 files changed, 126 insertions(+), 26 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6f08d81..c9fe502 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -63,6 +63,31 @@ jobs: path: ~/.cargo/git key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} - run: cargo build + + # build-wasm: + # runs-on: ubuntu-latest + # steps: + # - uses: actions/checkout@master + # - uses: actions-rs/toolchain@v1 + # with: + # profile: minimal + # toolchain: stable + # override: true + # - name: Cache cargo registry + # uses: actions/cache@v1 + # with: + # path: ~/.cargo/registry + # key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} + # - name: Cache cargo index + # uses: actions/cache@v1 + # with: + # path: ~/.cargo/git + # key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} + # - run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh + # - run: cd docx-wasm && yarn install + # - run: rustup target add wasm32-unknown-unknown + # - run: cd docx-wasm && npm run build + lint: name: Clippy runs-on: ubuntu-latest diff --git a/docx-core/src/documents/elements/table_cell_borders.rs b/docx-core/src/documents/elements/table_cell_borders.rs index 5659bed..0212e18 100644 --- a/docx-core/src/documents/elements/table_cell_borders.rs +++ b/docx-core/src/documents/elements/table_cell_borders.rs @@ -51,6 +51,18 @@ impl TableCellBorder { self.border_type = border_type; self } + + pub fn get_size(&self) -> usize { + self.size + } + + pub fn get_color(&self) -> String { + self.color.clone() + } + + pub fn get_border_type(&self) -> BorderType { + self.border_type + } } impl BuildXML for TableCellBorder { diff --git a/docx-wasm/js/index.ts b/docx-wasm/js/index.ts index 80fd471..b0ab087 100644 --- a/docx-wasm/js/index.ts +++ b/docx-wasm/js/index.ts @@ -4,6 +4,7 @@ import { Delete } from "./delete"; import { DeleteText } from "./delete-text"; import { Table } from "./table"; import { TableCell } from "./table-cell"; +import { BorderType } from "./table-cell-border"; import { Run } from "./run"; import { Text } from "./text"; import { Tab } from "./tab"; @@ -18,6 +19,33 @@ import { DocxJSON } from "./json"; import * as wasm from "./pkg"; +const convertBorderType = (t: BorderType) => { + switch (t) { + case "Nil": + return wasm.BorderType.Nil; + case "None": + return wasm.BorderType.None; + case "Single": + return wasm.BorderType.Single; + case "Thick": + return wasm.BorderType.Thick; + case "Double": + return wasm.BorderType.Double; + case "Dotted": + return wasm.BorderType.Dotted; + case "Dashed": + return wasm.BorderType.Dashed; + case "DotDash": + return wasm.BorderType.DotDash; + case "DotDotDash": + return wasm.BorderType.DotDotDash; + case "Triple": + return wasm.BorderType.Triple; + default: + return wasm.BorderType.Single; + } +}; + export class Docx { children: (Paragraph | Table)[] = []; abstractNumberings: AbstractNumbering[] = []; @@ -274,54 +302,62 @@ export class Docx { if (c.property.borders.top) { const border = wasm .createTableCellBorder(wasm.BorderPosition.Top) - .size(c.property.borders.top.size) - .color(c.property.borders.top.color) - .border_type(c.property.borders.top.border_type); + .size(c.property.borders.top._size) + .color(c.property.borders.top._color) + .border_type(convertBorderType(c.property.borders.top._border_type)); cell = cell.set_border(border); } if (c.property.borders.right) { const border = wasm .createTableCellBorder(wasm.BorderPosition.Right) - .size(c.property.borders.right.size) - .color(c.property.borders.right.color) - .border_type(c.property.borders.right.border_type); + .size(c.property.borders.right._size) + .color(c.property.borders.right._color) + .border_type( + convertBorderType(c.property.borders.right._border_type) + ); cell = cell.set_border(border); } if (c.property.borders.bottom) { const border = wasm .createTableCellBorder(wasm.BorderPosition.Bottom) - .size(c.property.borders.bottom.size) - .color(c.property.borders.bottom.color) - .border_type(c.property.borders.bottom.border_type); + .size(c.property.borders.bottom._size) + .color(c.property.borders.bottom._color) + .border_type( + convertBorderType(c.property.borders.bottom._border_type) + ); cell = cell.set_border(border); } if (c.property.borders.left) { const border = wasm .createTableCellBorder(wasm.BorderPosition.Left) - .size(c.property.borders.left.size) - .color(c.property.borders.left.color) - .border_type(c.property.borders.left.border_type); + .size(c.property.borders.left._size) + .color(c.property.borders.left._color) + .border_type(convertBorderType(c.property.borders.left._border_type)); cell = cell.set_border(border); } if (c.property.borders.insideH) { const border = wasm .createTableCellBorder(wasm.BorderPosition.InsideH) - .size(c.property.borders.insideH.size) - .color(c.property.borders.insideH.color) - .border_type(c.property.borders.insideH.border_type); + .size(c.property.borders.insideH._size) + .color(c.property.borders.insideH._color) + .border_type( + convertBorderType(c.property.borders.insideH._border_type) + ); cell = cell.set_border(border); } if (c.property.borders.insideV) { const border = wasm .createTableCellBorder(wasm.BorderPosition.InsideV) - .size(c.property.borders.insideV.size) - .color(c.property.borders.insideV.color) - .border_type(c.property.borders.insideV.border_type); + .size(c.property.borders.insideV._size) + .color(c.property.borders.insideV._color) + .border_type( + convertBorderType(c.property.borders.insideV._border_type) + ); cell = cell.set_border(border); } } diff --git a/docx-wasm/js/table-cell-borders.ts b/docx-wasm/js/table-cell-borders.ts index 007e86a..1b79b1d 100644 --- a/docx-wasm/js/table-cell-borders.ts +++ b/docx-wasm/js/table-cell-borders.ts @@ -1,5 +1,13 @@ import { BorderPosition, TableCellBorder } from "./table-cell-border"; +export type PositionKeys = + | "top" + | "left" + | "bottom" + | "right" + | "insideH" + | "insideV"; + export class TableCellBorders { top: TableCellBorder | null = new TableCellBorder("Top"); left: TableCellBorder | null = new TableCellBorder("Left"); diff --git a/docx-wasm/js/table-cell.ts b/docx-wasm/js/table-cell.ts index 6344769..75b4f47 100644 --- a/docx-wasm/js/table-cell.ts +++ b/docx-wasm/js/table-cell.ts @@ -1,5 +1,5 @@ import { Paragraph } from "./paragraph"; -import { TableCellBorders } from "./table-cell-borders"; +import { TableCellBorders, PositionKeys } from "./table-cell-borders"; import { BorderPosition, TableCellBorder } from "./table-cell-border"; export type VMergeType = "restart" | "continue"; @@ -7,7 +7,7 @@ export type VMergeType = "restart" | "continue"; export type VAlignType = "top" | "center" | "bottom"; export type CellProperty = { - borders?: TableCellBorders; + borders: TableCellBorders; verticalMerge?: VMergeType; verticalAlign?: VAlignType; gridSpan?: number; @@ -16,7 +16,9 @@ export type CellProperty = { export class TableCell { children: Paragraph[] = []; - property: CellProperty = {}; + property: CellProperty = { + borders: new TableCellBorders(), + }; addParagraph(p: Paragraph) { this.children.push(p); @@ -44,14 +46,14 @@ export class TableCell { } setBorder(position: BorderPosition, border: TableCellBorder) { - this.property.borders[position] = border; + this.property.borders[position.toLowerCase() as PositionKeys] = border; return this; } clearBorder(position: BorderPosition) { - this.property.borders[position] = new TableCellBorder(position).border_type( - "Nil" - ); + this.property.borders[ + position.toLowerCase() as PositionKeys + ] = new TableCellBorder(position).border_type("Nil"); return this; } } diff --git a/docx-wasm/package.json b/docx-wasm/package.json index f64acce..ed5534d 100644 --- a/docx-wasm/package.json +++ b/docx-wasm/package.json @@ -1,6 +1,6 @@ { "name": "docx-wasm", - "version": "0.0.62", + "version": "0.0.63", "main": "dist/node/index.js", "browser": "dist/web/index.js", "author": "bokuweb ", diff --git a/docx-wasm/src/table_cell_border.rs b/docx-wasm/src/table_cell_border.rs index 32642ac..ea527ae 100644 --- a/docx-wasm/src/table_cell_border.rs +++ b/docx-wasm/src/table_cell_border.rs @@ -17,6 +17,11 @@ impl TableCellBorder { #[wasm_bindgen] impl TableCellBorder { + pub fn size(mut self, size: usize) -> TableCellBorder { + self.0.size = size; + self + } + pub fn color(mut self, color: String) -> TableCellBorder { self.0.color = color; self @@ -26,4 +31,16 @@ impl TableCellBorder { self.0.border_type = border_type; self } + + pub fn get_size(&self) -> usize { + self.0.get_size() + } + + pub fn get_color(&self) -> String { + self.0.get_color() + } + + pub fn get_border_type(&self) -> docx_rs::BorderType { + self.0.get_border_type() + } }