Skip to content

Commit

Permalink
feat: copy table structure and data #516
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Jun 15, 2024
1 parent 847e23c commit 76c20a6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
49 changes: 46 additions & 3 deletions src/editor/core/event/handlers/copy.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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())
}
8 changes: 8 additions & 0 deletions src/editor/core/range/RangeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 76c20a6

Please sign in to comment.