Skip to content

Commit

Permalink
feat: add executeDeleteElementById api #1003
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Feb 12, 2025
1 parent 15728e8 commit 089d684
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/en/guide/command-execute.md
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,16 @@ Usage:
instance.command.executeUpdateElementById(payload: IUpdateElementByIdOption)
```

## executeDeleteElementById

Feature: Delete element by id

Usage:

```javascript
instance.command.executeDeleteElementById(payload: IDeleteElementByIdOption)
```

## executeSetValue

Feature: Set the editor data
Expand Down
10 changes: 10 additions & 0 deletions docs/guide/command-execute.md
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,16 @@ instance.command.executeAppendElementList(elementList: IElement[], options?: IAp
instance.command.executeUpdateElementById(payload: IUpdateElementByIdOption)
```

## executeDeleteElementById

功能:根据 id 删除元素

用法:

```javascript
instance.command.executeDeleteElementById(payload: IDeleteElementByIdOption)
```

## executeSetValue

功能:设置编辑器数据
Expand Down
2 changes: 2 additions & 0 deletions src/editor/core/command/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class Command {
public executeLocationArea: CommandAdapt['locationArea']
public executeAppendElementList: CommandAdapt['appendElementList']
public executeUpdateElementById: CommandAdapt['updateElementById']
public executeDeleteElementById: CommandAdapt['deleteElementById']
public executeSetValue: CommandAdapt['setValue']
public executeRemoveControl: CommandAdapt['removeControl']
public executeSetLocale: CommandAdapt['setLocale']
Expand Down Expand Up @@ -230,6 +231,7 @@ export class Command {
this.executeInsertElementList = adapt.insertElementList.bind(adapt)
this.executeAppendElementList = adapt.appendElementList.bind(adapt)
this.executeUpdateElementById = adapt.updateElementById.bind(adapt)
this.executeDeleteElementById = adapt.deleteElementById.bind(adapt)
this.executeSetValue = adapt.setValue.bind(adapt)
this.executeRemoveControl = adapt.removeControl.bind(adapt)
this.executeSetLocale = adapt.setLocale.bind(adapt)
Expand Down
45 changes: 45 additions & 0 deletions src/editor/core/command/CommandAdapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import {
IUpdateOption
} from '../../interface/Editor'
import {
IDeleteElementByIdOption,
IElement,
IElementPosition,
IElementStyle,
Expand Down Expand Up @@ -1737,6 +1738,50 @@ export class CommandAdapt {
})
}

public deleteElementById(payload: IDeleteElementByIdOption) {
const { id, conceptId } = payload
if (!id && !conceptId) return
let isExistDelete = false
function deleteElement(elementList: IElement[]) {
let i = 0
while (i < elementList.length) {
const element = elementList[i]
if (element.type === ElementType.TABLE) {
const trList = element.trList!
for (let r = 0; r < trList.length; r++) {
const tr = trList[r]
for (let d = 0; d < tr.tdList.length; d++) {
const td = tr.tdList[d]
deleteElement(td.value)
}
}
}
if (
(id && element.id === id) ||
(conceptId && element.conceptId === conceptId)
) {
isExistDelete = true
elementList.splice(i, 1)
i--
}
i++
}
}
// 优先正文再页眉页脚
const data = [
this.draw.getOriginalMainElementList(),
this.draw.getHeaderElementList(),
this.draw.getFooterElementList()
]
for (const elementList of data) {
deleteElement(elementList)
}
if (!isExistDelete) return
this.draw.render({
isSetCursor: false
})
}

public getElementById(payload: IGetElementByIdOption): IElement[] {
const { id, conceptId } = payload
const result: IElement[] = []
Expand Down
5 changes: 5 additions & 0 deletions src/editor/interface/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ export interface IUpdateElementByIdOption {
properties: Omit<Partial<IElement>, 'id'>
}

export interface IDeleteElementByIdOption {
id?: string
conceptId?: string
}

export interface IGetElementByIdOption {
id?: string
conceptId?: string
Expand Down

0 comments on commit 089d684

Please sign in to comment.