Skip to content
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
30 changes: 29 additions & 1 deletion packages/g6/src/behaviors/drag-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getBBoxSize, getCombinedBBox } from '../utils/bbox';
import { isToBeDestroyed } from '../utils/element';
import { idOf } from '../utils/id';
import { subStyleProps } from '../utils/prefix';
import { Shortcut, ShortcutKey } from '../utils/shortcut';
import { divide, rotate, subtract } from '../utils/vector';
import type { BaseBehaviorOptions } from './base-behavior';
import { BaseBehavior } from './base-behavior';
Expand All @@ -32,6 +33,14 @@ export interface DragElementOptions extends BaseBehaviorOptions, Prefix<'shadow'
* @defaultValue ['node', 'combo'].includes(event.targetType)
*/
enable?: boolean | ((event: IElementDragEvent) => boolean);
/**
* <zh/> 触发拖拽的方式
* 支持按下组合键才能触发拖拽元素
*
* <en/> The way to trigger drag element
* Support triggering by pressing a combination of keys
*/
trigger?: ShortcutKey;
/**
* <zh/> 拖拽操作效果
* - `'link'`: 将拖拽元素置入为目标元素的子元素
Expand Down Expand Up @@ -125,6 +134,7 @@ export class DragElement extends BaseBehavior<DragElementOptions> {
static defaultOptions: Partial<DragElementOptions> = {
animation: true,
enable: (event) => ['node', 'combo'].includes(event.targetType),
trigger: [],
dropEffect: 'move',
state: 'selected',
hideEdge: 'none',
Expand Down Expand Up @@ -156,8 +166,12 @@ export class DragElement extends BaseBehavior<DragElementOptions> {

private isDragging: boolean = false;

private shortcut: Shortcut;

constructor(context: RuntimeContext, options: DragElementOptions) {
super(context, Object.assign({}, DragElement.defaultOptions, options));
this.shortcut = new Shortcut(context.graph);

this.onDragStart = this.onDragStart.bind(this);
this.onDrag = this.onDrag.bind(this);
this.onDragEnd = this.onDragEnd.bind(this);
Expand Down Expand Up @@ -332,6 +346,19 @@ export class DragElement extends BaseBehavior<DragElementOptions> {
else canvas.setCursor(cursor?.default || 'default');
};

/**
* <zh/> 当前按键是否和 trigger 配置一致
*
* <en/> Is the current key consistent with the trigger configuration
* @returns <zh/> 是否一致 | <en/> Is consistent
* @internal
*/
protected isKeydown(): boolean {
const { trigger } = this.options;
if (!trigger?.length) return true;
return this.shortcut.match(trigger);
}

/**
* <zh/> 验证元素是否允许拖拽
*
Expand All @@ -346,7 +373,8 @@ export class DragElement extends BaseBehavior<DragElementOptions> {
isToBeDestroyed(event.target) ||
// @ts-expect-error private property
// 避免动画冲突,在combo/node折叠展开过程中不触发
this.context.graph.isCollapsingExpanding
this.context.graph.isCollapsingExpanding ||
!this.isKeydown()
)
return false;
const { enable } = this.options;
Expand Down
23 changes: 12 additions & 11 deletions packages/site/docs/manual/behavior/DragElement.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,18 @@ const graph = new Graph({

## Configuration Options

| Option | Description | Type | Default | Required |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- | ---------------------------------------------- | -------- |
| type | Behavior type name | string | `drag-element` | ✓ |
| key | Unique identifier for the behavior, used for subsequent operations | string | - | |
| enable | Whether to enable the drag function, by default nodes and combos can be dragged | boolean \| ((event: IElementDragEvent) => boolean) | `['node', 'combo'].includes(event.targetType)` | |
| animation | Whether to enable drag animation | boolean | true | |
| state | Identifier for the selected state of nodes, when multi-selection is enabled, it will find the selected nodes based on this state | string | `selected` | |
| dropEffect | Defines the operation effect after dragging ends, optional values are: <br/>- `link`: Set the dragged element as a child of the target element <br/>- `move`: Move the element and automatically update the size of the parent element (such as combo) <br/>- `none`: Only update the position of the drag target without performing other operations | `link` \| `move` \| `none` | `move` | |
| hideEdge | Controls the display state of edges during dragging, optional values are: <br/>- `none`: Do not hide any edges <br/>- `out`: Hide edges with the current node as the source node <br/>- `in`: Hide edges with the current node as the target node <br/>- `both`: Hide all edges related to the current node <br/>- `all`: Hide all edges in the graph <br/>⚠️ Note: When `shadow` (ghost node) is enabled, the `hideEdge` configuration will not take effect. | `none` \| `all` \| `in` \| `out` \| `both` | `none` | |
| shadow | Whether to enable ghost nodes, which use a shape to follow the mouse movement. [Customize ghost node style](#shadow-style-configuration) ⚠️Note: React nodes do not support enabling | boolean | false | |
| cursor | Customize the mouse style during dragging, [configuration options](#cursor) | { default?: Cursor; grab: Cursor; grabbing: Cursor } | - | |
| Option | Description | Type | Default | Required |
| ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------- | ---------------------------------------------- | -------- |
| type | Behavior type name | string | `drag-element` | ✓ |
| key | Unique identifier for the behavior, used for subsequent operations | string | - | |
| enable | Whether to enable the drag function, by default nodes and combos can be dragged | boolean \| ((event: IElementDragEvent) => boolean) | `['node', 'combo'].includes(event.targetType)` | |
| animation | Whether to enable drag animation | boolean | true | |
| state | Identifier for the selected state of nodes, when multi-selection is enabled, it will find the selected nodes based on this state | string | `selected` | |
| dropEffect | Defines the operation effect after dragging ends, optional values are: <br/>- `link`: Set the dragged element as a child of the target element <br/>- `move`: Move the element and automatically update the size of the parent element (such as combo) <br/>- `none`: Only update the position of the drag target without performing other operations | `link` \| `move` \| `none` | `move` | |
| hideEdge | Controls the display state of edges during dragging, optional values are: <br/>- `none`: Do not hide any edges <br/>- `out`: Hide edges with the current node as the source node <br/>- `in`: Hide edges with the current node as the target node <br/>- `both`: Hide all edges related to the current node <br/>- `all`: Hide all edges in the graph <br/>⚠️ Note: When `shadow` (ghost node) is enabled, the `hideEdge` configuration will not take effect. | `none` \| `all` \| `in` \| `out` \| `both` | `none` | |
| shadow | Whether to enable ghost nodes, which use a shape to follow the mouse movement. [Customize ghost node style](#shadow-style-configuration) ⚠️Note: React nodes do not support enabling | boolean | false | |
| cursor | Customize the mouse style during dragging, [configuration options](#cursor) | { default?: Cursor; grab: Cursor; grabbing: Cursor } | - | |
| trigger | Press this shortcut key in combination with mouse perform drag element **Key reference:** _<a href="https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values" target="_blank" rel="noopener noreferrer">MDN Key Values</a>_. If set to an **empty array**, it means drag element can be performed with mouse without pressing other keys <br/> ⚠️ Note, setting `trigger` to `['drag']` will cause the `drag-canvas` behavior to fail. The two cannot be configured simultaneously. | string[] \| (`Control` \| `Shift`\| `Alt` \| `......`)[] | [`shift`] | |

### cursor

Expand Down
Loading
Loading