feat: add shd for run (#810)
parent
31c109aacb
commit
a4c38a7b0e
|
@ -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 => {
|
||||
|
|
|
@ -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 =
|
||||
|
|
|
@ -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 = <T extends wasm.Run | wasm.Style>(
|
|||
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;
|
||||
};
|
||||
|
|
|
@ -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) => {
|
||||
|
|
|
@ -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 <bokuweb12@gmail.com>",
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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`] = `"<?xml version=\\"1.0\\" encoding=\\"UT
|
|||
|
||||
exports[`writer should write ptab 3`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?><w:numbering xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\" xmlns:w=\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\"><w:abstractNum w:abstractNumId=\\"1\\"><w:lvl w:ilvl=\\"0\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%1.\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"420\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"1\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"(%2)\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"840\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"2\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimalEnclosedCircle\\" /><w:lvlText w:val=\\"%3\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"1260\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"3\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%4.\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"1680\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"4\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"(%5)\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"2100\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"5\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimalEnclosedCircle\\" /><w:lvlText w:val=\\"%6\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"2520\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"6\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%7.\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"2940\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"7\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"(%8)\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"3360\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"8\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimalEnclosedCircle\\" /><w:lvlText w:val=\\"%9\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"3780\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl></w:abstractNum><w:num w:numId=\\"1\\"><w:abstractNumId w:val=\\"1\\" /></w:num></w:numbering>"`;
|
||||
|
||||
exports[`writer should write shd 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><Relationships xmlns=\\"http://schemas.openxmlformats.org/package/2006/relationships\\"><Relationship Id=\\"rId1\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\\" Target=\\"styles.xml\\" /><Relationship Id=\\"rId2\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\\" Target=\\"fontTable.xml\\" /><Relationship Id=\\"rId3\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\\" Target=\\"settings.xml\\" /><Relationship Id=\\"rId5\\" Type=\\"http://schemas.microsoft.com/office/2011/relationships/commentsExtended\\" Target=\\"commentsExtended.xml\\" /></Relationships>"`;
|
||||
|
||||
exports[`writer should write shd 2`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?><w:document xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\" xmlns:w=\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\" xmlns:w10=\\"urn:schemas-microsoft-com:office:word\\" xmlns:wp=\\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\\" xmlns:wps=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\\" xmlns:wpg=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\\" xmlns:mc=\\"http://schemas.openxmlformats.org/markup-compatibility/2006\\" xmlns:wp14=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\\" xmlns:w14=\\"http://schemas.microsoft.com/office/word/2010/wordml\\" xmlns:w15=\\"http://schemas.microsoft.com/office/word/2012/wordml\\" mc:Ignorable=\\"w14 wp14\\"><w:body><w:p w14:paraId=\\"00000001\\"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:t xml:space=\\"preserve\\">Hello </w:t></w:r><w:r><w:rPr><w:shd w:val=\\"clear\\" w:color=\\"auto\\" w:fill=\\"FFC000\\" /></w:rPr><w:t xml:space=\\"preserve\\">World!</w:t></w:r></w:p><w:sectPr><w:pgSz w:w=\\"11906\\" w:h=\\"16838\\" /><w:pgMar w:top=\\"1985\\" w:right=\\"1701\\" w:bottom=\\"1701\\" w:left=\\"1701\\" w:header=\\"851\\" w:footer=\\"992\\" w:gutter=\\"0\\" /><w:cols w:space=\\"425\\" w:num=\\"1\\" /></w:sectPr></w:body></w:document>"`;
|
||||
|
||||
exports[`writer should write shd 3`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?><w:numbering xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\" xmlns:w=\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\"><w:abstractNum w:abstractNumId=\\"1\\"><w:lvl w:ilvl=\\"0\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%1.\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"420\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"1\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"(%2)\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"840\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"2\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimalEnclosedCircle\\" /><w:lvlText w:val=\\"%3\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"1260\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"3\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%4.\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"1680\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"4\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"(%5)\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"2100\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"5\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimalEnclosedCircle\\" /><w:lvlText w:val=\\"%6\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"2520\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"6\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%7.\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"2940\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"7\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"(%8)\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"3360\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"8\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimalEnclosedCircle\\" /><w:lvlText w:val=\\"%9\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"3780\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl></w:abstractNum><w:num w:numId=\\"1\\"><w:abstractNumId w:val=\\"1\\" /></w:num></w:numbering>"`;
|
||||
|
||||
exports[`writer should write strike 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><Relationships xmlns=\\"http://schemas.openxmlformats.org/package/2006/relationships\\"><Relationship Id=\\"rId1\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\\" Target=\\"styles.xml\\" /><Relationship Id=\\"rId2\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\\" Target=\\"fontTable.xml\\" /><Relationship Id=\\"rId3\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\\" Target=\\"settings.xml\\" /><Relationship Id=\\"rId5\\" Type=\\"http://schemas.microsoft.com/office/2011/relationships/commentsExtended\\" Target=\\"commentsExtended.xml\\" /></Relationships>"`;
|
||||
|
||||
exports[`writer should write strike 2`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?><w:document xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\" xmlns:w=\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\" xmlns:w10=\\"urn:schemas-microsoft-com:office:word\\" xmlns:wp=\\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\\" xmlns:wps=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\\" xmlns:wpg=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\\" xmlns:mc=\\"http://schemas.openxmlformats.org/markup-compatibility/2006\\" xmlns:wp14=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\\" xmlns:w14=\\"http://schemas.microsoft.com/office/word/2010/wordml\\" xmlns:w15=\\"http://schemas.microsoft.com/office/word/2012/wordml\\" mc:Ignorable=\\"w14 wp14\\"><w:body><w:p w14:paraId=\\"00000001\\"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr><w:strike /></w:rPr><w:t xml:space=\\"preserve\\">Hello world!!</w:t></w:r></w:p><w:sectPr><w:pgSz w:w=\\"11906\\" w:h=\\"16838\\" /><w:pgMar w:top=\\"1985\\" w:right=\\"1701\\" w:bottom=\\"1701\\" w:left=\\"1701\\" w:header=\\"851\\" w:footer=\\"992\\" w:gutter=\\"0\\" /><w:cols w:space=\\"425\\" w:num=\\"1\\" /></w:sectPr></w:body></w:document>"`;
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue