feat: get page value and append element api #211

pr675
Hufe921 3 years ago
parent ccb1aa70ab
commit 85a9dcbcf2

@ -551,6 +551,14 @@ instance.command.executeSetPaperMargin([top: number, right: number, bottom: numb
instance.command.executeInsertElementList(elementList: IElement[])
```
## executeAppendElementList
功能:追加元素
用法:
```javascript
instance.command.executeAppendElementList(elementList: IElement[], options?: IAppendElementListOption)
```
## executeSetValue
功能:设置编辑器数据

@ -21,7 +21,7 @@ const {
header?: IHeader;
watermark?: IWatermark;
data: IEditorData;
} = instance.command.getValue()
} = instance.command.getValue(options?: IGetValueOption)
```
## getImage

@ -69,6 +69,7 @@ export class Command {
public executePaperDirection: CommandAdapt['paperDirection']
public executeSetPaperMargin: CommandAdapt['setPaperMargin']
public executeInsertElementList: CommandAdapt['insertElementList']
public executeAppendElementList: CommandAdapt['appendElementList']
public executeSetValue: CommandAdapt['setValue']
public executeRemoveControl: CommandAdapt['removeControl']
public executeSetLocale: CommandAdapt['setLocale']
@ -156,6 +157,7 @@ export class Command {
this.executeSetPaperMargin = adapt.setPaperMargin.bind(adapt)
// 通用
this.executeInsertElementList = adapt.insertElementList.bind(adapt)
this.executeAppendElementList = adapt.appendElementList.bind(adapt)
this.executeSetValue = adapt.setValue.bind(adapt)
this.executeRemoveControl = adapt.removeControl.bind(adapt)
this.executeSetLocale = adapt.setLocale.bind(adapt)

@ -12,7 +12,7 @@ import { TableBorder } from '../../dataset/enum/table/Table'
import { TitleLevel } from '../../dataset/enum/Title'
import { VerticalAlign } from '../../dataset/enum/VerticalAlign'
import { ICatalog } from '../../interface/Catalog'
import { IDrawImagePayload, IPainterOptions } from '../../interface/Draw'
import { IAppendElementListOption, IDrawImagePayload, IGetValueOption, IPainterOptions } from '../../interface/Draw'
import { IEditorData, IEditorOption, IEditorResult } from '../../interface/Editor'
import { IElement, IElementStyle } from '../../interface/Element'
import { IMargin } from '../../interface/Margin'
@ -1589,8 +1589,8 @@ export class CommandAdapt {
return this.draw.getDataURL()
}
public getValue(): IEditorResult {
return this.draw.getValue()
public getValue(options?: IGetValueOption): IEditorResult {
return this.draw.getValue(options)
}
public getWordCount(): Promise<number> {
@ -1655,6 +1655,13 @@ export class CommandAdapt {
this.draw.insertElementList(payload)
}
public appendElementList(elementList: IElement[], options?: IAppendElementListOption) {
if (!elementList.length) return
const isReadonly = this.draw.isReadonly()
if (isReadonly) return
this.draw.appendElementList(elementList, options)
}
public setValue(payload: Partial<IEditorData>) {
this.draw.setValue(payload)
}

@ -1,7 +1,7 @@
import { version } from '../../../../package.json'
import { ZERO } from '../../dataset/constant/Common'
import { RowFlex } from '../../dataset/enum/Row'
import { IDrawOption, IDrawPagePayload, IDrawRowPayload, IPainterOptions } from '../../interface/Draw'
import { IAppendElementListOption, IDrawOption, IDrawPagePayload, IDrawRowPayload, IGetValueOption, IPainterOptions } from '../../interface/Draw'
import { IEditorData, IEditorOption, IEditorResult } from '../../interface/Editor'
import { IElement, IElementMetrics, IElementFillRect, IElementStyle } from '../../interface/Element'
import { IRow, IRowElement } from '../../interface/Row'
@ -474,6 +474,27 @@ export class Draw {
}
}
public appendElementList(elementList: IElement[], options: IAppendElementListOption = {}) {
if (!elementList.length) return
formatElementList(elementList, {
isHandleFirstElement: false,
editorOptions: this.options
})
let curIndex: number
const { isPrepend } = options
if (isPrepend) {
this.elementList.splice(1, 0, ...elementList)
curIndex = elementList.length
} else {
this.elementList.push(...elementList)
curIndex = this.elementList.length - 1
}
this.range.setRange(curIndex, curIndex)
this.render({
curIndex
})
}
public spliceElementList(elementList: IElement[], start: number, deleteCount: number, ...items: IElement[]) {
if (deleteCount > 0) {
// 当最后元素与开始元素列表信息不一致时:清除当前列表信息
@ -719,13 +740,18 @@ export class Draw {
})
}
public getValue(): IEditorResult {
public getValue(options: IGetValueOption = {}): IEditorResult {
// 配置
const { width, height, margins, watermark } = this.options
// 数据
const { pageNo } = options
let mainElementList = this.elementList
if (Number.isInteger(pageNo) && pageNo! >= 0 && pageNo! < this.pageRowList.length) {
mainElementList = this.pageRowList[pageNo!].flatMap(row => row.elementList)
}
const data: IEditorData = {
header: zipElementList(this.headerElementList),
main: zipElementList(this.elementList),
main: zipElementList(mainElementList),
footer: zipElementList(this.footerElementList)
}
return {

@ -35,4 +35,12 @@ export interface IDrawPagePayload {
export interface IPainterOptions {
isDblclick: boolean;
}
export interface IGetValueOption {
pageNo?: number;
}
export interface IAppendElementListOption {
isPrepend?: boolean;
}
Loading…
Cancel
Save