-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hyperlink to a paragraph, set text and uri of a hyperlink (#6)
- Loading branch information
Showing
10 changed files
with
246 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { OdfAttributeName } from "../OdfAttributeName"; | ||
import { OdfElementName } from "../OdfElementName"; | ||
import { Text } from "./Text"; | ||
|
||
/** | ||
* This class represents a hyperlink in a paragraph. | ||
* | ||
* @since 0.3.0 | ||
*/ | ||
export class Hyperlink extends Text { | ||
/** | ||
* Creates a hyperlink | ||
* | ||
* @param {string} text The text content of the hyperlink | ||
* @param {string} uri The URI of the hyperlink | ||
* @since 0.3.0 | ||
*/ | ||
public constructor(text: string, private uri: string) { | ||
super(text); | ||
} | ||
|
||
/** | ||
* Returns the URI of this hyperlink. | ||
* | ||
* @returns {string} The URI of this hyperlink | ||
* @since 0.3.0 | ||
*/ | ||
public getURI(): string { | ||
return this.uri; | ||
} | ||
|
||
/** | ||
* Sets the URI for this hyperlink. | ||
* | ||
* @param {string} uri The new URI of this hyperlink | ||
* @since 0.3.0 | ||
*/ | ||
public setURI(uri: string): void { | ||
this.uri = uri; | ||
} | ||
|
||
/** @inheritDoc */ | ||
protected toXML(document: Document, parent: Element): void { | ||
const text = this.getText(); | ||
|
||
if (text === undefined || text === "") { | ||
return; | ||
} | ||
|
||
(document.firstChild as Element).setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink"); | ||
|
||
const hyperlink = document.createElement(OdfElementName.TextHyperlink); | ||
hyperlink.setAttribute(OdfAttributeName.XlinkType, "simple"); | ||
hyperlink.setAttribute(OdfAttributeName.XlinkHref, this.uri); | ||
|
||
const textNode = document.createTextNode(text); | ||
hyperlink.appendChild(textNode); | ||
|
||
parent.appendChild(hyperlink); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { OdfElement } from "../OdfElement"; | ||
import { OdfElementName } from "../OdfElementName"; | ||
|
||
/** | ||
* This class represents text in a paragraph. | ||
* | ||
* @since 0.3.0 | ||
*/ | ||
export class Text extends OdfElement { | ||
/** | ||
* Creates a text | ||
* | ||
* @param {string} text The text content | ||
* @since 0.3.0 | ||
*/ | ||
public constructor(private text: string) { | ||
super(); | ||
} | ||
|
||
/** | ||
* Returns the text content. | ||
* | ||
* @returns {string} The text content | ||
* @since 0.3.0 | ||
*/ | ||
public getText(): string { | ||
return this.text; | ||
} | ||
|
||
/** | ||
* Sets the new text content. | ||
* | ||
* @param {string} text The new text content | ||
* @since 0.3.0 | ||
*/ | ||
public setText(text: string): void { | ||
this.text = text; | ||
} | ||
|
||
/** @inheritDoc */ | ||
protected toXML(document: Document, parent: Element): void { | ||
if (this.text === undefined || this.text === "") { | ||
return; | ||
} | ||
|
||
const lines = this.text.split("\n"); | ||
|
||
for (let i = 0; i < lines.length; i++) { | ||
if (i > 0) { | ||
const lineBreak = document.createElement(OdfElementName.TextLineBreak); | ||
parent.appendChild(lineBreak); | ||
} | ||
|
||
const textNode = document.createTextNode(lines[i]); | ||
parent.appendChild(textNode); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.