Skip to content

Commit

Permalink
fix: 🐛 add updateCellId api (antvis#1739)
Browse files Browse the repository at this point in the history
  • Loading branch information
NewByVector authored Jan 8, 2022
1 parent 0dd0c13 commit 78cdb3b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/x6/src/graph/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions packages/x6/src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
15 changes: 15 additions & 0 deletions sites/x6-sites/docs/api/graph/model.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions sites/x6-sites/docs/api/graph/model.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 78cdb3b

Please sign in to comment.