feat: add executeUpdateElementById api #648

This commit is contained in:
Hufe921
2024-06-13 22:33:12 +08:00
parent f7da332354
commit 5c896bf958
5 changed files with 75 additions and 3 deletions
+2
View File
@@ -76,6 +76,7 @@ export class Command {
public executeSetPaperMargin: CommandAdapt['setPaperMargin']
public executeInsertElementList: CommandAdapt['insertElementList']
public executeAppendElementList: CommandAdapt['appendElementList']
public executeUpdateElementById: CommandAdapt['updateElementById']
public executeSetValue: CommandAdapt['setValue']
public executeRemoveControl: CommandAdapt['removeControl']
public executeSetLocale: CommandAdapt['setLocale']
@@ -198,6 +199,7 @@ export class Command {
// 通用
this.executeInsertElementList = adapt.insertElementList.bind(adapt)
this.executeAppendElementList = adapt.appendElementList.bind(adapt)
this.executeUpdateElementById = adapt.updateElementById.bind(adapt)
this.executeSetValue = adapt.setValue.bind(adapt)
this.executeRemoveControl = adapt.removeControl.bind(adapt)
this.executeSetLocale = adapt.setLocale.bind(adapt)
+48 -3
View File
@@ -53,7 +53,11 @@ import {
IEditorText,
IUpdateOption
} from '../../interface/Editor'
import { IElement, IElementStyle } from '../../interface/Element'
import {
IElement,
IElementStyle,
IUpdateElementByIdOption
} from '../../interface/Element'
import { IPasteOption } from '../../interface/Event'
import { IMargin } from '../../interface/Margin'
import { ILocationPosition } from '../../interface/Position'
@@ -2093,9 +2097,14 @@ export class CommandAdapt {
// 元素信息
const elementList = this.draw.getElementList()
const startElement = pickElementAttr(
elementList[isCollapsed ? startIndex : startIndex + 1]
elementList[isCollapsed ? startIndex : startIndex + 1],
{
extraPickAttrs: ['id']
}
)
const endElement = pickElementAttr(elementList[endIndex])
const endElement = pickElementAttr(elementList[endIndex], {
extraPickAttrs: ['id']
})
// 页码信息
const positionList = this.position.getPositionList()
const startPageNo = positionList[startIndex].pageNo
@@ -2262,6 +2271,42 @@ export class CommandAdapt {
this.draw.appendElementList(deepClone(elementList), options)
}
public updateElementById(payload: IUpdateElementByIdOption) {
function getElementIndexById(elementList: IElement[]): number {
for (let e = 0; e < elementList.length; e++) {
const element = elementList[e]
if (element.id === payload.id) {
return e
}
}
return -1
}
// 优先正文再页眉页脚
const getElementListFnList = [
this.draw.getOriginalMainElementList,
this.draw.getHeaderElementList,
this.draw.getFooterElementList
]
for (const getElementList of getElementListFnList) {
const elementList = getElementList.call(this.draw)
const elementIndex = getElementIndexById(elementList)
if (~elementIndex) {
elementList[elementIndex] = {
...elementList[elementIndex],
...payload.properties
}
formatElementList([elementList[elementIndex]], {
isHandleFirstElement: false,
editorOptions: this.options
})
this.draw.render({
isSetCursor: false
})
break
}
}
}
public setValue(payload: Partial<IEditorData>) {
this.draw.setValue(payload)
}
+5
View File
@@ -174,3 +174,8 @@ export interface IElementFillRect {
width: number
height: number
}
export interface IUpdateElementByIdOption {
id: string
properties: Omit<IElement, 'id'>
}