feat: add getElementData api (#5814)

This commit is contained in:
Yuxin 2024-06-04 21:29:58 +08:00 committed by GitHub
parent 3f4810d9ae
commit e932c0ac7b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 0 deletions

View File

@ -134,6 +134,11 @@ describe('Graph', () => {
});
});
it('getElementData', () => {
expect(graph.getElementData('node-1').id).toEqual('node-1');
expect(graph.getElementData(['node-1']).map(idOf)).toEqual(['node-1']);
});
it('getXxxData/addXxxData/updateXxxData/removeXxxData', () => {
expect(graph.getNodeData('node-1').id).toEqual('node-1');
expect(graph.getNodeData(['node-1']).map(idOf)).toEqual(['node-1']);

View File

@ -442,6 +442,28 @@ export class Graph extends EventEmitter {
return this.context.model.getData();
}
/**
* <zh/>
*
* <en/> Get element data by ID
* @param id - <zh/> ID | <en/> element ID
* @returns <zh/> | <en/> element data
* @apiCategory data
*/
public getElementData(id: ID): ElementDatum;
/**
* <zh/>
*
* <en/> Get multiple element data in batch
* @param ids - <zh/> ID | <en/> element ID array
* @apiCategory data
*/
public getElementData(ids: ID[]): ElementDatum[];
public getElementData(ids: ID | ID[]): ElementDatum | ElementDatum[] {
if (Array.isArray(ids)) return ids.map((id) => this.context.model.getElementDataById(id));
return this.context.model.getElementDataById(ids);
}
/**
* <zh/>
*