From a4c38a7b0ed489c4c42d6bb9127213e03dd13043 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Wed, 12 Mar 2025 15:31:09 +0900 Subject: [PATCH] feat: add shd for run (#810) --- docx-core/src/reader/run_property.rs | 5 ++++ docx-wasm/js/json/run.ts | 2 ++ docx-wasm/js/run-property.ts | 27 +++++++++++++++++++ docx-wasm/js/run.ts | 6 +++++ docx-wasm/package.json | 2 +- docx-wasm/src/run.rs | 12 ++++++++- docx-wasm/src/run_property.rs | 11 ++++++++ docx-wasm/src/style.rs | 12 ++++++++- .../test/__snapshots__/index.test.js.snap | 16 +++++++++++ docx-wasm/test/index.test.js | 14 ++++++++++ 10 files changed, 104 insertions(+), 3 deletions(-) diff --git a/docx-core/src/reader/run_property.rs b/docx-core/src/reader/run_property.rs index 09d5449..c737298 100644 --- a/docx-core/src/reader/run_property.rs +++ b/docx-core/src/reader/run_property.rs @@ -128,6 +128,11 @@ impl ElementReader for RunProperty { } rp = rp.italic(); } + XMLElement::Shading => { + if let Ok(shd) = Shading::read(r, &attributes) { + rp = rp.shading(shd); + } + } XMLElement::Vanish => rp = rp.vanish(), XMLElement::SpecVanish => rp = rp.spec_vanish(), XMLElement::TextBorder => { diff --git a/docx-wasm/js/json/run.ts b/docx-wasm/js/json/run.ts index 0b09938..4906c5f 100644 --- a/docx-wasm/js/json/run.ts +++ b/docx-wasm/js/json/run.ts @@ -5,6 +5,7 @@ import { CommentRangeEndJSON, InsertJSONData, DeleteJSONData, + ShadingJSON, } from ".."; import { BorderType } from "../border"; import { VertAlignType } from "../run-property"; @@ -56,6 +57,7 @@ export type RunPropertyJSON = { del?: DeleteJSONData | null; strike?: boolean; dstrike?: boolean; + shading?: ShadingJSON | null; }; export type RunChildJSON = diff --git a/docx-wasm/js/run-property.ts b/docx-wasm/js/run-property.ts index 3645c04..028f259 100644 --- a/docx-wasm/js/run-property.ts +++ b/docx-wasm/js/run-property.ts @@ -1,6 +1,7 @@ import * as wasm from "./pkg/docx_wasm"; import { BorderType } from "./border"; +import { Shading } from "./shading"; export type TextBorder = { borderType: BorderType; @@ -38,6 +39,7 @@ export class RunProperty { _textBorder?: TextBorder; _ins?: RunPropertyIns; _del?: RunPropertyDel; + _shading?: Shading; style(style: string) { this._style = style; @@ -143,6 +145,15 @@ export class RunProperty { }; return this; } + + shading(type: string, color: string, fill: string) { + const s = new Shading(); + s.color(color); + s.fill(fill); + s.type(type); + this._shading = s; + return this; + } } export const convertBorderType = (t: BorderType) => { @@ -340,6 +351,14 @@ export const setRunProperty = ( target = target.fonts(fonts) as T; } + if (property._shading != null) { + target = target.shading( + property._shading._type, + property._shading._color, + property._shading._fill + ) as T; + } + return target; }; @@ -429,5 +448,13 @@ export const createRunProperty = (property: RunProperty): wasm.RunProperty => { target = target.fonts(fonts); } + if (property._shading != null) { + target = target.shading( + property._shading._type, + property._shading._color, + property._shading._fill + ); + } + return target; }; diff --git a/docx-wasm/js/run.ts b/docx-wasm/js/run.ts index aa32a16..1270453 100644 --- a/docx-wasm/js/run.ts +++ b/docx-wasm/js/run.ts @@ -160,6 +160,12 @@ export class Run { return this; } + shading(type: string, color: string, fill: string) { + this.property ??= createDefaultRunProperty(); + this.property.shading(type, color, fill); + return this; + } + build() { let run = wasm.createRun(); this.children.forEach((child) => { diff --git a/docx-wasm/package.json b/docx-wasm/package.json index d640727..310f5d8 100644 --- a/docx-wasm/package.json +++ b/docx-wasm/package.json @@ -1,6 +1,6 @@ { "name": "docx-wasm", - "version": "0.4.18-rc42", + "version": "0.4.18-rc43", "main": "dist/node/index.js", "browser": "dist/web/index.js", "author": "bokuweb ", diff --git a/docx-wasm/src/run.rs b/docx-wasm/src/run.rs index 0858a1f..7328bf0 100644 --- a/docx-wasm/src/run.rs +++ b/docx-wasm/src/run.rs @@ -1,5 +1,6 @@ use super::*; -use docx_rs::{BorderType, TextBorder, VertAlignType}; +use docx_rs::{BorderType, Shading, TextBorder, VertAlignType}; +use std::str::FromStr; use wasm_bindgen::prelude::*; #[wasm_bindgen] @@ -137,6 +138,15 @@ impl Run { self } + pub fn shading(mut self, t: &str, color: &str, fill: &str) -> Self { + let mut s = Shading::new().color(color).fill(fill); + if let Ok(t) = docx_rs::ShdType::from_str(t) { + s = s.shd_type(t); + } + self.0.run_property = self.0.run_property.shading(s); + self + } + pub fn text_border( mut self, border_type: BorderType, diff --git a/docx-wasm/src/run_property.rs b/docx-wasm/src/run_property.rs index 3db76df..c0213ad 100644 --- a/docx-wasm/src/run_property.rs +++ b/docx-wasm/src/run_property.rs @@ -1,5 +1,7 @@ use super::*; use wasm_bindgen::prelude::*; +use std::str::FromStr; + #[wasm_bindgen] pub struct RunProperty(docx_rs::RunProperty); @@ -130,6 +132,15 @@ impl RunProperty { self.0 = self.0.text_border(border); self } + + pub fn shading(mut self, t: &str, color: &str, fill: &str) -> Self { + let mut s = docx_rs::Shading::new().color(color).fill(fill); + if let Ok(t) = docx_rs::ShdType::from_str(t) { + s = s.shd_type(t); + } + self.0 = self.0.shading(s); + self + } } impl RunProperty { diff --git a/docx-wasm/src/style.rs b/docx-wasm/src/style.rs index 4df2beb..364f06b 100644 --- a/docx-wasm/src/style.rs +++ b/docx-wasm/src/style.rs @@ -1,5 +1,6 @@ use super::*; -use docx_rs::{BorderType, TextBorder, VertAlignType, WidthType}; +use docx_rs::{BorderType, Shading, TextBorder, VertAlignType, WidthType}; +use std::str::FromStr; use wasm_bindgen::prelude::*; #[wasm_bindgen] @@ -63,6 +64,15 @@ impl Style { self } + pub fn shading(mut self, t: &str, color: &str, fill: &str) -> Self { + let mut s = Shading::new().color(color).fill(fill); + if let Ok(t) = docx_rs::ShdType::from_str(t) { + s = s.shd_type(t); + } + self.0.run_property = self.0.run_property.shading(s); + self + } + pub fn link(mut self, link: &str) -> Self { self.0 = self.0.link(link); self diff --git a/docx-wasm/test/__snapshots__/index.test.js.snap b/docx-wasm/test/__snapshots__/index.test.js.snap index 29e7338..04f513d 100644 --- a/docx-wasm/test/__snapshots__/index.test.js.snap +++ b/docx-wasm/test/__snapshots__/index.test.js.snap @@ -19078,6 +19078,11 @@ Object { }, "runProperty": Object { "color": "605E5C", + "shading": Object { + "color": "auto", + "fill": "E1DFDD", + "shdType": "clear", + }, }, "styleId": "a4", "styleType": "character", @@ -20805,6 +20810,11 @@ Object { }, "runProperty": Object { "color": "605E5C", + "shading": Object { + "color": "auto", + "fill": "E1DFDD", + "shdType": "clear", + }, }, "styleId": "a4", "styleType": "character", @@ -174650,6 +174660,12 @@ exports[`writer should write ptab 2`] = `""`; +exports[`writer should write shd 1`] = `""`; + +exports[`writer should write shd 2`] = `"Hello World!"`; + +exports[`writer should write shd 3`] = `""`; + exports[`writer should write strike 1`] = `""`; exports[`writer should write strike 2`] = `"Hello world!!"`; diff --git a/docx-wasm/test/index.test.js b/docx-wasm/test/index.test.js index 7bd0576..1dd63ab 100644 --- a/docx-wasm/test/index.test.js +++ b/docx-wasm/test/index.test.js @@ -470,6 +470,20 @@ describe("writer", () => { } }); + test("should write shd", () => { + const p = new w.Paragraph() + .addRun(new w.Run().addText("Hello ")) + .addRun(new w.Run().addText("World!").shading("clear", "auto", "FFC000")); + const buffer = new w.Docx().addParagraph(p).build(); + writeFileSync("../output/js/shading.docx", buffer); + const z = new Zip(Buffer.from(buffer)); + for (const e of z.getEntries()) { + if (e.entryName.match(/document.xml|numbering.xml/)) { + expect(z.readAsText(e)).toMatchSnapshot(); + } + } + }); + test("should write page orientation", () => { const p = new w.Paragraph().addRun(new w.Run().addText("Hello ")); const buffer = new w.Docx()