Merge branch 'feat/h4buli-update' of https://github.com/dolanmiu/docx into feat/h4buli-update

This commit is contained in:
Dolan
2018-05-08 02:12:20 +01:00
12 changed files with 150 additions and 7 deletions

View File

@ -8,7 +8,7 @@ import { FooterWrapper } from "./footer-wrapper";
import { HeaderWrapper } from "./header-wrapper";
import { Media } from "./media";
import { Numbering } from "./numbering";
import { Paragraph, PictureRun } from "./paragraph";
import { Hyperlink, Paragraph, PictureRun } from "./paragraph";
import { Relationships } from "./relationships";
import { Styles } from "./styles";
import { ExternalStylesFactory } from "./styles/external-styles-factory";
@ -129,6 +129,18 @@ export class File {
return mediaData;
}
public createHyperlink(link: string, text?: string): Hyperlink {
text = text === undefined ? link : text;
const hyperlink = new Hyperlink(text, this.docRelationships.RelationshipCount);
this.docRelationships.createRelationship(
hyperlink.linkId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
link,
"External",
);
return hyperlink;
}
public get Document(): Document {
return this.document;
}

View File

@ -2,3 +2,4 @@ export * from "./formatting";
export * from "./paragraph";
export * from "./properties";
export * from "./run";
export * from "./links";

View File

@ -0,0 +1,13 @@
import { XmlAttributeComponent } from "file/xml-components";
export interface IHyperlinkAttributesProperties {
id?: string;
history: number;
}
export class HyperlinkAttributes extends XmlAttributeComponent<IHyperlinkAttributesProperties> {
protected xmlKeys = {
id: "r:id",
history: "w:history",
};
}

View File

@ -0,0 +1,40 @@
import { assert, expect } from "chai";
import { Formatter } from "../../../export/formatter";
import { Utility } from "../../../tests/utility";
import { Hyperlink } from "./";
describe("Hyperlink", () => {
let hyperlink: Hyperlink;
beforeEach(() => {
hyperlink = new Hyperlink("https://example.com", 0);
});
describe("#constructor()", () => {
it("should create a hyperlink with correct root key", () => {
const newJson = Utility.jsonify(hyperlink);
assert.equal(newJson.rootKey, "w:hyperlink");
});
it("should create a hyperlink with right attributes", () => {
const newJson = Utility.jsonify(hyperlink);
const attributes = {
id: "rId1",
history: 1,
};
assert.equal(JSON.stringify(newJson.root[0].root), JSON.stringify(attributes));
});
it("should create a hyperlink with a run component", () => {
const tree = new Formatter().format(hyperlink);
const runJson = {
"w:r": [
{ "w:rPr": [{ "w:rStyle": [{ _attr: { "w:val": "Hyperlink" } }] }] },
{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "https://example.com"] },
],
};
expect(tree["w:hyperlink"][1]).to.deep.equal(runJson);
});
});
});

View File

@ -0,0 +1,21 @@
// http://officeopenxml.com/WPhyperlink.php
import { XmlComponent } from "file/xml-components";
import { TextRun } from "../run";
import { HyperlinkAttributes } from "./hyperlink-attributes";
export class Hyperlink extends XmlComponent {
public linkId: number;
constructor(text: string, relationshipsCount: number) {
super("w:hyperlink");
this.linkId = relationshipsCount + 1;
const attributes = new HyperlinkAttributes({
id: `rId${this.linkId}`,
history: 1,
});
this.root.push(attributes);
this.root.push(new TextRun(text).style("Hyperlink"));
}
}

View File

@ -0,0 +1 @@
export * from "./hyperlink";

View File

@ -13,6 +13,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 { Hyperlink } from "./links";
import { ParagraphProperties } from "./properties";
export class Paragraph extends XmlComponent {
@ -32,6 +33,11 @@ export class Paragraph extends XmlComponent {
return this;
}
public addHyperLink(hyperlink: Hyperlink): Paragraph {
this.root.push(hyperlink);
return this;
}
public createTextRun(text: string): TextRun {
const run = new TextRun(text);
this.addRun(run);

View File

@ -4,6 +4,7 @@ export interface IRelationshipAttributesProperties {
id: string;
type: string;
target: string;
targetMode?: string;
}
export class RelationshipAttributes extends XmlAttributeComponent<IRelationshipAttributesProperties> {
@ -11,5 +12,6 @@ export class RelationshipAttributes extends XmlAttributeComponent<IRelationshipA
id: "Id",
type: "Type",
target: "Target",
targetMode: "TargetMode",
};
}

View File

@ -13,10 +13,13 @@ export type RelationshipType =
| "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer"
| "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
| "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"
| "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties";
| "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"
| "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink";
export type TargetModeType = "External";
export class Relationship extends XmlComponent {
constructor(id: string, type: RelationshipType, target: string) {
constructor(id: string, type: RelationshipType, target: string, targetMode?: TargetModeType) {
super("Relationship");
this.root.push(
@ -24,6 +27,7 @@ export class Relationship extends XmlComponent {
id,
type,
target,
targetMode,
}),
);
}

View File

@ -1,6 +1,6 @@
import { XmlComponent } from "file/xml-components";
import { RelationshipsAttributes } from "./attributes";
import { Relationship, RelationshipType } from "./relationship/relationship";
import { Relationship, RelationshipType, TargetModeType } from "./relationship/relationship";
export class Relationships extends XmlComponent {
constructor() {
@ -16,8 +16,8 @@ export class Relationships extends XmlComponent {
this.root.push(relationship);
}
public createRelationship(id: number, type: RelationshipType, target: string): Relationship {
const relationship = new Relationship(`rId${id}`, type, target);
public createRelationship(id: number, type: RelationshipType, target: string, targetMode?: TargetModeType): Relationship {
const relationship = new Relationship(`rId${id}`, type, target, targetMode);
this.addRelationship(relationship);
return relationship;

View File

@ -9,6 +9,7 @@ import {
Heading4Style,
Heading5Style,
Heading6Style,
HyperlinkStyle,
ListParagraph,
TitleStyle,
} from "./style";
@ -62,6 +63,8 @@ export class DefaultStylesFactory {
// listParagraph.addParagraphProperty();
styles.push(listParagraph);
const hyperLinkStyle = new HyperlinkStyle();
styles.push(hyperLinkStyle);
return styles;
}
}

View File

@ -3,7 +3,7 @@ import * as paragraph from "../../paragraph";
import * as formatting from "../../paragraph/run/formatting";
import { RunProperties } from "../../paragraph/run/properties";
import { BasedOn, Name, Next, QuickFormat } from "./components";
import { BasedOn, Name, Next, QuickFormat, UiPriority, UnhideWhenUsed } from "./components";
export interface IStyleAttributes {
type?: string;
@ -249,3 +249,43 @@ export class ListParagraph extends ParagraphStyle {
this.root.push(new QuickFormat());
}
}
export class CharacterStyle extends Style {
private readonly runProperties: RunProperties;
constructor(styleId: string, name?: string) {
super({ type: "character", styleId: styleId }, name);
this.runProperties = new RunProperties();
this.root.push(this.runProperties);
this.root.push(new UiPriority("99"));
this.root.push(new UnhideWhenUsed(""));
}
public basedOn(parentId: string): CharacterStyle {
this.root.push(new BasedOn(parentId));
return this;
}
public addRunProperty(property: XmlComponent): void {
this.runProperties.push(property);
}
public color(color: string): CharacterStyle {
this.addRunProperty(new formatting.Color(color));
return this;
}
public underline(underlineType?: string, color?: string): CharacterStyle {
this.addRunProperty(new formatting.Underline(underlineType, color));
return this;
}
}
export class HyperlinkStyle extends CharacterStyle {
constructor() {
super("Hyperlink", "Hyperlink");
this.basedOn("DefaultParagraphFont")
.color("0563C1")
.underline("single");
}
}