feat: add separator option #530

pr675
Hufe921 2 years ago
parent 15f52c5e43
commit 7416a88151

@ -71,6 +71,7 @@ interface IEditorOption {
zone?: IZoneOption // Zone option。{tipDisabled?:boolean;} zone?: IZoneOption // Zone option。{tipDisabled?:boolean;}
background?: IBackgroundOption // Background option. {color?:string; image?:string; size?:BackgroundSize; repeat?:BackgroundRepeat;}。default: {color: '#FFFFFF'} background?: IBackgroundOption // Background option. {color?:string; image?:string; size?:BackgroundSize; repeat?:BackgroundRepeat;}。default: {color: '#FFFFFF'}
lineBreak?: ILineBreakOption // LineBreak option. {disabled?:boolean; color?:string; lineWidth?:number;} lineBreak?: ILineBreakOption // LineBreak option. {disabled?:boolean; color?:string; lineWidth?:number;}
separator?: ISeparatorOption // Separator option. {lineWidth?:number; strokeStyle?:string;}
} }
``` ```

@ -71,6 +71,7 @@ interface IEditorOption {
zone?: IZoneOption // 编辑器区域配置。{tipDisabled?:boolean;} zone?: IZoneOption // 编辑器区域配置。{tipDisabled?:boolean;}
background?: IBackgroundOption // 背景配置。{color?:string; image?:string; size?:BackgroundSize; repeat?:BackgroundRepeat;}。默认:{color: '#FFFFFF'} background?: IBackgroundOption // 背景配置。{color?:string; image?:string; size?:BackgroundSize; repeat?:BackgroundRepeat;}。默认:{color: '#FFFFFF'}
lineBreak?: ILineBreakOption // 换行符配置。{disabled?:boolean; color?:string; lineWidth?:number;} lineBreak?: ILineBreakOption // 换行符配置。{disabled?:boolean; color?:string; lineWidth?:number;}
separator?: ISeparatorOption // 分隔符配置。{lineWidth?:number; strokeStyle?:string;}
} }
``` ```

@ -1412,11 +1412,14 @@ export class Draw {
} }
} }
} else if (element.type === ElementType.SEPARATOR) { } else if (element.type === ElementType.SEPARATOR) {
const {
separator: { lineWidth }
} = this.options
element.width = availableWidth / scale element.width = availableWidth / scale
metrics.width = availableWidth metrics.width = availableWidth
metrics.height = defaultSize metrics.height = lineWidth * scale
metrics.boundingBoxAscent = -rowMargin metrics.boundingBoxAscent = -rowMargin
metrics.boundingBoxDescent = -rowMargin metrics.boundingBoxDescent = -rowMargin + metrics.height
} else if (element.type === ElementType.PAGE_BREAK) { } else if (element.type === ElementType.PAGE_BREAK) {
element.width = availableWidth / scale element.width = availableWidth / scale
metrics.width = availableWidth metrics.width = availableWidth

@ -16,18 +16,20 @@ export class SeparatorParticle {
y: number y: number
) { ) {
ctx.save() ctx.save()
const { scale } = this.options const {
ctx.lineWidth = scale scale,
if (element.color) { separator: { lineWidth, strokeStyle }
ctx.strokeStyle = element.color } = this.options
} ctx.lineWidth = lineWidth * scale
if (element.dashArray && element.dashArray.length) { ctx.strokeStyle = element.color || strokeStyle
if (element.dashArray?.length) {
ctx.setLineDash(element.dashArray) 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.beginPath()
ctx.moveTo(x, y) ctx.moveTo(x, offsetY)
ctx.lineTo(x + element.width! * scale, y) ctx.lineTo(x + element.width! * scale, offsetY)
ctx.stroke() ctx.stroke()
ctx.restore() ctx.restore()
} }

@ -0,0 +1,6 @@
import { ISeparatorOption } from '../../interface/Separator'
export const defaultSeparatorOption: Readonly<Required<ISeparatorOption>> = {
lineWidth: 1,
strokeStyle: '#000000'
}

@ -80,6 +80,8 @@ import { BackgroundRepeat, BackgroundSize } from './dataset/enum/Background'
import { TextDecorationStyle } from './dataset/enum/Text' import { TextDecorationStyle } from './dataset/enum/Text'
import { ILineBreakOption } from './interface/LineBreak' import { ILineBreakOption } from './interface/LineBreak'
import { defaultLineBreak } from './dataset/constant/LineBreak' import { defaultLineBreak } from './dataset/constant/LineBreak'
import { ISeparatorOption } from './interface/Separator'
import { defaultSeparatorOption } from './dataset/constant/Separator'
export default class Editor { export default class Editor {
public command: Command public command: Command
@ -155,6 +157,10 @@ export default class Editor {
...defaultLineBreak, ...defaultLineBreak,
...options.lineBreak ...options.lineBreak
} }
const separatorOptions: Required<ISeparatorOption> = {
...defaultSeparatorOption,
...options.separator
}
const editorOptions: DeepRequired<IEditorOption> = { const editorOptions: DeepRequired<IEditorOption> = {
mode: EditorMode.EDIT, mode: EditorMode.EDIT,
@ -214,7 +220,8 @@ export default class Editor {
pageBreak: pageBreakOptions, pageBreak: pageBreakOptions,
zone: zoneOptions, zone: zoneOptions,
background: backgroundOptions, background: backgroundOptions,
lineBreak: lineBreakOptions lineBreak: lineBreakOptions,
separator: separatorOptions
} }
// 数据处理 // 数据处理
data = deepClone(data) data = deepClone(data)

@ -22,6 +22,7 @@ import { IPlaceholder } from './Placeholder'
import { ITitleOption } from './Title' import { ITitleOption } from './Title'
import { IWatermark } from './Watermark' import { IWatermark } from './Watermark'
import { IZoneOption } from './Zone' import { IZoneOption } from './Zone'
import { ISeparatorOption } from './Separator'
export interface IEditorData { export interface IEditorData {
header?: IElement[] header?: IElement[]
@ -87,6 +88,7 @@ export interface IEditorOption {
zone?: IZoneOption zone?: IZoneOption
background?: IBackgroundOption background?: IBackgroundOption
lineBreak?: ILineBreakOption lineBreak?: ILineBreakOption
separator?: ISeparatorOption
} }
export interface IEditorResult { export interface IEditorResult {

@ -0,0 +1,4 @@
export interface ISeparatorOption {
strokeStyle?: string
lineWidth?: number
}
Loading…
Cancel
Save