feat:add separator

This commit is contained in:
黄云飞
2022-01-16 16:33:11 +08:00
parent c1cb37c56e
commit f7a677409b
16 changed files with 144 additions and 30 deletions
+2 -2
View File
@@ -235,8 +235,8 @@ export class Command {
return Command.image(payload)
}
public executeSeparator() {
return Command.separator()
public executeSeparator(payload: number[]) {
return Command.separator(payload)
}
public executeSearch(payload: string | null) {
+22 -8
View File
@@ -890,18 +890,32 @@ export class CommandAdapt {
this.draw.render({ curIndex })
}
public separator() {
public separator(payload: number[]) {
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
let curIndex = -1
// 光标存在分割线,则判断为修改线段逻辑
const endElement = elementList[endIndex + 1]
if (endElement && endElement.type === ElementType.SEPARATOR) {
if (endElement.dashArray && endElement.dashArray.join() === payload.join()) return
curIndex = endIndex
endElement.dashArray = payload
} else {
const newElement: IElement = {
value: '\n',
type: ElementType.SEPARATOR,
dashArray: payload
}
// 从行头增加分割线
if (startIndex !== 0 && elementList[startIndex].value === ZERO) {
elementList.splice(startIndex, 1, newElement)
curIndex = startIndex - 1
} else {
elementList.splice(startIndex + 1, 0, newElement)
curIndex = startIndex
}
}
const curIndex = startIndex + 1
elementList.splice(curIndex, 0, element)
this.range.setRange(curIndex, curIndex)
this.draw.render({ curIndex })
}
+2 -1
View File
@@ -104,7 +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.separatorParticle = new SeparatorParticle()
this.superscriptParticle = new SuperscriptParticle()
this.subscriptParticle = new SubscriptParticle()
@@ -447,6 +447,7 @@ export class Draw {
metrics.boundingBoxDescent = elementHeight
metrics.boundingBoxAscent = 0
} else if (element.type === ElementType.SEPARATOR) {
element.width = innerWidth
metrics.width = innerWidth
metrics.height = this.options.defaultSize
metrics.boundingBoxAscent = -rowMargin
+5 -11
View File
@@ -1,24 +1,18 @@
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)
if (element.dashArray && element.dashArray.length) {
ctx.setLineDash(element.dashArray)
}
ctx.translate(0, 0.5) // 从1处渲染,避免线宽度等于3
ctx.beginPath()
ctx.moveTo(x, y)
ctx.lineTo(x + innerWidth, y)
ctx.lineTo(x + element.width!, y)
ctx.stroke()
ctx.restore()
}
+5 -2
View File
@@ -78,6 +78,7 @@ export class RangeManager {
const highlight = curElement.highlight || null
const rowFlex = curElement.rowFlex || null
const rowMargin = curElement.rowMargin || this.options.defaultRowMargin
const dashArray = curElement.dashArray || []
// 菜单
const painter = !!this.draw.getPainterStyle()
const undo = this.historyManager.isCanUndo()
@@ -95,7 +96,8 @@ export class RangeManager {
color,
highlight,
rowFlex,
rowMargin
rowMargin,
dashArray
})
}
@@ -119,7 +121,8 @@ export class RangeManager {
color: null,
highlight: null,
rowFlex: null,
rowMargin
rowMargin,
dashArray: []
})
}
+10 -1
View File
@@ -47,7 +47,16 @@ export interface ISuperscriptSubscript {
actualSize?: number;
}
export type IElement = IElementBasic & IElementStyle & ITable & IHyperlinkElement & ISuperscriptSubscript
export interface ISeparator {
dashArray?: number[];
}
export type IElement = IElementBasic
& IElementStyle
& ITable
& IHyperlinkElement
& ISuperscriptSubscript
& ISeparator
export interface IElementMetrics {
width: number;
+1
View File
@@ -16,6 +16,7 @@ export interface IRangeStyle {
highlight: string | null;
rowFlex: RowFlex | null;
rowMargin: number;
dashArray: number[];
}
export type IRangeStyleChange = (payload: IRangeStyle) => void