diff --git a/packages/x6/src/graph/graph.ts b/packages/x6/src/graph/graph.ts index 5828a0fb38f..ccb918d207b 100644 --- a/packages/x6/src/graph/graph.ts +++ b/packages/x6/src/graph/graph.ts @@ -496,6 +496,10 @@ export class Graph extends Basecoat { return result } + updateCellId(cell: Cell, newId: string) { + return this.model.updateCellId(cell, newId) + } + // #endregion // #region view diff --git a/packages/x6/src/model/model.ts b/packages/x6/src/model/model.ts index fce467e31d7..b48fef139c5 100644 --- a/packages/x6/src/model/model.ts +++ b/packages/x6/src/model/model.ts @@ -306,6 +306,36 @@ export class Model extends Basecoat { 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', () => { diff --git a/sites/x6-sites/docs/api/graph/model.en.md b/sites/x6-sites/docs/api/graph/model.en.md index 4711c84cff4..255ac24bdc2 100644 --- a/sites/x6-sites/docs/api/graph/model.en.md +++ b/sites/x6-sites/docs/api/graph/model.en.md @@ -282,6 +282,21 @@ getCellById(id: string) |------|--------|:----:|--------|-------------| | cell | string | ✓ | | 节点/边的 ID。 | +### updateCellId(...) + +```sign +updateCellId(cell: Cell, newId: string) +``` + +更新节点或者边的 ID,会返回新创建的节点/边。 + +参数 + +| 名称 | 类型 | 必选 | 默认值 | 描述 | +|------|--------|:----:|--------|-------------| +| cell | Cell | ✓ | | 节点/边。 | +| newId |string | ✓ | | 新的 ID。 | + ### getCells() ```sign diff --git a/sites/x6-sites/docs/api/graph/model.zh.md b/sites/x6-sites/docs/api/graph/model.zh.md index ffa020cac43..1c2738f91af 100644 --- a/sites/x6-sites/docs/api/graph/model.zh.md +++ b/sites/x6-sites/docs/api/graph/model.zh.md @@ -282,6 +282,21 @@ getCellById(id: string) |------|--------|:----:|--------|-------------| | cell | string | ✓ | | 节点/边的 ID。 | +### updateCellId(...) + +```sign +updateCellId(cell: Cell, newId: string) +``` + +更新节点或者边的 ID,会返回新创建的节点/边。 + +参数 + +| 名称 | 类型 | 必选 | 默认值 | 描述 | +|------|--------|:----:|--------|-------------| +| cell | Cell | ✓ | | 节点/边。 | +| newId |string | ✓ | | 新的 ID。 | + ### getCells() ```sign