fix: table cell height adaptation #162
This commit is contained in:
@@ -795,8 +795,8 @@ export class Draw {
|
|||||||
td.rowList = rowList
|
td.rowList = rowList
|
||||||
// 移除缩放导致的行高变化-渲染时会进行缩放调整
|
// 移除缩放导致的行高变化-渲染时会进行缩放调整
|
||||||
const curTdHeight = (rowHeight + tdGap) / scale
|
const curTdHeight = (rowHeight + tdGap) / scale
|
||||||
|
// 内容高度大于当前单元格高度需增加
|
||||||
if (td.height! < curTdHeight) {
|
if (td.height! < curTdHeight) {
|
||||||
// 内容高度大于当前单元格高度需增加
|
|
||||||
const extraHeight = curTdHeight - td.height!
|
const extraHeight = curTdHeight - td.height!
|
||||||
const changeTr = trList[t + td.rowspan - 1]
|
const changeTr = trList[t + td.rowspan - 1]
|
||||||
changeTr.height += extraHeight
|
changeTr.height += extraHeight
|
||||||
@@ -804,6 +804,45 @@ export class Draw {
|
|||||||
changeTd.height! += extraHeight
|
changeTd.height! += extraHeight
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
// 当前单元格最小高度及真实高度(包含跨列)
|
||||||
|
let curTdMinHeight = 0
|
||||||
|
let curTdRealHeight = 0
|
||||||
|
let i = 0
|
||||||
|
while (i < td.rowspan) {
|
||||||
|
const curTr = trList[i + t]
|
||||||
|
curTdMinHeight += curTr.minHeight!
|
||||||
|
curTdRealHeight += curTr.height!
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
td.realMinHeight = curTdMinHeight
|
||||||
|
td.realHeight = curTdRealHeight
|
||||||
|
td.mainHeight = curTdHeight
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 单元格高度大于实际内容高度需减少
|
||||||
|
const reduceTrList = this.tableParticle.getTrListGroupByCol(trList)
|
||||||
|
for (let t = 0; t < reduceTrList.length; t++) {
|
||||||
|
const tr = reduceTrList[t]
|
||||||
|
let reduceHeight = -1
|
||||||
|
for (let d = 0; d < tr.tdList.length; d++) {
|
||||||
|
const td = tr.tdList[d]
|
||||||
|
const curTdRealHeight = td.realHeight!
|
||||||
|
const curTdHeight = td.mainHeight!
|
||||||
|
const curTdMinHeight = td.realMinHeight!
|
||||||
|
// 获取最大可减少高度
|
||||||
|
const curReduceHeight = curTdHeight < curTdMinHeight
|
||||||
|
? curTdRealHeight - curTdMinHeight
|
||||||
|
: curTdRealHeight - curTdHeight
|
||||||
|
if (!~reduceHeight || curReduceHeight < reduceHeight) {
|
||||||
|
reduceHeight = curReduceHeight
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (reduceHeight > 0) {
|
||||||
|
const changeTr = trList[t]
|
||||||
|
changeTr.height -= reduceHeight
|
||||||
|
changeTr.tdList.forEach(changeTd => {
|
||||||
|
changeTd.height! -= reduceHeight
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 需要重新计算表格内值
|
// 需要重新计算表格内值
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import { ElementType, IElement, TableBorder } from '../../../..'
|
import { ElementType, IElement, TableBorder } from '../../../..'
|
||||||
import { IEditorOption } from '../../../../interface/Editor'
|
import { IEditorOption } from '../../../../interface/Editor'
|
||||||
|
import { ITr } from '../../../../interface/table/Tr'
|
||||||
|
import { deepClone } from '../../../../utils'
|
||||||
import { RangeManager } from '../../../range/RangeManager'
|
import { RangeManager } from '../../../range/RangeManager'
|
||||||
import { Draw } from '../../Draw'
|
import { Draw } from '../../Draw'
|
||||||
|
|
||||||
@@ -22,6 +24,23 @@ export class TableParticle {
|
|||||||
this.options = draw.getOptions()
|
this.options = draw.getOptions()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getTrListGroupByCol(payload: ITr[]): ITr[] {
|
||||||
|
const trList = deepClone(payload)
|
||||||
|
for (let t = 0; t < payload.length; t++) {
|
||||||
|
const tr = trList[t]
|
||||||
|
for (let d = tr.tdList.length - 1; d >= 0; d--) {
|
||||||
|
const td = tr.tdList[d]
|
||||||
|
const { rowspan, rowIndex, colIndex } = td
|
||||||
|
const curRowIndex = rowIndex! + rowspan - 1
|
||||||
|
if (curRowIndex !== d) {
|
||||||
|
const changeTd = tr.tdList.splice(d, 1)[0]
|
||||||
|
trList[curRowIndex].tdList.splice(colIndex!, 0, changeTd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return trList
|
||||||
|
}
|
||||||
|
|
||||||
private _drawBorder(payload: IDrawTableBorderOption) {
|
private _drawBorder(payload: IDrawTableBorderOption) {
|
||||||
const { ctx, startX, startY, width, height, isDrawFullBorder } = payload
|
const { ctx, startX, startY, width, height, isDrawFullBorder } = payload
|
||||||
ctx.beginPath()
|
ctx.beginPath()
|
||||||
|
|||||||
@@ -243,8 +243,15 @@ export class TableTool {
|
|||||||
let isChangeSize = false
|
let isChangeSize = false
|
||||||
// 改变尺寸
|
// 改变尺寸
|
||||||
if (order === TableOrder.ROW) {
|
if (order === TableOrder.ROW) {
|
||||||
element.trList![index].height += dy
|
const tr = element.trList![index]
|
||||||
|
// 最大移动高度-向上移动超出最小高度限定,则减少移动量
|
||||||
|
const { defaultTrMinHeight } = this.options
|
||||||
|
if (dy < 0 && tr.height + dy < defaultTrMinHeight) {
|
||||||
|
dy = defaultTrMinHeight - tr.height
|
||||||
|
}
|
||||||
if (dy) {
|
if (dy) {
|
||||||
|
tr.height += dy
|
||||||
|
tr.minHeight = tr.height
|
||||||
isChangeSize = true
|
isChangeSize = true
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+1
-1
@@ -106,7 +106,7 @@ export default class Editor {
|
|||||||
margins: [100, 120, 100, 120],
|
margins: [100, 120, 100, 120],
|
||||||
pageMode: PageMode.PAGING,
|
pageMode: PageMode.PAGING,
|
||||||
tdPadding: 5,
|
tdPadding: 5,
|
||||||
defaultTdHeight: 40,
|
defaultTrMinHeight: 40,
|
||||||
defaultHyperlinkColor: '#0000FF',
|
defaultHyperlinkColor: '#0000FF',
|
||||||
paperDirection: PaperDirection.VERTICAL,
|
paperDirection: PaperDirection.VERTICAL,
|
||||||
inactiveAlpha: 0.6,
|
inactiveAlpha: 0.6,
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ export interface IEditorOption {
|
|||||||
margins?: IMargin,
|
margins?: IMargin,
|
||||||
pageMode?: PageMode;
|
pageMode?: PageMode;
|
||||||
tdPadding?: number;
|
tdPadding?: number;
|
||||||
defaultTdHeight?: number;
|
defaultTrMinHeight?: number;
|
||||||
defaultHyperlinkColor?: string;
|
defaultHyperlinkColor?: string;
|
||||||
paperDirection?: PaperDirection;
|
paperDirection?: PaperDirection;
|
||||||
inactiveAlpha?: number;
|
inactiveAlpha?: number;
|
||||||
|
|||||||
@@ -19,4 +19,7 @@ export interface ITd {
|
|||||||
rowList?: IRow[];
|
rowList?: IRow[];
|
||||||
positionList?: IElementPosition[];
|
positionList?: IElementPosition[];
|
||||||
verticalAlign?: VerticalAlign;
|
verticalAlign?: VerticalAlign;
|
||||||
|
mainHeight?: number; // 内容 + 内边距高度
|
||||||
|
realHeight?: number; // 真实高度(包含跨列)
|
||||||
|
realMinHeight?: number; // 真实最小高度(包含跨列)
|
||||||
}
|
}
|
||||||
@@ -4,4 +4,5 @@ export interface ITr {
|
|||||||
id?: string;
|
id?: string;
|
||||||
height: number;
|
height: number;
|
||||||
tdList: ITd[];
|
tdList: ITd[];
|
||||||
|
minHeight?: number;
|
||||||
}
|
}
|
||||||
@@ -34,6 +34,9 @@ export function formatElementList(elementList: IElement[], options: IFormatEleme
|
|||||||
const tr = el.trList[t]
|
const tr = el.trList[t]
|
||||||
const trId = getUUID()
|
const trId = getUUID()
|
||||||
tr.id = trId
|
tr.id = trId
|
||||||
|
if (!tr.minHeight || tr.minHeight < editorOptions.defaultTrMinHeight) {
|
||||||
|
tr.minHeight = editorOptions.defaultTrMinHeight
|
||||||
|
}
|
||||||
for (let d = 0; d < tr.tdList.length; d++) {
|
for (let d = 0; d < tr.tdList.length; d++) {
|
||||||
const td = tr.tdList[d]
|
const td = tr.tdList[d]
|
||||||
const tdId = getUUID()
|
const tdId = getUUID()
|
||||||
|
|||||||
Reference in New Issue
Block a user