feat: add line number option #734

This commit is contained in:
Hufe921
2024-08-04 21:31:00 +08:00
parent 8878fd7734
commit d89218a916
11 changed files with 138 additions and 16 deletions
+15 -1
View File
@@ -101,6 +101,7 @@ import { ImageDisplay } from '../../dataset/enum/Common'
import { PUNCTUATION_REG } from '../../dataset/constant/Regular'
import { LineBreakParticle } from './particle/LineBreakParticle'
import { MouseObserver } from '../observer/MouseObserver'
import { LineNumber } from './frame/LineNumber'
export class Draw {
private container: HTMLDivElement
@@ -138,6 +139,7 @@ export class Draw {
private tableParticle: TableParticle
private tableTool: TableTool
private pageNumber: PageNumber
private lineNumber: LineNumber
private waterMark: Watermark
private placeholder: Placeholder
private header: Header
@@ -213,6 +215,7 @@ export class Draw {
this.tableParticle = new TableParticle(this)
this.tableTool = new TableTool(this)
this.pageNumber = new PageNumber(this)
this.lineNumber = new LineNumber(this)
this.waterMark = new Watermark(this)
this.placeholder = new Placeholder(this)
this.header = new Header(this, data.header)
@@ -549,6 +552,10 @@ export class Draw {
return this.lineBreakParticle
}
public getTextParticle(): TextParticle {
return this.textParticle
}
public getHeaderElementList(): IElement[] {
return this.header.getElementList()
}
@@ -1194,6 +1201,7 @@ export class Draw {
ascent: 0,
elementList: [],
startIndex: 0,
rowIndex: 0,
rowFlex: elementList?.[0]?.rowFlex || elementList?.[1]?.rowFlex
})
}
@@ -1645,6 +1653,7 @@ export class Draw {
startIndex: i,
elementList: [rowElement],
ascent,
rowIndex: curRow.rowIndex + 1,
rowFlex: elementList[i]?.rowFlex || elementList[i + 1]?.rowFlex,
isPageBreak: element.type === ElementType.PAGE_BREAK
}
@@ -2186,7 +2195,8 @@ export class Draw {
private _drawPage(payload: IDrawPagePayload) {
const { elementList, positionList, rowList, pageNo } = payload
const { inactiveAlpha, pageMode, header, footer, pageNumber } = this.options
const { inactiveAlpha, pageMode, header, footer, pageNumber, lineNumber } =
this.options
const innerWidth = this.getInnerWidth()
const ctx = this.ctxList[pageNo]
// 判断当前激活区域-非正文区域时元素透明度降低
@@ -2247,6 +2257,10 @@ export class Draw {
if (this.elementList.length <= 1 && !this.elementList[0]?.listId) {
this.placeholder.render(ctx)
}
// 渲染行数
if (!lineNumber.disabled) {
this.lineNumber.render(ctx, pageNo)
}
}
private _disconnectLazyRender() {
+43
View File
@@ -0,0 +1,43 @@
import { LineNumberType } from '../../../dataset/enum/LineNumber'
import { DeepRequired } from '../../../interface/Common'
import { IEditorOption } from '../../../interface/Editor'
import { Draw } from '../Draw'
export class LineNumber {
private draw: Draw
private options: DeepRequired<IEditorOption>
constructor(draw: Draw) {
this.draw = draw
this.options = draw.getOptions()
}
public render(ctx: CanvasRenderingContext2D, pageNo: number) {
const {
scale,
lineNumber: { color, size, font, right, type }
} = this.options
const textParticle = this.draw.getTextParticle()
const margins = this.draw.getMargins()
const positionList = this.draw.getPosition().getOriginalMainPositionList()
const pageRowList = this.draw.getPageRowList()
const rowList = pageRowList[pageNo]
ctx.save()
ctx.fillStyle = color
ctx.font = `${size * scale}px ${font}`
for (let i = 0; i < rowList.length; i++) {
const row = rowList[i]
const {
coordinate: { leftBottom }
} = positionList[row.startIndex]
const seq = type === LineNumberType.PAGE ? i + 1 : row.rowIndex + 1
const textMetrics = textParticle.measureText(ctx, {
value: `${seq}`
})
const x = margins[3] - (textMetrics.width + right) * scale
const y = leftBottom[1] - textMetrics.actualBoundingBoxAscent * scale
ctx.fillText(`${seq}`, x, y)
}
ctx.restore()
}
}