From f747869f2498991efecb105c817940159910e7e7 Mon Sep 17 00:00:00 2001 From: Caaalabash Date: Sat, 14 Sep 2024 10:52:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E8=8A=82=E7=82=B9/=E8=BE=B9=E7=9A=84=E5=94=AF?= =?UTF-8?q?=E4=B8=80=E6=A0=87=E8=AF=86=20#4442?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/x6/src/model/cell.ts | 9 +++++++-- sites/x6-sites/docs/api/model/cell.zh.md | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) 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