Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create-edge behavior supports history tracking #6043

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pluginHistory } from '@/__tests__/demos';
import type { History } from '@/src';
import { Graph } from '@/src';
import { ComboEvent, Graph, NodeEvent } from '@/src';
import { createDemoGraph } from '@@/utils';

describe('history plugin', () => {
Expand Down Expand Up @@ -106,6 +106,21 @@ describe('history plugin', () => {
history.undo();
});

it('create-edge', async () => {
graph.setBehaviors((prev) => [
...prev,
{ type: 'create-edge', trigger: 'click', style: { stroke: 'red', lineWidth: 2 } },
]);
graph.emit(NodeEvent.CLICK, { target: { id: 'node-1' }, targetType: 'node' });
graph.emit(ComboEvent.CLICK, { target: { id: 'combo-2' }, targetType: 'combo' });
await expect(graph).toMatchSnapshot(__filename, 'create-edge');
history.undo();
await expect(graph).toMatchSnapshot(__filename, 'create-edge-undo');
history.redo();
await expect(graph).toMatchSnapshot(__filename, 'create-edge-redo');
history.undo();
});

it('beforeAddCommand', async () => {
const undoStackLen = history.undoStack.length;

Expand Down
10 changes: 6 additions & 4 deletions packages/g6/src/behaviors/create-edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class CreateEdge extends BaseBehavior<CreateEdgeOptions> {

private handleCreateEdge = async (event: IElementEvent) => {
if (!this.validate(event)) return;
const { graph, canvas } = this.context;
const { graph, canvas, batch, element } = this.context;
const { style } = this.options;

if (this.source) {
Expand All @@ -125,6 +125,7 @@ export class CreateEdge extends BaseBehavior<CreateEdgeOptions> {
return;
}

batch!.startBatch();
Aarebecca marked this conversation as resolved.
Show resolved Hide resolved
canvas.setCursor('crosshair');
this.source = this.getSelectedNodeIDs([event.target.id])[0];

Expand All @@ -149,6 +150,7 @@ export class CreateEdge extends BaseBehavior<CreateEdgeOptions> {
},
},
]);
await element!.draw({ animation: false })?.finished;
};

private updateAssistEdge = async (event: IPointerEvent) => {
Expand Down Expand Up @@ -176,14 +178,14 @@ export class CreateEdge extends BaseBehavior<CreateEdgeOptions> {

private cancelEdge = async () => {
if (!this.source) return;
const { graph, element, canvas } = this.context;
canvas.setCursor('default');
const { graph, element, batch } = this.context;

graph.removeNodeData([ASSIST_NODE_ID]);

this.source = undefined;

await element!.draw({ animation: false, silence: true })?.finished;
await element!.draw({ animation: false })?.finished;
batch!.endBatch();
};

private getSelectedNodeIDs(currTarget: ID[]) {
Expand Down
Loading