Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added keep-together paragraph style #45

Merged
merged 5 commits into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ style1.setTypeface(simpleOdf.Typeface.Bold);
// paragraph formatting
style1.setHorizontalAlignment(simpleOdf.HorizontalAlignment.Center);
style1.setPageBreakBefore();
style1.setKeepTogether();
p1.setStyle(style1);
// font usage
document.declareFont("Open Sans", "Open Sans", simpleOdf.FontPitch.Variable);
Expand Down
1 change: 1 addition & 0 deletions src/OdfAttributeName.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum OdfAttributeName {
FormatBreakBefore = "fo:break-before",
FormatKeepTogether = "fo:keep-together",
FormatColor = "fo:color",
FormatFontSize = "fo:font-size",
FormatFontStyle = "fo:font-style",
Expand Down
7 changes: 7 additions & 0 deletions src/style/IParagraphProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export interface IParagraphProperties {
*/
setPageBreakBefore(): void;

/**
* Keeps paragraph lines on the same page (page break before paragraph if necessary).
*
* @since 0.6.0
*/
setKeepTogether(keepTogether?: boolean): void;

/**
* Adds a new tab stop to this style.
* If a tab stop at the same position already exists, the new tab stop will not be added.
Expand Down
35 changes: 29 additions & 6 deletions src/style/ParagraphProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { TabStopType } from "./TabStopType";

const DEFAULT_HORIZONTAL_ALIGNMENT = HorizontalAlignment.Default;
const DEFAULT_PAGE_BREAK = false;
const DEFAULT_KEEP_TOGETHER = false;

/**
* This class represents the style of a paragraph.
Expand All @@ -16,6 +17,7 @@ const DEFAULT_PAGE_BREAK = false;
export class ParagraphProperties implements IParagraphProperties {
private horizontalAlignment: HorizontalAlignment;
private shouldBreakPageBefore: boolean;
private shouldKeepTogether: boolean;
private tabStops: TabStop[] = [];

/**
Expand All @@ -24,6 +26,7 @@ export class ParagraphProperties implements IParagraphProperties {
public constructor() {
this.horizontalAlignment = DEFAULT_HORIZONTAL_ALIGNMENT;
this.shouldBreakPageBefore = DEFAULT_PAGE_BREAK;
this.shouldKeepTogether = DEFAULT_KEEP_TOGETHER;
}

/** @inheritDoc */
Expand All @@ -41,6 +44,11 @@ export class ParagraphProperties implements IParagraphProperties {
this.shouldBreakPageBefore = true;
}

/** @inheritDoc */
public setKeepTogether(keepTogether: boolean = true): void {
this.shouldKeepTogether = keepTogether;
}

/** @inheritDoc */
public addTabStop(position: number, type: TabStopType): TabStop | undefined;

Expand Down Expand Up @@ -80,9 +88,12 @@ export class ParagraphProperties implements IParagraphProperties {
* @since 0.1.0
*/
public isDefault(): boolean {
return this.horizontalAlignment === DEFAULT_HORIZONTAL_ALIGNMENT
&& this.shouldBreakPageBefore === DEFAULT_PAGE_BREAK
&& this.tabStops.length === 0;
return (
this.horizontalAlignment === DEFAULT_HORIZONTAL_ALIGNMENT &&
this.shouldBreakPageBefore === DEFAULT_PAGE_BREAK &&
this.shouldKeepTogether === DEFAULT_KEEP_TOGETHER &&
this.tabStops.length === 0
);
}

/**
Expand All @@ -102,13 +113,14 @@ export class ParagraphProperties implements IParagraphProperties {

this.setHorizontalAlignmentAttribute(paragraphPropertiesElement);
this.setPageBreakAttribute(paragraphPropertiesElement);
this.setKeepTogetherAttribute(paragraphPropertiesElement);
this.setTabStopElements(document, paragraphPropertiesElement);
}

/**
* Sets the `text-align` attribute if an horizontal alignment is set.
*
* @param {Element} textPropertiesElement The element which will take the attribute
* @param {Element} paragraphPropertiesElement The element which will take the attribute
*/
private setHorizontalAlignmentAttribute(paragraphPropertiesElement: Element): void {
if (this.horizontalAlignment !== HorizontalAlignment.Default) {
Expand All @@ -119,19 +131,30 @@ export class ParagraphProperties implements IParagraphProperties {
/**
* Sets the page break attribute if a page break is set.
*
* @param {Element} textPropertiesElement The element which will take the attribute
* @param {Element} paragraphPropertiesElement The element which will take the attribute
*/
private setPageBreakAttribute(paragraphPropertiesElement: Element): void {
if (this.shouldBreakPageBefore === true) {
paragraphPropertiesElement.setAttribute(OdfAttributeName.FormatBreakBefore, "page");
}
}

/**
* Sets the keep together attribute if paragraph marked as keep together.
*
* @param {Element} paragraphPropertiesElement The element which will take the attribute
*/
private setKeepTogetherAttribute(paragraphPropertiesElement: Element): void {
if (this.shouldKeepTogether === true) {
paragraphPropertiesElement.setAttribute(OdfAttributeName.FormatKeepTogether, "always");
}
}

/**
* Adds the `tab-stops` element and the tab stop definitions if any tab stop is set.
*
* @param {Document} document The XML document
* @param {Element} textPropertiesElement The element which will be used as parent
* @param {Element} paragraphPropertiesElement The element which will be used as parent
*/
private setTabStopElements(document: Document, paragraphPropertiesElement: Element): void {
if (this.tabStops.length === 0) {
Expand Down
6 changes: 6 additions & 0 deletions src/style/ParagraphStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ export class ParagraphStyle implements IParagraphStyle {
return this.paragraphProperties.setPageBreakBefore();
}

/** @inheritDoc */
public setKeepTogether(keepTogether: boolean = true): void {
return this.paragraphProperties.setKeepTogether(keepTogether);
}

/** @inheritDoc */
public addTabStop(position: number, type: TabStopType): TabStop | undefined;
/** @inheritDoc */
Expand Down Expand Up @@ -143,6 +148,7 @@ export class ParagraphStyle implements IParagraphStyle {
// paragraph properties
hash.update(this.paragraphProperties.getHorizontalAlignment());
hash.update((this.paragraphProperties as any).shouldBreakPageBefore ? "pb" : "");
hash.update((this.paragraphProperties as any).shouldKeepTogether ? "kt" : "");
this.paragraphProperties.getTabStops().forEach((tabStop: TabStop) => {
hash.update(`${tabStop.getPosition()}${tabStop.getType()}`);
});
Expand Down
6 changes: 6 additions & 0 deletions test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ xdescribe("integration", () => {
heading.getStyle().setPageBreakBefore();
});

it("keep together", () => {
const heading = document.addParagraph("Paragraph Formatting");
heading.setStyle(new ParagraphStyle());
heading.getStyle().setKeepTogether();
});

it("align text", () => {
const paragraph = document.addParagraph("Some centered text");
paragraph.setStyle(new ParagraphStyle());
Expand Down
8 changes: 8 additions & 0 deletions test/style/ParagraphProperties.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ describe(ParagraphProperties.name, () => {

// setPageBreakBefore

// setKeepTogether

describe("#addTabStop", () => {
it("add new item to the list of tab stops by position and return the added tab stop", () => {
const testTabStop = new TabStop(23);
Expand Down Expand Up @@ -118,6 +120,12 @@ describe(ParagraphProperties.name, () => {
expect(properties.isDefault()).toBe(false);
});

it("return false if keep together was set", () => {
properties.setKeepTogether();

expect(properties.isDefault()).toBe(false);
});

it("return false if a tab stop was set", () => {
properties.addTabStop(23, TabStopType.Right);

Expand Down