feat: add separator option #530

This commit is contained in:
Hufe921
2024-05-03 22:54:24 +08:00
parent 15f52c5e43
commit 7416a88151
8 changed files with 38 additions and 12 deletions
+5 -2
View File
@@ -1412,11 +1412,14 @@ export class Draw {
}
}
} else if (element.type === ElementType.SEPARATOR) {
const {
separator: { lineWidth }
} = this.options
element.width = availableWidth / scale
metrics.width = availableWidth
metrics.height = defaultSize
metrics.height = lineWidth * scale
metrics.boundingBoxAscent = -rowMargin
metrics.boundingBoxDescent = -rowMargin
metrics.boundingBoxDescent = -rowMargin + metrics.height
} else if (element.type === ElementType.PAGE_BREAK) {
element.width = availableWidth / scale
metrics.width = availableWidth
+11 -9
View File
@@ -16,18 +16,20 @@ export class SeparatorParticle {
y: number
) {
ctx.save()
const { scale } = this.options
ctx.lineWidth = scale
if (element.color) {
ctx.strokeStyle = element.color
}
if (element.dashArray && element.dashArray.length) {
const {
scale,
separator: { lineWidth, strokeStyle }
} = this.options
ctx.lineWidth = lineWidth * scale
ctx.strokeStyle = element.color || strokeStyle
if (element.dashArray?.length) {
ctx.setLineDash(element.dashArray)
}
ctx.translate(0, 0.5) // 从1处渲染,避免线宽度等于3
const offsetY = Math.round(y) // 四舍五入避免绘制模糊
ctx.translate(0, ctx.lineWidth / 2)
ctx.beginPath()
ctx.moveTo(x, y)
ctx.lineTo(x + element.width! * scale, y)
ctx.moveTo(x, offsetY)
ctx.lineTo(x + element.width! * scale, offsetY)
ctx.stroke()
ctx.restore()
}
+6
View File
@@ -0,0 +1,6 @@
import { ISeparatorOption } from '../../interface/Separator'
export const defaultSeparatorOption: Readonly<Required<ISeparatorOption>> = {
lineWidth: 1,
strokeStyle: '#000000'
}
+8 -1
View File
@@ -80,6 +80,8 @@ import { BackgroundRepeat, BackgroundSize } from './dataset/enum/Background'
import { TextDecorationStyle } from './dataset/enum/Text'
import { ILineBreakOption } from './interface/LineBreak'
import { defaultLineBreak } from './dataset/constant/LineBreak'
import { ISeparatorOption } from './interface/Separator'
import { defaultSeparatorOption } from './dataset/constant/Separator'
export default class Editor {
public command: Command
@@ -155,6 +157,10 @@ export default class Editor {
...defaultLineBreak,
...options.lineBreak
}
const separatorOptions: Required<ISeparatorOption> = {
...defaultSeparatorOption,
...options.separator
}
const editorOptions: DeepRequired<IEditorOption> = {
mode: EditorMode.EDIT,
@@ -214,7 +220,8 @@ export default class Editor {
pageBreak: pageBreakOptions,
zone: zoneOptions,
background: backgroundOptions,
lineBreak: lineBreakOptions
lineBreak: lineBreakOptions,
separator: separatorOptions
}
// 数据处理
data = deepClone(data)
+2
View File
@@ -22,6 +22,7 @@ import { IPlaceholder } from './Placeholder'
import { ITitleOption } from './Title'
import { IWatermark } from './Watermark'
import { IZoneOption } from './Zone'
import { ISeparatorOption } from './Separator'
export interface IEditorData {
header?: IElement[]
@@ -87,6 +88,7 @@ export interface IEditorOption {
zone?: IZoneOption
background?: IBackgroundOption
lineBreak?: ILineBreakOption
separator?: ISeparatorOption
}
export interface IEditorResult {
+4
View File
@@ -0,0 +1,4 @@
export interface ISeparatorOption {
strokeStyle?: string
lineWidth?: number
}