Skip to content

Commit

Permalink
style: add keep-with-next flag (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
connium authored May 11, 2019
1 parent 80f4d13 commit 18fb562
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- **style:** add common styles
- **style:** allow page break to be before or after a paragraph, closes [#31](https://github.com/connium/simple-odf/issues/31)
- **style:** add keep-with-next flag
- **docs:** add a realistic example

### Changed
Expand Down
15 changes: 15 additions & 0 deletions src/api/style/IParagraphProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ export interface IParagraphProperties {
*/
getKeepTogether (): boolean;

/**
* TODO
*
* @since 0.9.0
*/
setKeepWithNext (keepWithNext?: boolean): void;

/**
* TODO
*
* @returns {boolean} `true` TODO, `false` otherwise
* @since 0.9.0
*/
getKeepWithNext (): boolean;

/**
* Sets the page break setting of the paragraph.
*
Expand Down
16 changes: 16 additions & 0 deletions src/api/style/ParagraphProperties.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ describe(ParagraphProperties.name, () => {
});
});

describe('keep with next', () => {
it('return false by default', () => {
expect(properties.getKeepWithNext()).toBe(false);
});

it('return previously set state', () => {
properties.setKeepWithNext();

expect(properties.getKeepWithNext()).toBe(true);

properties.setKeepWithNext(false);

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

describe('page break', () => {
it('return None by default', () => {
expect(properties.getPageBreak()).toBe(PageBreak.None);
Expand Down
13 changes: 13 additions & 0 deletions src/api/style/ParagraphProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ import { TabStop } from './TabStop';
const DEFAULT_HORIZONTAL_ALIGNMENT = HorizontalAlignment.Default;
const DEFAULT_PAGE_BREAK = PageBreak.None;
const DEFAULT_KEEP_TOGETHER = false;
const DEFAULT_KEEP_WITH_NEXT = false;

export class ParagraphProperties implements IParagraphProperties {
private horizontalAlignment: HorizontalAlignment;
private pageBreak: PageBreak;
private shouldKeepTogether: boolean;
private shouldKeepWithNext: boolean;
private tabStops: TabStop[] = [];

public constructor () {
this.horizontalAlignment = DEFAULT_HORIZONTAL_ALIGNMENT;
this.pageBreak = DEFAULT_PAGE_BREAK;
this.shouldKeepTogether = DEFAULT_KEEP_TOGETHER;
this.shouldKeepWithNext = DEFAULT_KEEP_WITH_NEXT;
}

/** @inheritdoc */
Expand All @@ -40,6 +43,16 @@ export class ParagraphProperties implements IParagraphProperties {
return this.shouldKeepTogether;
}

/** @inheritdoc */
setKeepWithNext (keepWithNext = true): void {
this.shouldKeepWithNext = keepWithNext;
}

/** @inheritdoc */
getKeepWithNext (): boolean {
return this.shouldKeepWithNext;
}

/** @inheritdoc */
public setPageBreak (pageBreak: PageBreak): void {
this.pageBreak = pageBreak;
Expand Down
12 changes: 12 additions & 0 deletions src/api/style/ParagraphStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ export class ParagraphStyle extends Style implements IParagraphProperties, IText
return this.paragraphProperties.getKeepTogether();
}

/** @inheritdoc */
public setKeepWithNext (keepWithNext = true): ParagraphStyle {
this.paragraphProperties.setKeepWithNext(keepWithNext);

return this;
}

/** @inheritdoc */
public getKeepWithNext (): boolean {
return this.paragraphProperties.getKeepWithNext();
}

/** @inheritdoc */
public setPageBreak (pageBreak: PageBreak): ParagraphStyle {
this.paragraphProperties.setPageBreak(pageBreak);
Expand Down
1 change: 1 addition & 0 deletions src/xml/OdfAttributeName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum OdfAttributeName {
FormatBreakAfter = 'fo:break-after',
FormatBreakBefore = 'fo:break-before',
FormatKeepTogether = 'fo:keep-together',
FormatKeepWithNext = 'fo:keep-with-next',
FormatColor = 'fo:color',
FormatFontSize = 'fo:font-size',
FormatFontStyle = 'fo:font-style',
Expand Down
9 changes: 9 additions & 0 deletions src/xml/office/StylesWriter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ describe(StylesWriter.name, () => {
expect(documentAsString).toMatch(/<style:paragraph-properties fo:keep-together="always"\/>/);
});

it('set keep with next', () => {
testStyle.setKeepWithNext(true);

stylesWriter.write(commonStyles, testDocument, testRoot);
const documentAsString = new XMLSerializer().serializeToString(testDocument);

expect(documentAsString).toMatch(/<style:paragraph-properties fo:keep-with-next="always"\/>/);
});

it('set page break before', () => {
testStyle.setPageBreak(PageBreak.Before);

Expand Down
4 changes: 4 additions & 0 deletions src/xml/office/StylesWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export class StylesWriter {
paragraphPropertiesElement.setAttribute(OdfAttributeName.FormatKeepTogether, 'always');
}

if (style.getKeepWithNext() === true) {
paragraphPropertiesElement.setAttribute(OdfAttributeName.FormatKeepWithNext, 'always');
}

switch (style.getPageBreak()) {
case PageBreak.Before:
paragraphPropertiesElement.setAttribute(OdfAttributeName.FormatBreakBefore, 'page');
Expand Down
10 changes: 9 additions & 1 deletion test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,19 @@ xdescribe('integration', () => {
heading.setStyle(style);
});

it('keep with next', () => {
const style = new ParagraphStyle();
style.setKeepWithNext();

const heading = body.addParagraph('Keep together with next paragraph');
heading.setStyle(style);
});

it('keep together', () => {
const style = new ParagraphStyle();
style.setKeepTogether();

const heading = body.addParagraph('Paragraph Formatting');
const heading = body.addParagraph('Do\nnot\nsplit\nthis\nparagraph');
heading.setStyle(style);
});

Expand Down

0 comments on commit 18fb562

Please sign in to comment.