rtl
This commit is contained in:
@ -10,7 +10,7 @@ import { FootNotes } from "./footnotes";
|
||||
import { HeaderWrapper } from "./header-wrapper";
|
||||
import { Media } from "./media";
|
||||
import { Numbering } from "./numbering";
|
||||
import { Hyperlink, Paragraph, PictureRun } from "./paragraph";
|
||||
import { Bookmark, Hyperlink, Paragraph, PictureRun } from "./paragraph";
|
||||
import { Relationships } from "./relationships";
|
||||
import { Styles } from "./styles";
|
||||
import { ExternalStylesFactory } from "./styles/external-styles-factory";
|
||||
@ -157,6 +157,20 @@ export class File {
|
||||
return hyperlink;
|
||||
}
|
||||
|
||||
public createInternalHyperLink(anchor: string, text?: string): Hyperlink {
|
||||
text = text === undefined ? anchor : text;
|
||||
const hyperlink = new Hyperlink(text, this.docRelationships.RelationshipCount, anchor);
|
||||
// NOTE: unlike File#createHyperlink(), since the link is to an internal bookmark
|
||||
// we don't need to create a new relationship.
|
||||
return hyperlink;
|
||||
}
|
||||
|
||||
public createBookmark(name: string, text?: string): Bookmark {
|
||||
text = text === undefined ? name : text;
|
||||
const bookmark = new Bookmark(name, text, this.docRelationships.RelationshipCount);
|
||||
return bookmark;
|
||||
}
|
||||
|
||||
public addSection(sectionPropertiesOptions: SectionPropertiesOptions): void {
|
||||
this.document.Body.addSection(sectionPropertiesOptions);
|
||||
}
|
||||
|
@ -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" };
|
||||
|
@ -4,4 +4,4 @@ export class Bidi extends XmlComponent {
|
||||
constructor() {
|
||||
super("w:bidi");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
23
src/file/paragraph/links/bookmark-attributes.ts
Normal file
23
src/file/paragraph/links/bookmark-attributes.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { XmlAttributeComponent } from "file/xml-components";
|
||||
|
||||
export interface IBookmarkStartAttributesProperties {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export class BookmarkStartAttributes extends XmlAttributeComponent<IBookmarkStartAttributesProperties> {
|
||||
protected xmlKeys = {
|
||||
id: "w:id",
|
||||
name: "w:name",
|
||||
};
|
||||
}
|
||||
|
||||
export interface IBookmarkEndAttributesProperties {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export class BookmarkEndAttributes extends XmlAttributeComponent<IBookmarkEndAttributesProperties> {
|
||||
protected xmlKeys = {
|
||||
id: "w:id",
|
||||
};
|
||||
}
|
42
src/file/paragraph/links/bookmark.spec.ts
Normal file
42
src/file/paragraph/links/bookmark.spec.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { assert } from "chai";
|
||||
|
||||
import { Utility } from "../../../tests/utility";
|
||||
import { Bookmark } from "./";
|
||||
|
||||
describe("Bookmark", () => {
|
||||
let bookmark: Bookmark;
|
||||
|
||||
beforeEach(() => {
|
||||
bookmark = new Bookmark("anchor", "Internal Link", 0);
|
||||
});
|
||||
|
||||
it("should create a bookmark with three root elements", () => {
|
||||
const newJson = Utility.jsonify(bookmark);
|
||||
assert.equal(newJson.rootKey, undefined);
|
||||
assert.equal(newJson.start.rootKey, "w:bookmarkStart");
|
||||
assert.equal(newJson.text.rootKey, "w:r");
|
||||
assert.equal(newJson.end.rootKey, "w:bookmarkEnd");
|
||||
});
|
||||
|
||||
it("should create a bookmark with the correct attributes on the bookmark start element", () => {
|
||||
const newJson = Utility.jsonify(bookmark);
|
||||
const attributes = {
|
||||
name: "anchor",
|
||||
id: "1",
|
||||
};
|
||||
assert.equal(JSON.stringify(newJson.start.root[0].root), JSON.stringify(attributes));
|
||||
});
|
||||
|
||||
it("should create a bookmark with the correct attributes on the text element", () => {
|
||||
const newJson = Utility.jsonify(bookmark);
|
||||
assert.equal(JSON.stringify(newJson.text.root[1].root[1]), JSON.stringify("Internal Link"));
|
||||
});
|
||||
|
||||
it("should create a bookmark with the correct attributes on the bookmark end element", () => {
|
||||
const newJson = Utility.jsonify(bookmark);
|
||||
const attributes = {
|
||||
id: "1",
|
||||
};
|
||||
assert.equal(JSON.stringify(newJson.end.root[0].root), JSON.stringify(attributes));
|
||||
});
|
||||
});
|
54
src/file/paragraph/links/bookmark.ts
Normal file
54
src/file/paragraph/links/bookmark.ts
Normal file
@ -0,0 +1,54 @@
|
||||
// http://officeopenxml.com/WPbookmark.php
|
||||
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { TextRun } from "../run";
|
||||
import { BookmarkEndAttributes, BookmarkStartAttributes } from "./bookmark-attributes";
|
||||
|
||||
export class Bookmark {
|
||||
public linkId: number;
|
||||
|
||||
public readonly start: BookmarkStart;
|
||||
|
||||
public readonly text: TextRun;
|
||||
|
||||
public readonly end: BookmarkEnd;
|
||||
|
||||
constructor(name: string, text: string, relationshipsCount: number) {
|
||||
this.linkId = relationshipsCount + 1;
|
||||
|
||||
this.start = new BookmarkStart(name, this.linkId);
|
||||
this.text = new TextRun(text);
|
||||
this.end = new BookmarkEnd(this.linkId);
|
||||
}
|
||||
}
|
||||
|
||||
export class BookmarkStart extends XmlComponent {
|
||||
public linkId: number;
|
||||
|
||||
constructor(name: string, relationshipsCount: number) {
|
||||
super("w:bookmarkStart");
|
||||
|
||||
this.linkId = relationshipsCount;
|
||||
const id = `${this.linkId}`;
|
||||
const attributes = new BookmarkStartAttributes({
|
||||
name,
|
||||
id,
|
||||
});
|
||||
this.root.push(attributes);
|
||||
}
|
||||
}
|
||||
|
||||
export class BookmarkEnd extends XmlComponent {
|
||||
public linkId: number;
|
||||
|
||||
constructor(relationshipsCount: number) {
|
||||
super("w:bookmarkEnd");
|
||||
|
||||
this.linkId = relationshipsCount;
|
||||
const id = `${this.linkId}`;
|
||||
const attributes = new BookmarkEndAttributes({
|
||||
id,
|
||||
});
|
||||
this.root.push(attributes);
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ import { XmlAttributeComponent } from "file/xml-components";
|
||||
|
||||
export interface IHyperlinkAttributesProperties {
|
||||
id?: string;
|
||||
anchor?: string;
|
||||
history: number;
|
||||
}
|
||||
|
||||
@ -9,5 +10,6 @@ export class HyperlinkAttributes extends XmlAttributeComponent<IHyperlinkAttribu
|
||||
protected xmlKeys = {
|
||||
id: "r:id",
|
||||
history: "w:history",
|
||||
anchor: "w:anchor",
|
||||
};
|
||||
}
|
||||
|
@ -20,8 +20,8 @@ describe("Hyperlink", () => {
|
||||
it("should create a hyperlink with right attributes", () => {
|
||||
const newJson = Utility.jsonify(hyperlink);
|
||||
const attributes = {
|
||||
id: "rId1",
|
||||
history: 1,
|
||||
id: "rId1",
|
||||
};
|
||||
assert.equal(JSON.stringify(newJson.root[0].root), JSON.stringify(attributes));
|
||||
});
|
||||
@ -36,5 +36,20 @@ describe("Hyperlink", () => {
|
||||
};
|
||||
expect(tree["w:hyperlink"][1]).to.deep.equal(runJson);
|
||||
});
|
||||
|
||||
describe("with optional anchor parameter", () => {
|
||||
beforeEach(() => {
|
||||
hyperlink = new Hyperlink("Anchor Text", 0, "anchor");
|
||||
});
|
||||
|
||||
it("should create an internal link with anchor tag", () => {
|
||||
const newJson = Utility.jsonify(hyperlink);
|
||||
const attributes = {
|
||||
history: 1,
|
||||
anchor: "anchor",
|
||||
};
|
||||
assert.equal(JSON.stringify(newJson.root[0].root), JSON.stringify(attributes));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -2,19 +2,27 @@
|
||||
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { TextRun } from "../run";
|
||||
import { HyperlinkAttributes } from "./hyperlink-attributes";
|
||||
import { HyperlinkAttributes, IHyperlinkAttributesProperties } from "./hyperlink-attributes";
|
||||
|
||||
export class Hyperlink extends XmlComponent {
|
||||
public linkId: number;
|
||||
|
||||
constructor(text: string, relationshipsCount: number) {
|
||||
constructor(text: string, relationshipsCount: number, anchor?: string) {
|
||||
super("w:hyperlink");
|
||||
|
||||
this.linkId = relationshipsCount + 1;
|
||||
const attributes = new HyperlinkAttributes({
|
||||
id: `rId${this.linkId}`,
|
||||
|
||||
const props: IHyperlinkAttributesProperties = {
|
||||
history: 1,
|
||||
});
|
||||
};
|
||||
|
||||
if (anchor) {
|
||||
props.anchor = anchor;
|
||||
} else {
|
||||
props.id = `rId${this.linkId}`;
|
||||
}
|
||||
|
||||
const attributes = new HyperlinkAttributes(props);
|
||||
this.root.push(attributes);
|
||||
this.root.push(new TextRun(text).style("Hyperlink"));
|
||||
}
|
||||
|
@ -1 +1,2 @@
|
||||
export * from "./hyperlink";
|
||||
export * from "./bookmark";
|
||||
|
@ -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";
|
||||
@ -13,8 +14,7 @@ import { ISpacingProperties, Spacing } from "./formatting/spacing";
|
||||
import { Style } from "./formatting/style";
|
||||
import { CenterTabStop, LeftTabStop, MaxRightTabStop, RightTabStop } from "./formatting/tab-stop";
|
||||
import { NumberProperties } from "./formatting/unordered-list";
|
||||
import { Bidi} from "./formatting/bidi"
|
||||
import { Hyperlink } from "./links";
|
||||
import { Bookmark, Hyperlink } from "./links";
|
||||
import { ParagraphProperties } from "./properties";
|
||||
import { PictureRun, Run, TextRun } from "./run";
|
||||
|
||||
@ -40,6 +40,14 @@ export class Paragraph extends XmlComponent {
|
||||
return this;
|
||||
}
|
||||
|
||||
public addBookmark(bookmark: Bookmark): Paragraph {
|
||||
// Bookmarks by spec have three components, a start, text, and end
|
||||
this.root.push(bookmark.start);
|
||||
this.root.push(bookmark.text);
|
||||
this.root.push(bookmark.end);
|
||||
return this;
|
||||
}
|
||||
|
||||
public createTextRun(text: string): TextRun {
|
||||
const run = new TextRun(text);
|
||||
this.addRun(run);
|
||||
@ -102,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;
|
||||
|
@ -124,6 +124,17 @@ 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");
|
||||
@ -133,4 +144,4 @@ export class RTL extends XmlComponent {
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -134,7 +134,11 @@ 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 } }] }],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -144,7 +148,7 @@ describe("Run", () => {
|
||||
run.rtl();
|
||||
const tree = new Formatter().format(run);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:r": [{ "w:rPr": [{ "w:rtl": [{ _attr: { "w:val": true } }]}]}],
|
||||
"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, RTL, 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,7 @@ export class Run extends XmlComponent {
|
||||
|
||||
public size(size: number): Run {
|
||||
this.properties.push(new Size(size));
|
||||
this.properties.push(new SizeCs(size));
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -99,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