Merge branch 'master' into feat/h4buli-update
This commit is contained in:
@ -2,3 +2,4 @@ export * from "./formatting";
|
||||
export * from "./paragraph";
|
||||
export * from "./properties";
|
||||
export * from "./run";
|
||||
export * from "./links";
|
||||
|
13
src/file/paragraph/links/hyperlink-attributes.ts
Normal file
13
src/file/paragraph/links/hyperlink-attributes.ts
Normal 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",
|
||||
};
|
||||
}
|
40
src/file/paragraph/links/hyperlink.spec.ts
Normal file
40
src/file/paragraph/links/hyperlink.spec.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
});
|
21
src/file/paragraph/links/hyperlink.ts
Normal file
21
src/file/paragraph/links/hyperlink.ts
Normal 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"));
|
||||
}
|
||||
}
|
1
src/file/paragraph/links/index.ts
Normal file
1
src/file/paragraph/links/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from "./hyperlink";
|
@ -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);
|
||||
|
Reference in New Issue
Block a user