From 04656f33f40edbc6c913e91f8f6aa94864525fbe Mon Sep 17 00:00:00 2001 From: Lloyd Zhou Date: Fri, 17 Feb 2023 21:27:40 +0800 Subject: [PATCH] feat: add updateNode/updateEdge API #3235 (#3239) * feat: add updateNode/updateEdge API #3235 * feat: add updateNode/updateEdge API #3235 * feat: update return value * fix: call error create function --- packages/x6/src/model/model.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/packages/x6/src/model/model.ts b/packages/x6/src/model/model.ts index dd23458b1ad..997951036f2 100644 --- a/packages/x6/src/model/model.ts +++ b/packages/x6/src/model/model.ts @@ -244,6 +244,13 @@ export class Model extends Basecoat { return node } + updateNode(metadata: Node.Metadata, options: Model.SetOptions = {}) { + const node = this.createNode(metadata) + const prop = node.getProp() + node.dispose() + return this.updateCell(prop, options) + } + createNode(metadata: Node.Metadata) { return Node.create(metadata) } @@ -258,6 +265,13 @@ export class Model extends Basecoat { return Edge.create(metadata) } + updateEdge(metadata: Edge.Metadata, options: Model.SetOptions = {}) { + const edge = this.createEdge(metadata) + const prop = edge.getProp() + edge.dispose() + return this.updateCell(prop, options) + } + addCell(cell: Cell | Cell[], options: Model.AddOptions = {}) { if (Array.isArray(cell)) { return this.addCells(cell, options) @@ -295,6 +309,23 @@ export class Model extends Basecoat { return this } + updateCell(prop: Cell.Properties, options: Model.SetOptions = {}): boolean { + const existing = prop.id && this.getCell(prop.id) + if (existing) { + return this.batchUpdate( + 'update', + () => { + Object.keys(prop).forEach((key) => + existing.setProp(key, prop[key], options), + ) + return true + }, + prop, + ) + } + return false + } + removeCell(cellId: string, options?: Collection.RemoveOptions): Cell | null removeCell(cell: Cell, options?: Collection.RemoveOptions): Cell | null removeCell(