Skip to content

Commit

Permalink
feat: table element can be merged after paging #41
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Mar 15, 2024
1 parent 0523fc2 commit a0c8f56
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
53 changes: 46 additions & 7 deletions src/editor/core/draw/Draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,25 @@ export class Draw {
} else if (element.type === ElementType.TABLE) {
const tdPaddingWidth = tdPadding[1] + tdPadding[3]
const tdPaddingHeight = tdPadding[0] + tdPadding[2]
// 表格分页处理进度:https://github.com/Hufe921/canvas-editor/issues/41
// 查看后续表格是否属于同一个源表格-存在即合并
if (element.pagingId) {
let tableIndex = i + 1
let combineCount = 0
while (tableIndex < elementList.length) {
const nextElement = elementList[tableIndex]
if (nextElement.pagingId === element.pagingId) {
element.trList!.push(...nextElement.trList!)
tableIndex++
combineCount++
} else {
break
}
}
if (combineCount) {
elementList.splice(i + 1, combineCount)
}
}
// 计算表格行列
this.tableParticle.computeRowColInfo(element)
// 计算表格内元素信息
Expand Down Expand Up @@ -1248,6 +1267,7 @@ export class Draw {
let deleteStart = 0
let deleteCount = 0
let preTrHeight = 0
// 大于一行时再拆分避免循环
if (trList.length > 1) {
for (let r = 0; r < trList.length; r++) {
const tr = trList[r]
Expand All @@ -1274,23 +1294,42 @@ export class Draw {
(pre, cur) => pre + cur.height,
0
)
const pagingId = getUUID()
element.pagingId = pagingId
element.height -= cloneTrHeight
metrics.height -= cloneTrHeight
metrics.boundingBoxDescent -= cloneTrHeight
// 追加拆分表格
const cloneElement = deepClone(element)
cloneElement.pagingId = pagingId
cloneElement.trList = cloneTrList
cloneElement.id = getUUID()
this.spliceElementList(elementList, i + 1, 0, cloneElement)
// 换页的是当前行则改变上下文
const positionContext = this.position.getPositionContext()
if (
positionContext.isTable &&
positionContext.trIndex === deleteStart
) {
positionContext.index! += 1
positionContext.trIndex = 0
this.position.setPositionContext(positionContext)
if (positionContext.isTable) {
// 查找光标所在表格索引(根据trId搜索)
let newPositionContextIndex = -1
let newPositionContextTrIndex = -1
let tableIndex = i
while (tableIndex < elementList.length) {
const curElement = elementList[tableIndex]
if (curElement.pagingId !== pagingId) break
const trIndex = curElement.trList!.findIndex(
r => r.id === positionContext.trId
)
if (~trIndex) {
newPositionContextIndex = tableIndex
newPositionContextTrIndex = trIndex
break
}
tableIndex++
}
if (~newPositionContextIndex) {
positionContext.index = newPositionContextIndex
positionContext.trIndex = newPositionContextTrIndex
this.position.setPositionContext(positionContext)
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/editor/interface/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export interface ITableElement {
trId?: string
tableId?: string
conceptId?: string
pagingId?: string // 用于区分拆分的表格同属一个源表格
}

export type ITable = ITableAttr & ITableElement
Expand Down
17 changes: 17 additions & 0 deletions src/editor/utils/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,23 @@ export function zipElementList(payload: IElement[]): IElement[] {
listElement.valueList = zipElementList(valueList)
element = listElement
} else if (element.type === ElementType.TABLE) {
// 分页表格先进行合并
if (element.pagingId) {
let tableIndex = e + 1
let combineCount = 0
while (tableIndex < elementList.length) {
const nextElement = elementList[tableIndex]
if (nextElement.pagingId === element.pagingId) {
element.height! += nextElement.height!
element.trList!.push(...nextElement.trList!)
tableIndex++
combineCount++
} else {
break
}
}
e += combineCount
}
if (element.trList) {
for (let t = 0; t < element.trList.length; t++) {
const tr = element.trList[t]
Expand Down

0 comments on commit a0c8f56

Please sign in to comment.