Skip to content

Commit

Permalink
fix: fix cr issues
Browse files Browse the repository at this point in the history
  • Loading branch information
yvonneyx committed Aug 16, 2024
1 parent e49dd08 commit d7c877d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/g6/__tests__/demos/plugin-snapline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Graph } from '@antv/g6';
import { Graph, Node } from '@antv/g6';

export const pluginSnapline: TestCase = async (context) => {
const graph = new Graph({
Expand Down Expand Up @@ -46,7 +46,7 @@ export const pluginSnapline: TestCase = async (context) => {
.onChange((filter: boolean) => {
graph.updatePlugin({
key: 'snapline',
filter: (nodeId: string) => (filter ? nodeId !== 'node3' : true),
filter: (node: Node) => (filter ? node.id !== 'node3' : true),
});
graph.render();
}),
Expand Down
4 changes: 2 additions & 2 deletions packages/g6/__tests__/unit/plugins/snapline.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NodeEvent, type Graph } from '@/src';
import { Node, NodeEvent, type Graph } from '@/src';
import { pluginSnapline } from '@@/demos';
import { createDemoGraph } from '../../utils';

Expand Down Expand Up @@ -61,7 +61,7 @@ describe('plugin snapline', () => {
await expect(graph).toMatchSnapshot(__filename, `offset-infinity`);
graph.emit(NodeEvent.DRAG_END, { target: node });

graph.updatePlugin({ key: 'snapline', filter: (nodeId: string) => nodeId !== 'node2' });
graph.updatePlugin({ key: 'snapline', filter: (node: Node) => node.id !== 'node2' });
graph.render();
graph.emit(NodeEvent.DRAG_START, { target: node, targetType: 'node' });
graph.emit(NodeEvent.DRAG, { target: node, dx: 0, dy: 0 });
Expand Down
12 changes: 12 additions & 0 deletions packages/g6/__tests__/unit/runtime/viewport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ describe('ViewportController', () => {
// @ts-expect-error
expect(graph.context.viewport.getBBoxInViewport(bbox).halfExtents).toBeCloseTo([100, 100, 0]);
});

it('isInViewport', async () => {
await graph.translateTo([100, 100]);
// @ts-expect-error
expect(graph.context.viewport?.isInViewport([0, 0])).toBe(false);
// @ts-expect-error
expect(graph.context.viewport?.isInViewport([100, 100])).toBe(true);
const bbox = new AABB();
bbox.setMinMax([0, 0, 0], [100, 100, 0]);
// @ts-expect-error
expect(graph.context.viewport?.isInViewport(bbox)).toBe(true);
});
});

describe('Viewport Fit without Animation', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/g6/src/plugins/snapline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface SnaplineOptions extends BasePluginOptions {
* <en/> Filter, used to filter nodes that do not need to be used as references
* @defaultValue `() => true`
*/
filter?: (nodeId: string, node: Node) => boolean;
filter?: (node: Node) => boolean;
}

const defaultLineStyle: LineStyleProps = { x1: 0, y1: 0, x2: 0, y2: 0, visibility: 'hidden' };
Expand Down Expand Up @@ -120,12 +120,12 @@ export class Snapline extends BasePlugin<SnaplineOptions> {
// 不考虑超出画布视口范围、不可见的节点
// Nodes that are out of the canvas viewport range, invisible are not considered
const nodes = allNodes.filter((node) => {
return isVisible(node) && this.context.viewport?.isNodeInViewport(node);
return isVisible(node) && this.context.viewport?.isInViewport(node.getRenderBounds());
});

if (!filter) return nodes;

return nodes.filter((node) => filter(node.id, node));
return nodes.filter((node) => filter(node));
}

private hideSnapline() {
Expand Down
25 changes: 12 additions & 13 deletions packages/g6/src/runtime/viewport.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { AABB, ICamera } from '@antv/g';
import { clamp, isNumber, pick } from '@antv/util';
import { AnimationType, GraphEvent } from '../constants';
import type {
FitViewOptions,
ID,
Node,
Point,
TransformOptions,
Vector2,
ViewportAnimationEffectTiming,
} from '../types';
import type { FitViewOptions, ID, Point, TransformOptions, Vector2, ViewportAnimationEffectTiming } from '../types';
import { getAnimationOptions } from '../utils/animation';
import { getBBoxSize, getCombinedBBox } from '../utils/bbox';
import { getBBoxSize, getCombinedBBox, isPointInBBox } from '../utils/bbox';
import { AnimateEvent, ViewportEvent, emit } from '../utils/event';
import { isPoint } from '../utils/is';
import { parsePadding } from '../utils/padding';
import { add, divide, subtract } from '../utils/vector';
import type { RuntimeContext } from './types';
Expand Down Expand Up @@ -276,7 +269,14 @@ export class ViewportController {
return bboxInViewport;
}

public isNodeInViewport(node: Node) {
/**
* <zh/> 判断点或包围盒是否在视口中
*
* <en/> Determine whether the point or bounding box is in the viewport
* @param target - <zh/> 点或包围盒 | <en/> Point or bounding box
* @returns - <zh/> 是否在视口中 | <en/> Whether it is in the viewport
*/
public isInViewport(target: Point | AABB) {
const { graph } = this.context;
const size = this.getCanvasSize();

Expand All @@ -286,8 +286,7 @@ export class ViewportController {
const viewportBBox = new AABB();
viewportBBox.setMinMax([x1, y1, 0], [x2, y2, 0]);

const nodeBBox = node.getRenderBounds();
return viewportBBox.intersects(nodeBBox);
return isPoint(target) ? isPointInBBox(target, viewportBBox) : viewportBBox.intersects(target);
}

public cancelAnimation() {
Expand Down

0 comments on commit d7c877d

Please sign in to comment.