Skip to content

Commit

Permalink
feat: add getElementById api
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Oct 26, 2024
1 parent 2bb84ab commit 8eabd07
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
10 changes: 10 additions & 0 deletions docs/en/guide/command-get.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,13 @@ instance.eventBus.on(
}, 200)
)``
```

## getElementById

Feature: Get element list by id

Usage:

```javascript
const elementList = await instance.command.getElementById(payload: IGetElementByIdOption)
```
10 changes: 10 additions & 0 deletions docs/guide/command-get.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,13 @@ instance.eventBus.on(
}, 200)
)``
```

## getElementById

功能:根据 id 获取元素

用法:

```javascript
const elementList = await instance.command.getElementById(payload: IGetElementByIdOption)
```
2 changes: 2 additions & 0 deletions src/editor/core/command/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export class Command {
public getContainer: CommandAdapt['getContainer']
public getTitleValue: CommandAdapt['getTitleValue']
public getPositionContextByEvent: CommandAdapt['getPositionContextByEvent']
public getElementById: CommandAdapt['getElementById']

constructor(adapt: CommandAdapt) {
// 全局命令
Expand Down Expand Up @@ -241,6 +242,7 @@ export class Command {
this.getContainer = adapt.getContainer.bind(adapt)
this.getTitleValue = adapt.getTitleValue.bind(adapt)
this.getPositionContextByEvent = adapt.getPositionContextByEvent.bind(adapt)
this.getElementById = adapt.getElementById.bind(adapt)
// 控件
this.executeSetControlValue = adapt.setControlValue.bind(adapt)
this.executeSetControlExtension = adapt.setControlExtension.bind(adapt)
Expand Down
39 changes: 38 additions & 1 deletion src/editor/core/command/CommandAdapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
IElement,
IElementPosition,
IElementStyle,
IGetElementByIdOption,
IUpdateElementByIdOption
} from '../../interface/Element'
import { IPasteOption, IPositionContextByEvent } from '../../interface/Event'
Expand Down Expand Up @@ -1613,10 +1614,15 @@ export class CommandAdapt {
}

public updateElementById(payload: IUpdateElementByIdOption) {
const { id, conceptId } = payload
if (!id && !conceptId) return
function getElementIndexById(elementList: IElement[]): number {
for (let e = 0; e < elementList.length; e++) {
const element = elementList[e]
if (element.id === payload.id) {
if (
(id && element.id === id) ||
(conceptId && element.conceptId === conceptId)
) {
return e
}
}
Expand Down Expand Up @@ -1648,6 +1654,37 @@ export class CommandAdapt {
}
}

public getElementById(payload: IGetElementByIdOption): IElement[] {
const { id, conceptId } = payload
const result: IElement[] = []
if (!id && !conceptId) return result
const getElement = (elementList: IElement[]) => {
let i = 0
while (i < elementList.length) {
const element = elementList[i]
i++
if (
(id && element.controlId !== id) ||
(conceptId && element.conceptId !== conceptId)
) {
continue
}
result.push(element)
}
}
const data = [
this.draw.getHeaderElementList(),
this.draw.getOriginalMainElementList(),
this.draw.getFooterElementList()
]
for (const elementList of data) {
getElement(elementList)
}
return zipElementList(result, {
extraPickAttrs: ['id']
})
}

public setValue(payload: Partial<IEditorData>, options?: ISetValueOption) {
this.draw.setValue(payload, options)
}
Expand Down
8 changes: 7 additions & 1 deletion src/editor/interface/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ export interface IElementFillRect {
}

export interface IUpdateElementByIdOption {
id: string
id?: string
conceptId?: string
properties: Omit<Partial<IElement>, 'id'>
}

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

0 comments on commit 8eabd07

Please sign in to comment.