Make table internals declarative
This commit is contained in:
@ -6,7 +6,23 @@ class TableCellMarginAttributes extends XmlAttributeComponent<{ readonly type: W
|
||||
protected readonly xmlKeys = { value: "w:w", type: "w:type" };
|
||||
}
|
||||
|
||||
interface IBaseTableCellMarginOptions {
|
||||
readonly value: number;
|
||||
readonly type?: WidthType;
|
||||
}
|
||||
|
||||
class BaseTableCellMargin extends XmlComponent {
|
||||
constructor(rootKey: string, options: IBaseTableCellMarginOptions) {
|
||||
super(rootKey);
|
||||
|
||||
this.root.push(
|
||||
new TableCellMarginAttributes({
|
||||
type: options.type ?? WidthType.DXA,
|
||||
value: options.value,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
public setProperties(value: number, type: WidthType = WidthType.DXA): void {
|
||||
this.root.push(
|
||||
new TableCellMarginAttributes({
|
||||
@ -17,36 +33,31 @@ class BaseTableCellMargin extends XmlComponent {
|
||||
}
|
||||
}
|
||||
|
||||
export interface ITableCellMarginOptions {
|
||||
readonly top?: IBaseTableCellMarginOptions;
|
||||
readonly bottom?: IBaseTableCellMarginOptions;
|
||||
readonly left?: IBaseTableCellMarginOptions;
|
||||
readonly right?: IBaseTableCellMarginOptions;
|
||||
}
|
||||
|
||||
export class TableCellMargin extends IgnoreIfEmptyXmlComponent {
|
||||
constructor() {
|
||||
constructor(options: ITableCellMarginOptions) {
|
||||
super("w:tblCellMar");
|
||||
}
|
||||
|
||||
public addTopMargin(value: number, type: WidthType = WidthType.DXA): void {
|
||||
const top = new BaseTableCellMargin("w:top");
|
||||
if (options.bottom) {
|
||||
this.root.push(new BaseTableCellMargin("w:bottom", options.bottom));
|
||||
}
|
||||
|
||||
top.setProperties(value, type);
|
||||
this.root.push(top);
|
||||
}
|
||||
if (options.top) {
|
||||
this.root.push(new BaseTableCellMargin("w:top", options.top));
|
||||
}
|
||||
|
||||
public addLeftMargin(value: number, type: WidthType = WidthType.DXA): void {
|
||||
const left = new BaseTableCellMargin("w:left");
|
||||
if (options.left) {
|
||||
this.root.push(new BaseTableCellMargin("w:left", options.left));
|
||||
}
|
||||
|
||||
left.setProperties(value, type);
|
||||
this.root.push(left);
|
||||
}
|
||||
|
||||
public addBottomMargin(value: number, type: WidthType = WidthType.DXA): void {
|
||||
const bottom = new BaseTableCellMargin("w:bottom");
|
||||
|
||||
bottom.setProperties(value, type);
|
||||
this.root.push(bottom);
|
||||
}
|
||||
|
||||
public addRightMargin(value: number, type: WidthType = WidthType.DXA): void {
|
||||
const right = new BaseTableCellMargin("w:right");
|
||||
|
||||
right.setProperties(value, type);
|
||||
this.root.push(right);
|
||||
if (options.right) {
|
||||
this.root.push(new BaseTableCellMargin("w:right", options.right));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user