Skip to content

Commit

Permalink
refactor: expose hover-activate getActiveIds function
Browse files Browse the repository at this point in the history
  • Loading branch information
yvonneyx committed Sep 6, 2024
1 parent 706b572 commit 9909d95
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions packages/g6/src/behaviors/hover-activate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface HoverActivateOptions extends BaseBehaviorOptions {
* - `1` means the current node and its directly adjacent nodes and edges are activated, etc
* @defaultValue 0
*/
degree?: number;
degree?: number | ((event: IPointerEvent) => number);
/**
* <zh/> 指定边的方向
* - `'both'`: 表示激活当前节点的所有关系
Expand Down Expand Up @@ -133,16 +133,29 @@ export class HoverActivate extends BaseBehavior<HoverActivateOptions> {
else onHoverEnd?.(event);
};

protected getActiveIds(event: IPointerEvent<Element>) {
const { graph } = this.context;
const { degree, direction } = this.options;
const { targetType, target } = event;

return degree
? getElementNthDegreeIds(
graph,
targetType as ElementType,
target.id,
typeof degree === 'function' ? degree(event) : degree,
direction,
)
: [target.id];
}

private updateElementsState = (event: IPointerEvent<Element>, add: boolean) => {
if (!this.options.state && !this.options.inactiveState) return;

const { graph } = this.context;
const { state, degree, direction, animation, inactiveState } = this.options;
const { targetType, target } = event;
const { state, animation, inactiveState } = this.options;

const activeIds = degree
? getElementNthDegreeIds(graph, targetType as ElementType, target.id, degree, direction)
: [target.id];
const activeIds = this.getActiveIds(event);

const states: Record<ID, State[]> = {};

Expand All @@ -162,8 +175,11 @@ export class HoverActivate extends BaseBehavior<HoverActivateOptions> {
const states: Record<ID, State[]> = {};
ids.forEach((id) => {
const currentState = graph.getElementState(id);
const updatedState = add ? [...currentState, state] : currentState.filter((s) => s !== state);
states[id] = updatedState;
if (add) {
states[id] = currentState.includes(state) ? currentState : [...currentState, state];
} else {
states[id] = currentState.filter((s) => s !== state);
}
});
return states;
};
Expand Down

0 comments on commit 9909d95

Please sign in to comment.