Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 支持自定义节点/边的唯一标识 #4442 #4444

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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++}`
}
```
Loading