Merge pull request #1738 from dolanmiu/feat/word-wrap
#1529 - Word wrap feature
This commit is contained in:
@ -7,12 +7,31 @@ const doc = new Document({
|
|||||||
sections: [
|
sections: [
|
||||||
{
|
{
|
||||||
children: [
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
wordWrap: true,
|
||||||
|
children: [
|
||||||
|
new TextRun("我今天遛狗去公园"),
|
||||||
|
new TextRun({
|
||||||
|
text: "456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345",
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
new Paragraph({
|
||||||
|
wordWrap: true,
|
||||||
|
children: [
|
||||||
|
new TextRun(
|
||||||
|
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua",
|
||||||
|
),
|
||||||
|
new TextRun({
|
||||||
|
text: "456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345",
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
new Paragraph({
|
new Paragraph({
|
||||||
children: [
|
children: [
|
||||||
new TextRun("我今天遛狗去公园"),
|
new TextRun("我今天遛狗去公园"),
|
||||||
new TextRun({
|
new TextRun({
|
||||||
text: "456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345",
|
text: "456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345",
|
||||||
space: SpaceType.PRESERVE,
|
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
@ -23,7 +42,6 @@ const doc = new Document({
|
|||||||
),
|
),
|
||||||
new TextRun({
|
new TextRun({
|
||||||
text: "456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345",
|
text: "456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345456435234523456435564745673456345",
|
||||||
space: SpaceType.PRESERVE,
|
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
|
20
src/file/paragraph/formatting/word-wrap.spec.ts
Normal file
20
src/file/paragraph/formatting/word-wrap.spec.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { expect } from "chai";
|
||||||
|
|
||||||
|
import { Formatter } from "@export/formatter";
|
||||||
|
|
||||||
|
import { WordWrap } from "./word-wrap";
|
||||||
|
|
||||||
|
describe("WordWrap", () => {
|
||||||
|
it("should create", () => {
|
||||||
|
const wordWrap = new WordWrap();
|
||||||
|
const tree = new Formatter().format(wordWrap);
|
||||||
|
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"w:wordWrap": {
|
||||||
|
_attr: {
|
||||||
|
"w:val": 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
14
src/file/paragraph/formatting/word-wrap.ts
Normal file
14
src/file/paragraph/formatting/word-wrap.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// http://officeopenxml.com/WPalignment.php
|
||||||
|
// http://officeopenxml.com/WPtableAlignment.php
|
||||||
|
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
|
||||||
|
|
||||||
|
export class WordWrapAttributes extends XmlAttributeComponent<{ readonly val: 0 }> {
|
||||||
|
protected readonly xmlKeys = { val: "w:val" };
|
||||||
|
}
|
||||||
|
|
||||||
|
export class WordWrap extends XmlComponent {
|
||||||
|
public constructor() {
|
||||||
|
super("w:wordWrap");
|
||||||
|
this.root.push(new WordWrapAttributes({ val: 0 }));
|
||||||
|
}
|
||||||
|
}
|
@ -123,5 +123,24 @@ describe("ParagraphProperties", () => {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should create with the wordWrap property", () => {
|
||||||
|
const properties = new ParagraphProperties({
|
||||||
|
wordWrap: true,
|
||||||
|
});
|
||||||
|
const tree = new Formatter().format(properties);
|
||||||
|
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"w:pPr": [
|
||||||
|
{
|
||||||
|
"w:wordWrap": {
|
||||||
|
_attr: {
|
||||||
|
"w:val": 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -11,6 +11,7 @@ import { ISpacingProperties, Spacing } from "./formatting/spacing";
|
|||||||
import { HeadingLevel, Style } from "./formatting/style";
|
import { HeadingLevel, Style } from "./formatting/style";
|
||||||
import { TabStop, TabStopDefinition, TabStopType } from "./formatting/tab-stop";
|
import { TabStop, TabStopDefinition, TabStopType } from "./formatting/tab-stop";
|
||||||
import { NumberProperties } from "./formatting/unordered-list";
|
import { NumberProperties } from "./formatting/unordered-list";
|
||||||
|
import { WordWrap } from "./formatting/word-wrap";
|
||||||
import { FrameProperties, IFrameOptions } from "./frame/frame-properties";
|
import { FrameProperties, IFrameOptions } from "./frame/frame-properties";
|
||||||
import { OutlineLevel } from "./links";
|
import { OutlineLevel } from "./links";
|
||||||
|
|
||||||
@ -50,6 +51,7 @@ export interface IParagraphPropertiesOptions extends IParagraphStylePropertiesOp
|
|||||||
readonly widowControl?: boolean;
|
readonly widowControl?: boolean;
|
||||||
readonly frame?: IFrameOptions;
|
readonly frame?: IFrameOptions;
|
||||||
readonly suppressLineNumbers?: boolean;
|
readonly suppressLineNumbers?: boolean;
|
||||||
|
readonly wordWrap?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
|
export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
|
||||||
@ -128,6 +130,10 @@ export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
|
|||||||
this.push(new Shading(options.shading));
|
this.push(new Shading(options.shading));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options.wordWrap) {
|
||||||
|
this.push(new WordWrap());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FIX: Multitab support for Libre Writer
|
* FIX: Multitab support for Libre Writer
|
||||||
* Ensure there is only one w:tabs tag with multiple w:tab
|
* Ensure there is only one w:tabs tag with multiple w:tab
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import { BorderElement, IBorderOptions } from "@file/border";
|
import { BorderElement, IBorderOptions } from "@file/border";
|
||||||
import { IShadingAttributesProperties, Shading } from "@file/shading";
|
import { IShadingAttributesProperties, Shading } from "@file/shading";
|
||||||
import { SpaceType } from "@file/space-type";
|
|
||||||
import { ChangeAttributes, IChangedAttributesProperties } from "@file/track-revision/track-revision";
|
import { ChangeAttributes, IChangedAttributesProperties } from "@file/track-revision/track-revision";
|
||||||
import { HpsMeasureElement, IgnoreIfEmptyXmlComponent, OnOffElement, StringValueElement, XmlComponent } from "@file/xml-components";
|
import { HpsMeasureElement, IgnoreIfEmptyXmlComponent, OnOffElement, StringValueElement, XmlComponent } from "@file/xml-components";
|
||||||
|
|
||||||
@ -46,7 +45,6 @@ export interface IRunStylePropertiesOptions {
|
|||||||
readonly imprint?: boolean;
|
readonly imprint?: boolean;
|
||||||
readonly revision?: IRunPropertiesChangeOptions;
|
readonly revision?: IRunPropertiesChangeOptions;
|
||||||
readonly border?: IBorderOptions;
|
readonly border?: IBorderOptions;
|
||||||
readonly space?: SpaceType;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IRunPropertiesOptions extends IRunStylePropertiesOptions {
|
export interface IRunPropertiesOptions extends IRunStylePropertiesOptions {
|
||||||
|
@ -3,7 +3,6 @@ import { expect } from "chai";
|
|||||||
import { Formatter } from "@export/formatter";
|
import { Formatter } from "@export/formatter";
|
||||||
import { BorderStyle } from "@file/border";
|
import { BorderStyle } from "@file/border";
|
||||||
import { ShadingType } from "@file/shading";
|
import { ShadingType } from "@file/shading";
|
||||||
import { SpaceType } from "@file/space-type";
|
|
||||||
|
|
||||||
import { EmphasisMarkType } from "./emphasis-mark";
|
import { EmphasisMarkType } from "./emphasis-mark";
|
||||||
import { PageNumber, Run } from "./run";
|
import { PageNumber, Run } from "./run";
|
||||||
@ -520,20 +519,4 @@ describe("Run", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("#space", () => {
|
|
||||||
it("should correctly set the border", () => {
|
|
||||||
const run = new Run({
|
|
||||||
space: SpaceType.PRESERVE,
|
|
||||||
});
|
|
||||||
const tree = new Formatter().format(run);
|
|
||||||
expect(tree).to.deep.equal({
|
|
||||||
"w:r": {
|
|
||||||
_attr: {
|
|
||||||
"xml:space": "preserve",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
@ -9,7 +9,6 @@ import { Begin, End, Separate } from "./field";
|
|||||||
import { NumberOfPages, NumberOfPagesSection, Page } from "./page-number";
|
import { NumberOfPages, NumberOfPagesSection, Page } from "./page-number";
|
||||||
import { IRunPropertiesOptions, RunProperties } from "./properties";
|
import { IRunPropertiesOptions, RunProperties } from "./properties";
|
||||||
import { Text } from "./run-components/text";
|
import { Text } from "./run-components/text";
|
||||||
import { TextAttributes } from "./text-attributes";
|
|
||||||
|
|
||||||
export interface IRunOptions extends IRunPropertiesOptions {
|
export interface IRunOptions extends IRunPropertiesOptions {
|
||||||
readonly children?: readonly (Begin | FieldInstruction | Separate | End | PageNumber | FootnoteReferenceRun | string)[];
|
readonly children?: readonly (Begin | FieldInstruction | Separate | End | PageNumber | FootnoteReferenceRun | string)[];
|
||||||
@ -37,10 +36,6 @@ export class Run extends XmlComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.space) {
|
|
||||||
this.root.push(new TextAttributes({ space: options.space }));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.children) {
|
if (options.children) {
|
||||||
for (const child of options.children) {
|
for (const child of options.children) {
|
||||||
if (typeof child === "string") {
|
if (typeof child === "string") {
|
||||||
|
Reference in New Issue
Block a user