Merge pull request #103 from amitm02/master
resubmitting bidi and RTL support with lint fixs
This commit is contained in:
14
demo/demo22.js
Normal file
14
demo/demo22.js
Normal file
@ -0,0 +1,14 @@
|
||||
const docx = require('../build');
|
||||
|
||||
var doc = new docx.Document();
|
||||
|
||||
var textRun = new docx.TextRun("שלום עולם").rtl();
|
||||
var paragraph = new docx.Paragraph().bidi();
|
||||
paragraph.addRun(textRun);
|
||||
|
||||
doc.addParagraph(paragraph);
|
||||
|
||||
var exporter = new docx.LocalPacker(doc);
|
||||
exporter.pack('My Document');
|
||||
|
||||
console.log('Document created successfully at project root!');
|
@ -1,7 +1,7 @@
|
||||
// http://officeopenxml.com/WPalignment.php
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
export type AlignmentOptions = "left" | "center" | "right" | "both";
|
||||
export type AlignmentOptions = "start" | "end" | "center" | "both" | "distribute" | "left" | "right";
|
||||
|
||||
export class AlignmentAttributes extends XmlAttributeComponent<{ val: AlignmentOptions }> {
|
||||
protected xmlKeys = { val: "w:val" };
|
||||
|
7
src/file/paragraph/formatting/bidi.ts
Normal file
7
src/file/paragraph/formatting/bidi.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
export class Bidi extends XmlComponent {
|
||||
constructor() {
|
||||
super("w:bidi");
|
||||
}
|
||||
}
|
@ -338,4 +338,14 @@ describe("Paragraph", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("#bidi", () => {
|
||||
it("set paragraph right to left layout", () => {
|
||||
paragraph.bidi();
|
||||
const tree = new Formatter().format(paragraph);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:p": [{ "w:pPr": [{ "w:bidi": [] }] }],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -5,6 +5,7 @@ import { Num } from "file/numbering/num";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { Alignment } from "./formatting/alignment";
|
||||
import { Bidi } from "./formatting/bidi";
|
||||
import { ThematicBreak } from "./formatting/border";
|
||||
import { Indent } from "./formatting/indent";
|
||||
import { KeepLines, KeepNext } from "./formatting/keep";
|
||||
@ -109,6 +110,21 @@ export class Paragraph extends XmlComponent {
|
||||
return this;
|
||||
}
|
||||
|
||||
public start(): Paragraph {
|
||||
this.properties.push(new Alignment("start"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public end(): Paragraph {
|
||||
this.properties.push(new Alignment("end"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public distribute(): Paragraph {
|
||||
this.properties.push(new Alignment("distribute"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public justified(): Paragraph {
|
||||
this.properties.push(new Alignment("both"));
|
||||
return this;
|
||||
@ -200,4 +216,9 @@ export class Paragraph extends XmlComponent {
|
||||
this.root.splice(1, 0, run);
|
||||
return this;
|
||||
}
|
||||
|
||||
public bidi(): Paragraph {
|
||||
this.properties.push(new Bidi());
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@ -123,3 +123,25 @@ export class Size extends XmlComponent {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export class SizeCs extends XmlComponent {
|
||||
constructor(size: number) {
|
||||
super("w:szCs");
|
||||
this.root.push(
|
||||
new Attributes({
|
||||
val: size,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export class RTL extends XmlComponent {
|
||||
constructor() {
|
||||
super("w:rtl");
|
||||
this.root.push(
|
||||
new Attributes({
|
||||
val: true,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -134,7 +134,21 @@ describe("Run", () => {
|
||||
run.size(24);
|
||||
const tree = new Formatter().format(run);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:r": [{ "w:rPr": [{ "w:sz": [{ _attr: { "w:val": 24 } }] }] }],
|
||||
"w:r": [
|
||||
{
|
||||
"w:rPr": [{ "w:sz": [{ _attr: { "w:val": 24 } }] }, { "w:szCs": [{ _attr: { "w:val": 24 } }] }],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("#rtl", () => {
|
||||
it("should set the run to the RTL mode", () => {
|
||||
run.rtl();
|
||||
const tree = new Formatter().format(run);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:r": [{ "w:rPr": [{ "w:rtl": [{ _attr: { "w:val": true } }] }] }],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -1,7 +1,7 @@
|
||||
// http://officeopenxml.com/WPtext.php
|
||||
import { Break } from "./break";
|
||||
import { Caps, SmallCaps } from "./caps";
|
||||
import { Bold, Color, DoubleStrike, Italics, Size, Strike } from "./formatting";
|
||||
import { Bold, Color, DoubleStrike, Italics, RTL, Size, SizeCs, Strike } from "./formatting";
|
||||
import { Begin, End, Page, Separate } from "./page-number";
|
||||
import { RunProperties } from "./properties";
|
||||
import { RunFonts } from "./run-fonts";
|
||||
@ -43,6 +43,12 @@ export class Run extends XmlComponent {
|
||||
|
||||
public size(size: number): Run {
|
||||
this.properties.push(new Size(size));
|
||||
this.properties.push(new SizeCs(size));
|
||||
return this;
|
||||
}
|
||||
|
||||
public rtl(): Run {
|
||||
this.properties.push(new RTL());
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -94,8 +100,8 @@ export class Run extends XmlComponent {
|
||||
return this;
|
||||
}
|
||||
|
||||
public font(fontName: string): Run {
|
||||
this.properties.push(new RunFonts(fontName));
|
||||
public font(fontName: string, hint?: string | undefined): Run {
|
||||
this.properties.push(new RunFonts(fontName, hint));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { Size } from "../../paragraph/run/formatting";
|
||||
import { Size, SizeCs } from "../../paragraph/run/formatting";
|
||||
import { RunProperties } from "../../paragraph/run/properties";
|
||||
import { RunFonts } from "../../paragraph/run/run-fonts";
|
||||
|
||||
@ -14,6 +14,7 @@ export class RunPropertiesDefaults extends XmlComponent {
|
||||
|
||||
public size(size: number): RunPropertiesDefaults {
|
||||
this.properties.push(new Size(size));
|
||||
this.properties.push(new SizeCs(size));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -74,6 +74,7 @@ export class ParagraphStyle extends Style {
|
||||
|
||||
public size(twips: number): ParagraphStyle {
|
||||
this.addRunProperty(new formatting.Size(twips));
|
||||
this.addRunProperty(new formatting.SizeCs(twips));
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -282,6 +283,7 @@ export class CharacterStyle extends Style {
|
||||
|
||||
public size(twips: number): CharacterStyle {
|
||||
this.addRunProperty(new formatting.Size(twips));
|
||||
this.addRunProperty(new formatting.SizeCs(twips));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@ -362,7 +362,7 @@ describe("ParagraphStyle", () => {
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:rPr": [{ "w:sz": [{ _attr: { "w:val": 24 } }] }],
|
||||
"w:rPr": [{ "w:sz": [{ _attr: { "w:val": 24 } }] }, { "w:szCs": [{ _attr: { "w:val": 24 } }] }],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
Reference in New Issue
Block a user