diff --git a/packages/x6/src/model/cell.ts b/packages/x6/src/model/cell.ts index 87e94587a7d..c9b303af8ff 100644 --- a/packages/x6/src/model/cell.ts +++ b/packages/x6/src/model/cell.ts @@ -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]() { @@ -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() @@ -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 diff --git a/sites/x6-sites/docs/api/model/cell.zh.md b/sites/x6-sites/docs/api/model/cell.zh.md index cb93e6aa361..21a46a5a46f 100644 --- a/sites/x6-sites/docs/api/model/cell.zh.md +++ b/sites/x6-sites/docs/api/model/cell.zh.md @@ -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++}` +} +``` \ No newline at end of file