feat: copy table structure and data #516
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
import { ElementType } from '../../../dataset/enum/Element'
|
||||
import { IElement } from '../../../interface/Element'
|
||||
import { ITr } from '../../../interface/table/Tr'
|
||||
import { writeElementList } from '../../../utils/clipboard'
|
||||
import { zipElementList } from '../../../utils/element'
|
||||
import { CanvasEvent } from '../CanvasEvent'
|
||||
|
||||
export function copy(host: CanvasEvent) {
|
||||
@@ -11,9 +15,48 @@ export function copy(host: CanvasEvent) {
|
||||
}
|
||||
const rangeManager = draw.getRange()
|
||||
// 光标闭合时复制整行
|
||||
const copyElementList = rangeManager.getIsCollapsed()
|
||||
? rangeManager.getRangeRowElementList()
|
||||
: rangeManager.getSelectionElementList()
|
||||
let copyElementList: IElement[] | null = null
|
||||
const range = rangeManager.getRange()
|
||||
if (range.isCrossRowCol) {
|
||||
// 原始表格信息
|
||||
const tableElement = rangeManager.getRangeTableElement()
|
||||
if (!tableElement) return
|
||||
// 选区行列信息
|
||||
const rowCol = draw.getTableParticle().getRangeRowCol()
|
||||
if (!rowCol) return
|
||||
// 构造表格
|
||||
const copyTableElement: IElement = {
|
||||
type: ElementType.TABLE,
|
||||
value: '',
|
||||
colgroup: [],
|
||||
trList: []
|
||||
}
|
||||
const firstRow = rowCol[0]
|
||||
const colStartIndex = firstRow[0].colIndex!
|
||||
const lastCol = firstRow[firstRow.length - 1]
|
||||
const colEndIndex = lastCol.colIndex! + lastCol.colspan - 1
|
||||
for (let c = colStartIndex; c <= colEndIndex; c++) {
|
||||
copyTableElement.colgroup!.push(tableElement.colgroup![c])
|
||||
}
|
||||
for (let r = 0; r < rowCol.length; r++) {
|
||||
const row = rowCol[r]
|
||||
const tr = tableElement.trList![row[0].rowIndex!]
|
||||
const coptTr: ITr = {
|
||||
tdList: [],
|
||||
height: tr.height,
|
||||
minHeight: tr.minHeight
|
||||
}
|
||||
for (let c = 0; c < row.length; c++) {
|
||||
coptTr.tdList.push(row[c])
|
||||
}
|
||||
copyTableElement.trList!.push(coptTr)
|
||||
}
|
||||
copyElementList = zipElementList([copyTableElement])
|
||||
} else {
|
||||
copyElementList = rangeManager.getIsCollapsed()
|
||||
? rangeManager.getRangeRowElementList()
|
||||
: rangeManager.getSelectionElementList()
|
||||
}
|
||||
if (!copyElementList?.length) return
|
||||
writeElementList(copyElementList, draw.getOptions())
|
||||
}
|
||||
|
||||
@@ -249,6 +249,14 @@ export class RangeManager {
|
||||
return this.getRangeParagraphInfo()?.elementList || null
|
||||
}
|
||||
|
||||
// 获取选区表格
|
||||
public getRangeTableElement(): IElement | null {
|
||||
const positionContext = this.position.getPositionContext()
|
||||
if (!positionContext.isTable) return null
|
||||
const originalElementList = this.draw.getOriginalElementList()
|
||||
return originalElementList[positionContext.index!]
|
||||
}
|
||||
|
||||
public getIsSelectAll() {
|
||||
const elementList = this.draw.getElementList()
|
||||
const { startIndex, endIndex } = this.range
|
||||
|
||||
Reference in New Issue
Block a user