Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1507 - Add scale for run #1761

Merged
merged 1 commit into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"check-coverage": true,
"statements": 99.73,
"branches": 98.32,
"functions": 99.83,
"statements": 99.79,
"branches": 98.41,
"functions": 100,
"lines": 99.73,
"include": [
"src/**/*.ts"
Expand Down
12 changes: 12 additions & 0 deletions demo/2-declaritive-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ const doc = new Document({
new TextRun({
text: " Another Hello World",
}),
new TextRun({
scale: 50,
text: " Scaled text",
}),
],
}),
new Paragraph({
scale: 200,
children: [
new TextRun({
text: "Scaled paragraph",
}),
],
}),
],
Expand Down
Empty file.
1 change: 1 addition & 0 deletions src/file/paragraph/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface IParagraphPropertiesOptions extends IParagraphStylePropertiesOp
readonly frame?: IFrameOptions;
readonly suppressLineNumbers?: boolean;
readonly wordWrap?: boolean;
readonly scale?: number;
}

export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
Expand Down
14 changes: 13 additions & 1 deletion src/file/paragraph/run/properties.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { BorderElement, IBorderOptions } from "@file/border";
import { IShadingAttributesProperties, Shading } from "@file/shading";
import { ChangeAttributes, IChangedAttributesProperties } from "@file/track-revision/track-revision";
import { HpsMeasureElement, IgnoreIfEmptyXmlComponent, OnOffElement, StringValueElement, XmlComponent } from "@file/xml-components";
import {
HpsMeasureElement,
IgnoreIfEmptyXmlComponent,
NumberValueElement,
OnOffElement,
StringValueElement,
XmlComponent,
} from "@file/xml-components";

import { EmphasisMark, EmphasisMarkType } from "./emphasis-mark";
import { CharacterSpacing, Color, Highlight, HighlightComplexScript } from "./formatting";
Expand Down Expand Up @@ -47,6 +54,7 @@ export interface IRunStylePropertiesOptions {
readonly border?: IBorderOptions;
readonly vanish?: boolean;
readonly specVanish?: boolean;
readonly scale?: number;
}

export interface IRunPropertiesOptions extends IRunStylePropertiesOptions {
Expand Down Expand Up @@ -228,6 +236,10 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent {
// https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_specVanish_topic_ID0EIE1O.html
this.push(new OnOffElement("w:specVanish", options.vanish));
}

if (options.scale !== undefined) {
this.push(new NumberValueElement("w:w", options.scale));
}
}

public push(item: XmlComponent): void {
Expand Down
24 changes: 24 additions & 0 deletions src/file/paragraph/run/run.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,5 +556,29 @@ describe("Run", () => {
],
});
});

describe("#scale", () => {
it("should correctly set the border", () => {
const run = new Run({
scale: 200,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:w": {
_attr: {
"w:val": 200,
},
},
},
],
},
],
});
});
});
});
});
12 changes: 0 additions & 12 deletions src/file/table-of-contents/alias.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/file/table-of-contents/sdt-properties.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// http://www.datypic.com/sc/ooxml/e-w_sdtPr-1.html
import { XmlComponent } from "@file/xml-components";
import { Alias } from "./alias";
import { StringValueElement, XmlComponent } from "@file/xml-components";

export class StructuredDocumentTagProperties extends XmlComponent {
public constructor(alias: string) {
super("w:sdtPr");
this.root.push(new Alias(alias));
this.root.push(new StringValueElement("w:alias", alias));
}
}
1 change: 0 additions & 1 deletion src/file/table/table-properties/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export * from "./table-properties";
export * from "./table-float-properties";
export * from "./table-layout";
export * from "./table-borders";
export * from "./table-overlap";
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import { expect } from "chai";

import { Formatter } from "@export/formatter";

import { RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType, TableFloatProperties } from "./table-float-properties";
import { OverlapType } from "./table-overlap";
import {
OverlapType,
RelativeHorizontalPosition,
RelativeVerticalPosition,
TableAnchorType,
TableFloatProperties,
} from "./table-float-properties";

describe("Table Float Properties", () => {
describe("#constructor", () => {
Expand Down
20 changes: 16 additions & 4 deletions src/file/table/table-properties/table-float-properties.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
import { StringEnumValueElement, XmlAttributeComponent, XmlComponent } from "@file/xml-components";
import { signedTwipsMeasureValue, twipsMeasureValue } from "@util/values";

import { OverlapType, TableOverlap } from "./table-overlap";

export enum TableAnchorType {
MARGIN = "margin",
PAGE = "page",
Expand All @@ -26,6 +24,17 @@ export enum RelativeVerticalPosition {
TOP = "top",
}

// <xsd:simpleType name="ST_TblOverlap">
// <xsd:restriction base="xsd:string">
// <xsd:enumeration value="never"/>
// <xsd:enumeration value="overlap"/>
// </xsd:restriction>
// </xsd:simpleType>
export enum OverlapType {
NEVER = "never",
OVERLAP = "overlap",
}

export interface ITableFloatOptions {
/* cSpell:disable */
/**
Expand Down Expand Up @@ -171,7 +180,10 @@ export class TableFloatProperties extends XmlComponent {
);

if (options.overlap) {
this.root.push(new TableOverlap(options.overlap));
// <xsd:complexType name="CT_TblOverlap">
// <xsd:attribute name="val" type="ST_TblOverlap" use="required"/>
// </xsd:complexType>
this.root.push(new StringEnumValueElement<OverlapType>("w:tblOverlap", options.overlap));
}
}
}
22 changes: 0 additions & 22 deletions src/file/table/table-properties/table-overlap.spec.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/file/table/table-properties/table-overlap.ts

This file was deleted.

26 changes: 25 additions & 1 deletion src/file/xml-components/imported-xml-component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Element, xml2js } from "xml-js";

import { EMPTY_OBJECT } from "@file/xml-components";

import { convertToXmlComponent, ImportedXmlComponent } from "./imported-xml-component";
import { convertToXmlComponent, ImportedRootElementAttributes, ImportedXmlComponent } from "./imported-xml-component";
import { IContext } from "./base";

const xmlString = `
Expand Down Expand Up @@ -90,5 +90,29 @@ describe("ImportedXmlComponent", () => {
const converted = convertToXmlComponent(xmlObj);
expect(converted).to.deep.equal(convertedXmlElement);
});

it("should return undefined if xml type is invalid", () => {
const xmlObj = { type: "invalid" } as Element;
const converted = convertToXmlComponent(xmlObj);
expect(converted).to.equal(undefined);
});
});
});

describe("ImportedRootElementAttributes", () => {
let attributes: ImportedRootElementAttributes;

beforeEach(() => {
attributes = new ImportedRootElementAttributes({});
});

describe("#prepForXml()", () => {
it("should work", () => {
// tslint:disable-next-line: no-object-literal-type-assertion
const converted = attributes.prepForXml({} as IContext);
expect(converted).to.deep.equal({
_attr: {},
});
});
});
});
7 changes: 7 additions & 0 deletions src/file/xml-components/simple-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export class NumberValueElement extends XmlComponent {
}
}

export class StringEnumValueElement<T extends string> extends XmlComponent {
public constructor(name: string, val: T) {
super(name);
this.root.push(new Attributes({ val }));
}
}

// Simple nodes containing text.
//
// new StringContainer("hello", "world")
Expand Down