Skip to content

Commit

Permalink
fix: destroy shape after change icon type
Browse files Browse the repository at this point in the history
  • Loading branch information
antv authored and Aaron committed Aug 6, 2024
1 parent 7ce727a commit c218dcc
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
33 changes: 33 additions & 0 deletions packages/g6/__tests__/bugs/element-node-icon-switch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createGraph } from '@@/utils';

describe('bug: element-node-icon-switch', () => {
it('change node icon', async () => {
const graph = createGraph({
animation: false,
data: {
nodes: [{ id: 'node-1', style: { x: 50, y: 50, iconText: 'Text' } }],
},
node: {
style: {},
},
});

await graph.draw();

await expect(graph).toMatchSnapshot(__filename, 'text-icon');

graph.updateNodeData([
{
id: 'node-1',
style: {
iconText: '',
iconSrc: 'https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*AzSISZeq81IAAAAAAAAAAAAADmJ7AQ/original',
},
},
]);

await graph.draw();

await expect(graph).toMatchSnapshot(__filename, 'image-icon');
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion packages/g6/src/elements/shapes/base-shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
}

// create
if (!target || target.destroyed) {
if (!target || target.destroyed || !(target instanceof Ctor)) {
target?.destroy();

const instance = new Ctor({ className, style });
container.appendChild(instance);
this.shapeMap[className] = instance;
Expand Down
9 changes: 5 additions & 4 deletions packages/g6/src/elements/shapes/icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ export class Icon extends BaseShape<IconStyleProps> {
super(options);
}

private isGImage() {
return !!this.getAttribute('src');
private isImage() {
const { src } = this.attributes;
return !!src;
}

protected getIconStyle(attributes: IconStyleProps = this.attributes): IconStyleProps {
const { width = 0, height = 0 } = attributes;
const style = this.getGraphicStyle(attributes);
if (this.isGImage()) {
if (this.isImage()) {
return {
x: -width / 2,
y: -height / 2,
Expand All @@ -47,6 +48,6 @@ export class Icon extends BaseShape<IconStyleProps> {
}

public render(attributes = this.attributes, container: Group = this): void {
this.upsert('icon', (this.isGImage() ? GImage : GText) as any, this.getIconStyle(attributes), container);
this.upsert('icon', (this.isImage() ? GImage : GText) as any, this.getIconStyle(attributes), container);
}
}

0 comments on commit c218dcc

Please sign in to comment.