Skip to content

Commit

Permalink
feat: 支持自定义节点/边的唯一标识 #4442
Browse files Browse the repository at this point in the history
  • Loading branch information
Caaalabash committed Sep 14, 2024
1 parent 003491b commit f747869
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/x6/src/model/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export class Cell<
}, metadata)
}

// eslint-disable-next-line
public static customId(metadata: Cell.Metadata = {}) {
return StringExt.uuid()
}

// #endregion

protected get [Symbol.toStringTag]() {
Expand All @@ -107,7 +112,7 @@ export class Cell<
this.preprocess(metadata),
)

this.id = props.id || StringExt.uuid()
this.id = props.id || Cell.customId(metadata)
this.store = new Store(props)
this.animation = new Animation(this)
this.setup()
Expand Down Expand Up @@ -140,7 +145,7 @@ export class Cell<
const props = ctor.applyPropHooks(this, metadata)

if (id == null && ignoreIdCheck !== true) {
props.id = StringExt.uuid()
props.id = Cell.customId(metadata)
}

return props as Properties
Expand Down
21 changes: 21 additions & 0 deletions sites/x6-sites/docs/api/model/cell.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2200,3 +2200,24 @@ const rect = graph.addNode({
},
})
```

## 静态方法

### customId

我们在 Cell 基类上提供了一个静态方法 `customId` 来快速实现自定义节点/边ID。

```ts
customId(metadata: Cell.Metadata): string
```

例如:

```ts
import { Cell } from '@antv/x6'

let sid = 0
Cell.customId = ({ shape }) => {
return `${shape}_${sid++}`
}
```

0 comments on commit f747869

Please sign in to comment.