diff --git a/docs/guide/command-execute.md b/docs/guide/command-execute.md index 63a33d3..3cfb642 100644 --- a/docs/guide/command-execute.md +++ b/docs/guide/command-execute.md @@ -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 功能:设置编辑器数据 diff --git a/docs/guide/command-get.md b/docs/guide/command-get.md index 6a4ad81..e74b077 100644 --- a/docs/guide/command-get.md +++ b/docs/guide/command-get.md @@ -21,7 +21,7 @@ const { header?: IHeader; watermark?: IWatermark; data: IEditorData; -} = instance.command.getValue() +} = instance.command.getValue(options?: IGetValueOption) ``` ## getImage diff --git a/src/editor/core/command/Command.ts b/src/editor/core/command/Command.ts index a2c9aba..6b79afd 100644 --- a/src/editor/core/command/Command.ts +++ b/src/editor/core/command/Command.ts @@ -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) diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts index 1c5a3ab..eb258b4 100644 --- a/src/editor/core/command/CommandAdapt.ts +++ b/src/editor/core/command/CommandAdapt.ts @@ -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 { @@ -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) { this.draw.setValue(payload) } diff --git a/src/editor/core/draw/Draw.ts b/src/editor/core/draw/Draw.ts index c52c134..8c3a664 100644 --- a/src/editor/core/draw/Draw.ts +++ b/src/editor/core/draw/Draw.ts @@ -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 { diff --git a/src/editor/interface/Draw.ts b/src/editor/interface/Draw.ts index 8adffa3..42eb4f7 100644 --- a/src/editor/interface/Draw.ts +++ b/src/editor/interface/Draw.ts @@ -35,4 +35,12 @@ export interface IDrawPagePayload { export interface IPainterOptions { isDblclick: boolean; +} + +export interface IGetValueOption { + pageNo?: number; +} + +export interface IAppendElementListOption { + isPrepend?: boolean; } \ No newline at end of file