Skip to content

Commit

Permalink
feat: table td with multiple slash types #436
Browse files Browse the repository at this point in the history
Co-authored-by: Hufe921 <huangyunfeihufe@hotmail.com>
  • Loading branch information
baseWalker and Hufe921 authored Feb 21, 2024
1 parent 1d4987e commit 5b52bb8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/en/guide/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ interface IElement {
verticalAlign?: VerticalAlign;
backgroundColor?: string;
borderTypes?: TdBorder[];
slashType?: TdSlash;
slashTypes?: TdSlash[];
value: IElement[];
}[];
}[];
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ interface IElement {
verticalAlign?: VerticalAlign;
backgroundColor?: string;
borderTypes?: TdBorder[];
slashType?: TdSlash;
slashTypes?: TdSlash[];
value: IElement[];
}[];
}[];
Expand Down
24 changes: 20 additions & 4 deletions src/editor/core/command/CommandAdapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,9 @@ export class CommandAdapt {
if (!rowCol) return
const tdList = rowCol.flat()
// 存在则设置边框类型,否则取消设置
const isSetBorderType = tdList.some(td => !td.borderTypes?.includes(payload))
const isSetBorderType = tdList.some(
td => !td.borderTypes?.includes(payload)
)
tdList.forEach(td => {
if (!td.borderTypes) {
td.borderTypes = []
Expand Down Expand Up @@ -1449,12 +1451,26 @@ export class CommandAdapt {
if (!rowCol) return
const tdList = rowCol.flat()
// 存在则设置单元格斜线类型,否则取消设置
const isSetTdSlashType = tdList.some(td => td.slashType !== payload)
const isSetTdSlashType = tdList.some(
td => !td.slashTypes?.includes(payload)
)
tdList.forEach(td => {
if (!td.slashTypes) {
td.slashTypes = []
}
const slashTypeIndex = td.slashTypes.findIndex(type => type === payload)
if (isSetTdSlashType) {
td.slashType = payload
if (!~slashTypeIndex) {
td.slashTypes.push(payload)
}
} else {
delete td.slashType
if (~slashTypeIndex) {
td.slashTypes.splice(slashTypeIndex, 1)
}
}
// 不存在斜线设置时删除字段
if (!td.slashTypes.length) {
delete td.slashTypes
}
})
const { endIndex } = this.range.getRange()
Expand Down
9 changes: 5 additions & 4 deletions src/editor/core/draw/particle/table/TableParticle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ export class TableParticle {
const x = Math.round(td.x! * scale + startX)
const y = Math.round(td.y! * scale + startY)
// 正斜线 /
if (td.slashType === TdSlash.FORWARD) {
if (td.slashTypes?.includes(TdSlash.FORWARD)) {
ctx.moveTo(x + width, y)
ctx.lineTo(x, y + height)
} else {
// 反斜线 \
}
// 反斜线 \
if (td.slashTypes?.includes(TdSlash.BACK)) {
ctx.moveTo(x, y)
ctx.lineTo(x + width, y + height)
}
Expand Down Expand Up @@ -175,7 +176,7 @@ export class TableParticle {
for (let d = 0; d < tr.tdList.length; d++) {
const td = tr.tdList[d]
// 单元格内斜线
if (td.slashType) {
if (td.slashTypes?.length) {
this._drawSlash(ctx, td, startX, startY)
}
// 没有设置单元格边框 && 没有设置表格边框则忽略
Expand Down
3 changes: 2 additions & 1 deletion src/editor/dataset/constant/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export const EDITOR_ELEMENT_ZIP_ATTR: Array<keyof IElement> = [
export const TABLE_TD_ZIP_ATTR: Array<keyof ITd> = [
'verticalAlign',
'backgroundColor',
'borderTypes'
'borderTypes',
'slashTypes'
]

export const TABLE_CONTEXT_ATTR: Array<keyof IElement> = [
Expand Down
2 changes: 1 addition & 1 deletion src/editor/interface/table/Td.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface ITd {
verticalAlign?: VerticalAlign
backgroundColor?: string
borderTypes?: TdBorder[]
slashType?: TdSlash
slashTypes?: TdSlash[]
mainHeight?: number // 内容 + 内边距高度
realHeight?: number // 真实高度(包含跨列)
realMinHeight?: number // 真实最小高度(包含跨列)
Expand Down

0 comments on commit 5b52bb8

Please sign in to comment.