From c492b86abad1d37d5d5ba66389cb086278fdbe2b Mon Sep 17 00:00:00 2001 From: bokuweb Date: Fri, 7 Mar 2025 18:14:17 +0900 Subject: [PATCH] Support dstrike (#807) * fix: support dstrike * fix * fix: package --- CHANGELOG.md | 2 + docx-core/src/documents/elements/dstrike.rs | 45 +++++++++++++++++++ docx-core/src/documents/elements/level.rs | 10 +++++ docx-core/src/documents/elements/mod.rs | 2 + docx-core/src/documents/elements/run.rs | 5 +++ .../src/documents/elements/run_property.rs | 25 +++++++++++ docx-core/src/reader/xml_element.rs | 2 + docx-core/src/xml_builder/elements.rs | 1 + docx-wasm/js/json/run.ts | 1 + docx-wasm/js/run-property.ts | 23 ++++++++++ docx-wasm/js/run.ts | 6 +++ docx-wasm/js/style.ts | 5 +++ docx-wasm/package.json | 2 +- docx-wasm/src/level.rs | 10 +++++ docx-wasm/src/run.rs | 5 +++ docx-wasm/src/run_property.rs | 10 +++++ docx-wasm/src/style.rs | 5 +++ .../test/__snapshots__/index.test.js.snap | 6 +++ docx-wasm/test/index.test.js | 14 ++++++ 19 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 docx-core/src/documents/elements/dstrike.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index df3598b..ee3a067 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 diff --git a/docx-core/src/documents/elements/dstrike.rs b/docx-core/src/documents/elements/dstrike.rs new file mode 100644 index 0000000..8dc86c6 --- /dev/null +++ b/docx-core/src/documents/elements/dstrike.rs @@ -0,0 +1,45 @@ +use serde::{Deserialize, Serialize, Serializer}; +use std::io::Write; + +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone, Deserialize, PartialEq)] +pub struct Dstrike { + pub val: bool, +} + +impl Dstrike { + pub fn new() -> Dstrike { + Default::default() + } + + pub fn disable(mut self) -> Dstrike { + self.val = false; + self + } +} + +impl Default for Dstrike { + fn default() -> Self { + Self { val: true } + } +} + +impl Serialize for Dstrike { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_bool(self.val) + } +} + +impl BuildXML for Dstrike { + fn build_to( + &self, + stream: xml::writer::EventWriter, + ) -> xml::writer::Result> { + XMLBuilder::from(stream).dstrike()?.into_inner() + } +} diff --git a/docx-core/src/documents/elements/level.rs b/docx-core/src/documents/elements/level.rs index 0cc3881..0e87da2 100644 --- a/docx-core/src/documents/elements/level.rs +++ b/docx-core/src/documents/elements/level.rs @@ -119,6 +119,16 @@ impl Level { self } + pub fn dstrike(mut self) -> Self { + self.run_property = self.run_property.dstrike(); + self + } + + pub fn disable_dstrike(mut self) -> Self { + self.run_property = self.run_property.disable_dstrike(); + self + } + pub fn underline(mut self, line_type: impl Into) -> Self { self.run_property = self.run_property.underline(line_type); self diff --git a/docx-core/src/documents/elements/mod.rs b/docx-core/src/documents/elements/mod.rs index 808ded4..1feecb9 100644 --- a/docx-core/src/documents/elements/mod.rs +++ b/docx-core/src/documents/elements/mod.rs @@ -28,6 +28,7 @@ mod doc_grid; mod doc_id; mod doc_var; mod drawing; +mod dstrike; mod fld_char; mod font; mod font_scheme; @@ -166,6 +167,7 @@ pub use doc_grid::*; pub use doc_id::*; pub use doc_var::*; pub use drawing::*; +pub use dstrike::*; pub use fld_char::*; pub use font::*; pub use font_scheme::*; diff --git a/docx-core/src/documents/elements/run.rs b/docx-core/src/documents/elements/run.rs index dd8b8d1..eef316e 100644 --- a/docx-core/src/documents/elements/run.rs +++ b/docx-core/src/documents/elements/run.rs @@ -293,6 +293,11 @@ impl Run { self } + pub fn dstrike(mut self) -> Run { + self.run_property = self.run_property.dstrike(); + self + } + pub fn text_border(mut self, b: TextBorder) -> Run { self.run_property = self.run_property.text_border(b); self diff --git a/docx-core/src/documents/elements/run_property.rs b/docx-core/src/documents/elements/run_property.rs index 2a50720..9529710 100644 --- a/docx-core/src/documents/elements/run_property.rs +++ b/docx-core/src/documents/elements/run_property.rs @@ -50,6 +50,8 @@ pub struct RunProperty { #[serde(skip_serializing_if = "Option::is_none")] pub strike: Option, #[serde(skip_serializing_if = "Option::is_none")] + pub dstrike: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub positional_tab: Option, #[serde(skip_serializing_if = "Option::is_none")] pub shading: Option, @@ -116,6 +118,7 @@ impl RunProperty { pub fn strike(mut self) -> RunProperty { self.strike = Some(Strike::new()); + self.dstrike = None; self } @@ -124,6 +127,17 @@ impl RunProperty { self } + pub fn dstrike(mut self) -> RunProperty { + self.dstrike = Some(Dstrike::new()); + self.strike = None; + self + } + + pub fn disable_dstrike(mut self) -> RunProperty { + self.dstrike = Some(Dstrike::new().disable()); + self + } + pub fn disable_italic(mut self) -> RunProperty { self.italic = Some(Italic::new().disable()); self.italic_cs = Some(ItalicCs::new().disable()); @@ -197,6 +211,7 @@ impl BuildXML for RunProperty { .add_optional_child(&self.italic)? .add_optional_child(&self.italic_cs)? .add_optional_child(&self.strike)? + .add_optional_child(&self.dstrike)? .add_optional_child(&self.highlight)? .add_optional_child(&self.underline)? .add_optional_child(&self.vanish)? @@ -331,4 +346,14 @@ mod tests { r#""# ); } + + #[test] + fn test_dstrike() { + let c = RunProperty::new().dstrike(); + let b = c.build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""# + ); + } } diff --git a/docx-core/src/reader/xml_element.rs b/docx-core/src/reader/xml_element.rs index 4a965e0..f8762bc 100644 --- a/docx-core/src/reader/xml_element.rs +++ b/docx-core/src/reader/xml_element.rs @@ -79,6 +79,7 @@ pub enum XMLElement { VAlign, Shading, Strike, + Dstrike, TextDirection, Table, TableProperty, @@ -381,6 +382,7 @@ impl FromStr for XMLElement { "lvlOverride" => Ok(XMLElement::LvlOverride), "startOverride" => Ok(XMLElement::StartOverride), "strike" => Ok(XMLElement::Strike), + "dstrike" => Ok(XMLElement::Dstrike), "docId" => Ok(XMLElement::DocId), "docVar" => Ok(XMLElement::DocVar), "docVars" => Ok(XMLElement::DocVars), diff --git a/docx-core/src/xml_builder/elements.rs b/docx-core/src/xml_builder/elements.rs index ecd6649..439b96f 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -189,6 +189,7 @@ impl XMLBuilder { closed!(i_cs, "w:iCs"); closed!(strike, "w:strike"); + closed!(dstrike, "w:dstrike"); // Build w:style element // i.e. diff --git a/docx-wasm/js/json/run.ts b/docx-wasm/js/json/run.ts index 7aac85f..0b09938 100644 --- a/docx-wasm/js/json/run.ts +++ b/docx-wasm/js/json/run.ts @@ -55,6 +55,7 @@ export type RunPropertyJSON = { ins?: InsertJSONData | null; del?: DeleteJSONData | null; strike?: boolean; + dstrike?: boolean; }; export type RunChildJSON = diff --git a/docx-wasm/js/run-property.ts b/docx-wasm/js/run-property.ts index 9409062..3645c04 100644 --- a/docx-wasm/js/run-property.ts +++ b/docx-wasm/js/run-property.ts @@ -30,6 +30,7 @@ export class RunProperty { _bold?: boolean; _italic?: boolean; _strike?: boolean; + _dstrike?: boolean; _underline?: string; _vanish?: boolean; _fonts?: RunFonts; @@ -83,6 +84,16 @@ export class RunProperty { return this; } + dstrike() { + this._dstrike = true; + return this; + } + + disableDstrike() { + this._dstrike = false; + return this; + } + italic() { this._italic = true; return this; @@ -298,6 +309,10 @@ export const setRunProperty = ( target = target.strike() as T; } + if (property._dstrike) { + target = target.dstrike() as T; + } + if (property._underline) { target = target.underline(property._underline) as T; } @@ -379,6 +394,14 @@ export const createRunProperty = (property: RunProperty): wasm.RunProperty => { } } + if (property._dstrike != null) { + if (property._dstrike) { + target = target.dstrike(); + } else { + target = target.disable_dstrike(); + } + } + if (property._underline) { target = target.underline(property._underline); } diff --git a/docx-wasm/js/run.ts b/docx-wasm/js/run.ts index bbe5ab5..aa32a16 100644 --- a/docx-wasm/js/run.ts +++ b/docx-wasm/js/run.ts @@ -106,6 +106,12 @@ export class Run { return this; } + dstrike() { + this.property ??= createDefaultRunProperty(); + this.property.dstrike(); + return this; + } + italic() { this.property ??= createDefaultRunProperty(); this.property.italic(); diff --git a/docx-wasm/js/style.ts b/docx-wasm/js/style.ts index c27c832..2ef6590 100644 --- a/docx-wasm/js/style.ts +++ b/docx-wasm/js/style.ts @@ -100,6 +100,11 @@ export class Style { return this; } + dstrike() { + this._runProperty.dstrike(); + return this; + } + italic() { this._runProperty.italic(); return this; diff --git a/docx-wasm/package.json b/docx-wasm/package.json index 4f87745..2efbd51 100644 --- a/docx-wasm/package.json +++ b/docx-wasm/package.json @@ -1,6 +1,6 @@ { "name": "docx-wasm", - "version": "0.4.18-rc40", + "version": "0.4.18-rc41", "main": "dist/node/index.js", "browser": "dist/web/index.js", "author": "bokuweb ", diff --git a/docx-wasm/src/level.rs b/docx-wasm/src/level.rs index 5d336d4..765c957 100644 --- a/docx-wasm/src/level.rs +++ b/docx-wasm/src/level.rs @@ -82,6 +82,16 @@ impl Level { self } + pub fn dstrike(mut self) -> Self { + self.0 = self.0.dstrike(); + self + } + + pub fn disable_dstrike(mut self) -> Self { + self.0 = self.0.disable_dstrike(); + self + } + pub fn disable_italic(mut self) -> Self { self.0 = self.0.disable_italic(); self diff --git a/docx-wasm/src/run.rs b/docx-wasm/src/run.rs index 89e42dd..0858a1f 100644 --- a/docx-wasm/src/run.rs +++ b/docx-wasm/src/run.rs @@ -107,6 +107,11 @@ impl Run { self } + pub fn dstrike(mut self) -> Run { + self.0.run_property = self.0.run_property.dstrike(); + self + } + pub fn underline(mut self, line_type: &str) -> Run { self.0.run_property = self.0.run_property.underline(line_type); self diff --git a/docx-wasm/src/run_property.rs b/docx-wasm/src/run_property.rs index 7d027b6..3db76df 100644 --- a/docx-wasm/src/run_property.rs +++ b/docx-wasm/src/run_property.rs @@ -51,6 +51,16 @@ impl RunProperty { self } + pub fn dstrike(mut self) -> Self { + self.0 = self.0.dstrike(); + self + } + + pub fn disable_dstrike(mut self) -> Self { + self.0 = self.0.disable_dstrike(); + self + } + pub fn fonts(mut self, f: RunFonts) -> Self { self.0 = self.0.fonts(f.take()); self diff --git a/docx-wasm/src/style.rs b/docx-wasm/src/style.rs index 5683666..4df2beb 100644 --- a/docx-wasm/src/style.rs +++ b/docx-wasm/src/style.rs @@ -53,6 +53,11 @@ impl Style { self } + pub fn dstrike(mut self) -> Self { + self.0.run_property = self.0.run_property.dstrike(); + self + } + pub fn underline(mut self, line_type: &str) -> Self { self.0.run_property = self.0.run_property.underline(line_type); self diff --git a/docx-wasm/test/__snapshots__/index.test.js.snap b/docx-wasm/test/__snapshots__/index.test.js.snap index d547457..e977c8a 100644 --- a/docx-wasm/test/__snapshots__/index.test.js.snap +++ b/docx-wasm/test/__snapshots__/index.test.js.snap @@ -171141,6 +171141,12 @@ exports[`writer should write doc vars 2`] = `""`; +exports[`writer should write dstrike 1`] = `""`; + +exports[`writer should write dstrike 2`] = `"Hello World!"`; + +exports[`writer should write dstrike 3`] = `""`; + exports[`writer should write evenFooter with table for default section 1`] = `""`; exports[`writer should write evenFooter with table for default section 2`] = `""`; diff --git a/docx-wasm/test/index.test.js b/docx-wasm/test/index.test.js index 9f46ef1..f76f954 100644 --- a/docx-wasm/test/index.test.js +++ b/docx-wasm/test/index.test.js @@ -450,6 +450,20 @@ describe("writer", () => { } }); + test("should write dstrike", () => { + const p = new w.Paragraph() + .addRun(new w.Run().addText("Hello ")) + .addRun(new w.Run().addText("World!").dstrike()); + const buffer = new w.Docx().addParagraph(p).build(); + writeFileSync("../output/js/dstrike.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()