Files
docx-js/src/file/drawing/anchor/anchor.ts

83 lines
3.4 KiB
TypeScript
Raw Normal View History

// http://officeopenxml.com/drwPicFloating.php
import { IMediaDataDimensions } from "file/media";
import { XmlComponent } from "file/xml-components";
2018-06-09 23:49:01 +01:00
import { IDrawingOptions } from "../drawing";
import { HorizontalPosition, IFloating, SimplePos, VerticalPosition } from "../floating";
2018-06-09 23:49:01 +01:00
import { Graphic } from "../inline/graphic";
2019-01-10 02:10:20 +00:00
import { TextWrappingType, WrapNone, WrapSquare, WrapTight, WrapTopAndBottom } from "../text-wrap";
2018-06-09 23:49:01 +01:00
import { DocProperties } from "./../doc-properties/doc-properties";
import { EffectExtent } from "./../effect-extent/effect-extent";
import { Extent } from "./../extent/extent";
import { GraphicFrameProperties } from "./../graphic-frame/graphic-frame-properties";
import { AnchorAttributes } from "./anchor-attributes";
2018-06-09 23:49:01 +01:00
const defaultOptions: IFloating = {
allowOverlap: true,
behindDocument: false,
lockAnchor: false,
layoutInCell: true,
verticalPosition: {},
horizontalPosition: {},
};
export class Anchor extends XmlComponent {
2018-06-09 23:49:01 +01:00
constructor(referenceId: number, dimensions: IMediaDataDimensions, drawingOptions: IDrawingOptions) {
super("wp:anchor");
const floating = {
2019-01-10 02:10:20 +00:00
margins: {
top: 0,
bottom: 0,
left: 0,
right: 0,
},
...defaultOptions,
...drawingOptions.floating,
};
2019-01-10 02:10:20 +00:00
this.root.push(
new AnchorAttributes({
2019-01-10 02:10:20 +00:00
distT: floating.margins.top || 0,
distB: floating.margins.bottom || 0,
distL: floating.margins.left || 0,
distR: floating.margins.right || 0,
simplePos: "0", // note: word doesn't fully support - so we use 0
allowOverlap: floating.allowOverlap === true ? "1" : "0",
behindDoc: floating.behindDocument === true ? "1" : "0",
locked: floating.lockAnchor === true ? "1" : "0",
layoutInCell: floating.layoutInCell === true ? "1" : "0",
relativeHeight: dimensions.emus.y,
}),
);
this.root.push(new SimplePos());
this.root.push(new HorizontalPosition(floating.horizontalPosition));
this.root.push(new VerticalPosition(floating.verticalPosition));
this.root.push(new Extent(dimensions.emus.x, dimensions.emus.y));
this.root.push(new EffectExtent());
2019-01-10 02:10:20 +00:00
if (drawingOptions.floating !== undefined && drawingOptions.floating.wrap !== undefined) {
switch (drawingOptions.floating.wrap.type) {
case TextWrappingType.SQUARE:
this.root.push(new WrapSquare(drawingOptions.floating.wrap, drawingOptions.floating.margins));
break;
2019-01-10 02:10:20 +00:00
case TextWrappingType.TIGHT:
this.root.push(new WrapTight(drawingOptions.floating.margins));
break;
2019-01-10 02:10:20 +00:00
case TextWrappingType.TOP_AND_BOTTOM:
this.root.push(new WrapTopAndBottom(drawingOptions.floating.margins));
break;
2019-01-10 02:10:20 +00:00
case TextWrappingType.NONE:
default:
this.root.push(new WrapNone());
}
} else {
this.root.push(new WrapNone());
}
this.root.push(new DocProperties());
this.root.push(new GraphicFrameProperties());
this.root.push(new Graphic(referenceId, dimensions.emus.x, dimensions.emus.y));
}
}