feat:add separator

This commit is contained in:
黄云飞
2022-01-14 23:29:32 +08:00
parent 094f3f5a50
commit c1cb37c56e
9 changed files with 82 additions and 6 deletions
+6
View File
@@ -40,6 +40,7 @@ export class Command {
private static cancelMergeTableCell: Function
private static image: Function
private static hyperlink: Function
private static separator: Function
private static search: Function
private static print: Function
private static pageScaleRecovery: Function
@@ -82,6 +83,7 @@ export class Command {
Command.cancelMergeTableCell = adapt.cancelMergeTableCell.bind(adapt)
Command.image = adapt.image.bind(adapt)
Command.hyperlink = adapt.hyperlink.bind(adapt)
Command.separator = adapt.separator.bind(adapt)
Command.search = adapt.search.bind(adapt)
Command.print = adapt.print.bind(adapt)
Command.pageScaleRecovery = adapt.pageScaleRecovery.bind(adapt)
@@ -233,6 +235,10 @@ export class Command {
return Command.image(payload)
}
public executeSeparator() {
return Command.separator()
}
public executeSearch(payload: string | null) {
return Command.search(payload)
}
+16
View File
@@ -890,6 +890,22 @@ export class CommandAdapt {
this.draw.render({ curIndex })
}
public separator() {
const { startIndex, endIndex } = this.range.getRange()
if (!~startIndex && !~endIndex) return
const elementList = this.draw.getElementList()
// 光标后是否存在分割线
if (elementList[endIndex]?.type === ElementType.SEPARATOR) return
const element: IElement = {
value: '\n',
type: ElementType.SEPARATOR
}
const curIndex = startIndex + 1
elementList.splice(curIndex, 0, element)
this.range.setRange(curIndex, curIndex)
this.draw.render({ curIndex })
}
public image(payload: IDrawImagePayload) {
const { startIndex, endIndex } = this.range.getRange()
if (!~startIndex && !~endIndex) return
+10
View File
@@ -30,6 +30,7 @@ import { HyperlinkParticle } from "./particle/HyperlinkParticle"
import { Header } from "./frame/Header"
import { SuperscriptParticle } from "./particle/Superscript"
import { SubscriptParticle } from "./particle/Subscript"
import { SeparatorParticle } from "./particle/Separator"
export class Draw {
@@ -60,6 +61,7 @@ export class Draw {
private pageNumber: PageNumber
private header: Header
private hyperlinkParticle: HyperlinkParticle
private separatorParticle: SeparatorParticle
private superscriptParticle: SuperscriptParticle
private subscriptParticle: SubscriptParticle
@@ -102,6 +104,7 @@ export class Draw {
this.pageNumber = new PageNumber(this)
this.header = new Header(this)
this.hyperlinkParticle = new HyperlinkParticle(this)
this.separatorParticle = new SeparatorParticle(this)
this.superscriptParticle = new SuperscriptParticle()
this.subscriptParticle = new SubscriptParticle()
@@ -443,6 +446,11 @@ export class Draw {
metrics.height = elementHeight
metrics.boundingBoxDescent = elementHeight
metrics.boundingBoxAscent = 0
} else if (element.type === ElementType.SEPARATOR) {
metrics.width = innerWidth
metrics.height = this.options.defaultSize
metrics.boundingBoxAscent = -rowMargin
metrics.boundingBoxDescent = -rowMargin
} else {
// 设置上下标真实字体尺寸
const size = element.size || this.options.defaultSize
@@ -570,6 +578,8 @@ export class Draw {
} else if (element.type === ElementType.SUBSCRIPT) {
this.textParticle.complete()
this.subscriptParticle.render(ctx, element, x, y + offsetY)
} else if (element.type === ElementType.SEPARATOR) {
this.separatorParticle.render(ctx, element, x, y)
} else {
this.textParticle.record(ctx, element, x, y + offsetY)
}
@@ -0,0 +1,26 @@
import { IRowElement } from "../../../interface/Row"
import { Draw } from "../Draw"
export class SeparatorParticle {
private draw: Draw
constructor(draw: Draw) {
this.draw = draw
}
public render(ctx: CanvasRenderingContext2D, element: IRowElement, x: number, y: number) {
const innerWidth = this.draw.getInnerWidth()
ctx.save()
if (element.color) {
ctx.strokeStyle = element.color
}
ctx.translate(0.5, 0.5)
ctx.beginPath()
ctx.moveTo(x, y)
ctx.lineTo(x + innerWidth, y)
ctx.stroke()
ctx.restore()
}
}