fix: 🐛 add updateCellId api (#1739)

This commit is contained in:
vector
2022-01-08 11:01:17 +08:00
committed by GitHub
parent 0dd0c134c1
commit 78cdb3bd56
4 changed files with 64 additions and 0 deletions

View File

@@ -496,6 +496,10 @@ export class Graph extends Basecoat<EventArgs> {
return result
}
updateCellId(cell: Cell, newId: string) {
return this.model.updateCellId(cell, newId)
}
// #endregion
// #region view

View File

@@ -306,6 +306,36 @@ export class Model extends Basecoat<Model.EventArgs> {
return null
}
updateCellId(cell: Cell, newId: string) {
this.startBatch('update', { id: newId })
cell.prop('id', newId)
const newCell = cell.clone({ keepId: true })
this.addCell(newCell)
// update connected edge terminal
const edges = this.getConnectedEdges(cell)
edges.forEach((edge) => {
const sourceCell = edge.getSourceCell()
const targetCell = edge.getTargetCell()
if (sourceCell === cell) {
edge.setSource({
...edge.getSource(),
cell: newId,
})
}
if (targetCell === cell) {
edge.setTarget({
...edge.getTarget(),
cell: newId,
})
}
})
this.removeCell(cell)
this.startBatch('update', { id: newId })
return newCell
}
removeCells(cells: (Cell | string)[], options: Cell.RemoveOptions = {}) {
if (cells.length) {
return this.batchUpdate('remove', () => {

View File

@@ -282,6 +282,21 @@ getCellById(id: string)
|------|--------|:----:|--------|-------------|
| cell | string | ✓ | | 节点/边的 ID。 |
### updateCellId(...)
```sign
updateCellId(cell: Cell, newId: string)
```
更新节点或者边的 ID会返回新创建的节点/边。
<span class="tag-param">参数<span>
| 名称 | 类型 | 必选 | 默认值 | 描述 |
|------|--------|:----:|--------|-------------|
| cell | Cell | ✓ | | 节点/边。 |
| newId |string | ✓ | | 新的 ID。 |
### getCells()
```sign

View File

@@ -282,6 +282,21 @@ getCellById(id: string)
|------|--------|:----:|--------|-------------|
| cell | string | ✓ | | 节点/边的 ID。 |
### updateCellId(...)
```sign
updateCellId(cell: Cell, newId: string)
```
更新节点或者边的 ID会返回新创建的节点/边。
<span class="tag-param">参数<span>
| 名称 | 类型 | 必选 | 默认值 | 描述 |
|------|--------|:----:|--------|-------------|
| cell | Cell | ✓ | | 节点/边。 |
| newId |string | ✓ | | 新的 ID。 |
### getCells()
```sign