feat: add checkbox list #385

This commit is contained in:
Hufe
2024-04-07 22:05:35 +08:00
committed by GitHub
parent 237e570179
commit a546262b9d
8 changed files with 184 additions and 119 deletions
+34 -14
View File
@@ -6,7 +6,7 @@ import { ListStyle, ListType, UlStyle } from '../../../dataset/enum/List'
import { DeepRequired } from '../../../interface/Common'
import { IEditorOption } from '../../../interface/Editor'
import { IElement, IElementPosition } from '../../../interface/Element'
import { IRow } from '../../../interface/Row'
import { IRow, IRowElement } from '../../../interface/Row'
import { getUUID } from '../../../utils'
import { RangeManager } from '../../range/RangeManager'
import { Draw } from '../Draw'
@@ -174,15 +174,7 @@ export class ListParticle {
if (element?.type !== ElementType.TAB) break
tabWidth += defaultTabWidth * scale
}
let text = ''
if (startElement.listType === ListType.UL) {
text =
ulStyleMapping[<UlStyle>(<unknown>startElement.listStyle)] ||
ulStyleMapping[UlStyle.DISC]
} else {
text = `${listIndex! + 1}${KeyMap.PERIOD}`
}
if (!text) return
// 列表样式渲染
const {
coordinate: {
leftTop: [startX, startY]
@@ -190,9 +182,37 @@ export class ListParticle {
} = position
const x = startX - offsetX! + tabWidth
const y = startY + ascent
ctx.save()
ctx.font = `${defaultSize * scale}px ${defaultFont}`
ctx.fillText(text, x, y)
ctx.restore()
// 复选框样式特殊处理
if (startElement.listStyle === ListStyle.CHECKBOX) {
const { width, height, gap } = this.options.checkbox
const checkboxRowElement: IRowElement = {
...startElement,
checkbox: {
value: !!startElement.checkbox?.value
},
metrics: {
...startElement.metrics,
width: (width + gap * 2) * scale,
height: height * scale
}
}
this.draw
.getCheckboxParticle()
.render(ctx, checkboxRowElement, x - gap * scale, y)
} else {
let text = ''
if (startElement.listType === ListType.UL) {
text =
ulStyleMapping[<UlStyle>(<unknown>startElement.listStyle)] ||
ulStyleMapping[UlStyle.DISC]
} else {
text = `${listIndex! + 1}${KeyMap.PERIOD}`
}
if (!text) return
ctx.save()
ctx.font = `${defaultSize * scale}px ${defaultFont}`
ctx.fillText(text, x, y)
ctx.restore()
}
}
}
+18 -8
View File
@@ -1,4 +1,4 @@
import { ElementType, RowFlex, VerticalAlign } from '../..'
import { ElementType, ListStyle, RowFlex, VerticalAlign } from '../..'
import { ZERO } from '../../dataset/constant/Common'
import { ControlComponent } from '../../dataset/enum/Control'
import {
@@ -358,6 +358,7 @@ export class Position {
coordinate: { leftTop, rightTop, leftBottom }
} = positionList[j]
if (positionNo !== pageNo) continue
if (pageNo > positionNo) break
// 命中元素
if (
leftTop[0] - left <= x &&
@@ -482,17 +483,15 @@ export class Position {
for (let j = 0; j < lastLetterList.length; j++) {
const {
index,
pageNo,
rowNo,
coordinate: { leftTop, leftBottom }
} = lastLetterList[j]
if (positionNo !== pageNo) continue
if (y > leftTop[1] && y <= leftBottom[1]) {
const isHead = x < this.options.margins[3]
const headIndex = positionList.findIndex(
p => p.pageNo === positionNo && p.rowNo === rowNo
)
// 是否在头部
if (isHead) {
const headIndex = positionList.findIndex(
p => p.pageNo === positionNo && p.rowNo === lastLetterList[j].rowNo
)
if (x < this.options.margins[3]) {
// 头部元素为空元素时无需选中
if (~headIndex) {
if (positionList[headIndex].value === ZERO) {
@@ -505,6 +504,17 @@ export class Position {
curPositionIndex = index
}
} else {
// 是否是复选框列表
if (
elementList[headIndex].listStyle === ListStyle.CHECKBOX &&
x < leftTop[0]
) {
return {
index: headIndex,
isDirectHit: true,
isCheckbox: true
}
}
curPositionIndex = index
}
isLastArea = true