feat:add row alignment

This commit is contained in:
Hufe921
2022-10-13 16:40:00 +08:00
parent 039cdba511
commit 758bbdd36a
7 changed files with 43 additions and 8 deletions
+6
View File
@@ -35,6 +35,7 @@ export class Command {
private static left: CommandAdapt['rowFlex']
private static center: CommandAdapt['rowFlex']
private static right: CommandAdapt['rowFlex']
private static alignment: CommandAdapt['rowFlex']
private static rowMargin: CommandAdapt['rowMargin']
private static insertTable: CommandAdapt['insertTable']
private static insertTableTopRow: CommandAdapt['insertTableTopRow']
@@ -104,6 +105,7 @@ export class Command {
Command.left = adapt.rowFlex.bind(adapt)
Command.center = adapt.rowFlex.bind(adapt)
Command.right = adapt.rowFlex.bind(adapt)
Command.alignment = adapt.rowFlex.bind(adapt)
Command.rowMargin = adapt.rowMargin.bind(adapt)
Command.insertTable = adapt.insertTable.bind(adapt)
Command.insertTableTopRow = adapt.insertTableTopRow.bind(adapt)
@@ -254,6 +256,10 @@ export class Command {
return Command.right(RowFlex.RIGHT)
}
public executeAlignment() {
return Command.alignment(RowFlex.ALIGNMENT)
}
public executeRowMargin(payload: number) {
return Command.rowMargin(payload)
}
+18 -7
View File
@@ -744,6 +744,15 @@ export class Draw {
|| curRow.width + metrics.width > innerWidth
|| (i !== 0 && element.value === ZERO)
) {
// 两端对齐
if (preElement?.rowFlex === RowFlex.ALIGNMENT && curRow.width + metrics.width > innerWidth) {
const gap = (innerWidth - curRow.width) / curRow.elementList.length
for (let e = 0; e < curRow.elementList.length; e++) {
const el = curRow.elementList[e]
el.metrics.width += gap
}
curRow.width = innerWidth
}
rowList.push({
width: metrics.width,
height,
@@ -785,13 +794,11 @@ export class Draw {
let index = startIndex
for (let i = 0; i < rowList.length; i++) {
const curRow = rowList[i]
// 计算行偏移量(行居左、居中、居右)
if (curRow.rowFlex && curRow.rowFlex !== RowFlex.LEFT) {
if (curRow.rowFlex === RowFlex.CENTER) {
x += (innerWidth - curRow.width) / 2
} else {
x += innerWidth - curRow.width
}
// 计算行偏移量(行居中、居右)
if (curRow.rowFlex === RowFlex.CENTER) {
x += (innerWidth - curRow.width) / 2
} else if (curRow.rowFlex === RowFlex.RIGHT) {
x += innerWidth - curRow.width
}
// 当前td所在位置
const tablePreX = x
@@ -869,6 +876,10 @@ export class Draw {
this.checkboxParticle.render(ctx, element, x, y + offsetY)
} else if (element.type === ElementType.TAB) {
this._drawRichText(ctx)
} else if (element.rowFlex === RowFlex.ALIGNMENT) {
// 如果是两端对齐,因canvas目前不支持letterSpacing需单独绘制文本
this.textParticle.record(ctx, element, x, y + offsetY)
this._drawRichText(ctx)
} else {
this.textParticle.record(ctx, element, x, y + offsetY)
}