feat: table td with multiple border types #435

pr675
Hufe921 2 years ago
parent 956035b0e3
commit 1d4987ea67

@ -55,7 +55,7 @@ interface IElement {
rowspan: number; rowspan: number;
verticalAlign?: VerticalAlign; verticalAlign?: VerticalAlign;
backgroundColor?: string; backgroundColor?: string;
borderType?: TdBorder; borderTypes?: TdBorder[];
slashType?: TdSlash; slashType?: TdSlash;
value: IElement[]; value: IElement[];
}[]; }[];

@ -55,7 +55,7 @@ interface IElement {
rowspan: number; rowspan: number;
verticalAlign?: VerticalAlign; verticalAlign?: VerticalAlign;
backgroundColor?: string; backgroundColor?: string;
borderType?: TdBorder; borderTypes?: TdBorder[];
slashType?: TdSlash; slashType?: TdSlash;
value: IElement[]; value: IElement[];
}[]; }[];

@ -1416,12 +1416,24 @@ export class CommandAdapt {
if (!rowCol) return if (!rowCol) return
const tdList = rowCol.flat() const tdList = rowCol.flat()
// 存在则设置边框类型,否则取消设置 // 存在则设置边框类型,否则取消设置
const isSetBorderType = tdList.some(td => td.borderType !== payload) const isSetBorderType = tdList.some(td => !td.borderTypes?.includes(payload))
tdList.forEach(td => { tdList.forEach(td => {
if (!td.borderTypes) {
td.borderTypes = []
}
const borderTypeIndex = td.borderTypes.findIndex(type => type === payload)
if (isSetBorderType) { if (isSetBorderType) {
td.borderType = payload if (!~borderTypeIndex) {
td.borderTypes.push(payload)
}
} else { } else {
delete td.borderType if (~borderTypeIndex) {
td.borderTypes.splice(borderTypeIndex, 1)
}
}
// 不存在边框设置时删除字段
if (!td.borderTypes.length) {
delete td.borderTypes
} }
}) })
const { endIndex } = this.range.getRange() const { endIndex } = this.range.getRange()

@ -179,7 +179,10 @@ export class TableParticle {
this._drawSlash(ctx, td, startX, startY) this._drawSlash(ctx, td, startX, startY)
} }
// 没有设置单元格边框 && 没有设置表格边框则忽略 // 没有设置单元格边框 && 没有设置表格边框则忽略
if (!td.borderType && (isEmptyBorderType || isExternalBorderType)) { if (
!td.borderTypes?.length &&
(isEmptyBorderType || isExternalBorderType)
) {
continue continue
} }
const width = td.width! * scale const width = td.width! * scale
@ -190,22 +193,22 @@ export class TableParticle {
// 绘制线条 // 绘制线条
ctx.beginPath() ctx.beginPath()
// 单元格边框 // 单元格边框
if (td.borderType === TdBorder.TOP) { if (td.borderTypes?.includes(TdBorder.TOP)) {
ctx.moveTo(x - width, y) ctx.moveTo(x - width, y)
ctx.lineTo(x, y) ctx.lineTo(x, y)
ctx.stroke() ctx.stroke()
} }
if (td.borderType === TdBorder.RIGHT) { if (td.borderTypes?.includes(TdBorder.RIGHT)) {
ctx.moveTo(x, y) ctx.moveTo(x, y)
ctx.lineTo(x, y + height) ctx.lineTo(x, y + height)
ctx.stroke() ctx.stroke()
} }
if (td.borderType === TdBorder.BOTTOM) { if (td.borderTypes?.includes(TdBorder.BOTTOM)) {
ctx.moveTo(x, y + height) ctx.moveTo(x, y + height)
ctx.lineTo(x - width, y + height) ctx.lineTo(x - width, y + height)
ctx.stroke() ctx.stroke()
} }
if (td.borderType === TdBorder.LEFT) { if (td.borderTypes?.includes(TdBorder.LEFT)) {
ctx.moveTo(x - width, y) ctx.moveTo(x - width, y)
ctx.lineTo(x - width, y + height) ctx.lineTo(x - width, y + height)
ctx.stroke() ctx.stroke()

@ -70,7 +70,7 @@ export const EDITOR_ELEMENT_ZIP_ATTR: Array<keyof IElement> = [
export const TABLE_TD_ZIP_ATTR: Array<keyof ITd> = [ export const TABLE_TD_ZIP_ATTR: Array<keyof ITd> = [
'verticalAlign', 'verticalAlign',
'backgroundColor', 'backgroundColor',
'borderType' 'borderTypes'
] ]
export const TABLE_CONTEXT_ATTR: Array<keyof IElement> = [ export const TABLE_CONTEXT_ATTR: Array<keyof IElement> = [

@ -21,7 +21,7 @@ export interface ITd {
positionList?: IElementPosition[] positionList?: IElementPosition[]
verticalAlign?: VerticalAlign verticalAlign?: VerticalAlign
backgroundColor?: string backgroundColor?: string
borderType?: TdBorder borderTypes?: TdBorder[]
slashType?: TdSlash slashType?: TdSlash
mainHeight?: number // 内容 + 内边距高度 mainHeight?: number // 内容 + 内边距高度
realHeight?: number // 真实高度(包含跨列) realHeight?: number // 真实高度(包含跨列)

Loading…
Cancel
Save