g6/packages/site/docs/manual/middle/states/mode.en.md

97 lines
3.6 KiB
Markdown
Raw Normal View History

2023-02-02 10:31:36 +08:00
---
title: Interaction Mode
order: 3
---
## What is Mode
When a user interacts with a graph, there may be different interaction modes due to different intents. For example, clicking a node in edit mode requires a pop-up window for the user to edit, and clicking a node in view mode requires selecting a node.
To address the problem above, G6 provides the interaction Mode. It is a manage mechanism for the [Behavior](/en/docs/manual/middle/states/defaultBehavior) on a graph. There can be multiple interaction modes on a graph, each interaction mode contains multiple interaction [Behavior](/en/docs/manual/middle/states/defaultBehavior)s.
For example, there are two modes on a graph: default and edit:
- default mode contains click to select node behavior and drag canvas behavior;
- edit mode contains click node to pop up an editing window behavior and drag node behavior;
Default mode takes effect by default, which means the node will be selected by clicking insteand of a editing window pops up. You can switch to edit mode by simple code, then the behaviors in the defualt mode will not take effect any more, which means the editing window will pop up when user clicks a node.
## Configure Mode
Configure the `modes` when instantiating a Graph:
```javascript
const graph = new G6.Graph({
container: 'mountNode',
width: 500,
height: 500,
modes: {
V5 feat/cubic edge,cubic horizon edge,cubic vertical edge (#4669) * chore: update dependencies * update jest.config.js for using `@kayahr/jest-electron-runner` * update rollup.config.js for using `@rollup/plugin-terser` * chore: add line-edge integration test * chore: done feat/cubic-edge chore: add integration test chore: add unit test(but there have several tests TODO ) * feat: v5-cubic-horizon-edge *feat: v5-cubic-horizon-edge *chore: test/unit/v5-cubic-horizon-edge *chore: test/integration/v5-cubic-horizon-edge * feat: v5-cubic-horizon-edge *feat: v5-cubic-horizon-edge *chore: test/unit/v5-cubic-horizon-edge *chore: test/integration/v5-cubic-horizon-edge fix: curve bug * chore: lint fix & use English comments * chore: change test svg link & fix cubic offset *chore: change test svg link *fix: fix the cubic offset * chore: fix rebase issue * Feat: add 'topRight' position configuration in BadgesShapes (#4624) * fix: support position config:'topRight' * add demo to reproduce bug * chore: fix rebase conflict * V5 fix label shape error (#4645) * update: update demo for bug reproduce * fix: fix the bug * v5-rect#1 (#4640) * feat: add RectNode * add: add rect test demo * add: add rect test demo * update: update RectNode with less override * update: remove 'console.log' * update: remove comment * update: remove cn comment * add: add rect-spec.ts for unit test --------- Co-authored-by: Yanyan Wang <yanyanwang93@gmail.com> * fix: implement onZoom as empty function for 3d node (#4647) * fix: implement onZoom as empty function for 3d node * chore: refine * chore: lint * add: make the error message more readable (#4666) * feat: add contextmenu event for graph canvas, node, and edge (#4675) * feat: v5-quadratic (#4656) * add: add a test demo * feat: add quadratic * add: add unit test demo * fix: add seItemState(selected、active、highlight、inactive、disable) test into spec. * chore: fix rebase conflicts * chore: done feat/cubic-edge chore: add integration test chore: add unit test(but there have several tests TODO ) * feat: v5-cubic-horizon-edge *feat: v5-cubic-horizon-edge *chore: test/unit/v5-cubic-horizon-edge *chore: test/integration/v5-cubic-horizon-edge * feat: v5-cubic-horizon-edge *feat: v5-cubic-horizon-edge *chore: test/unit/v5-cubic-horizon-edge *chore: test/integration/v5-cubic-horizon-edge fix: curve bug * chore: lint fix & use English comments * chore: trim up code format *chore: use relative import path *chore: delete unused imports *fix: fix the rebase imports * chore: fix base.ts * chore: fix rebase conflicts * chore: elint fix format --------- Co-authored-by: MaLaiXiGua <55946653+zqqcee@users.noreply.github.com> Co-authored-by: MaLaiXiGua <941107676@qq.com> Co-authored-by: Yanyan Wang <yanyanwang93@gmail.com> Co-authored-by: MaLaiXiGua <zqqcee@163.com>
2023-06-26 16:54:52 +08:00
// supported behavior
2023-02-02 10:31:36 +08:00
default: ['drag-canvas', 'zoom-canvas'],
edit: ['click-select'],
},
});
```
There are two modes on the graph defined above: `default` and `edit`. The `default` mode contains two [Behavior](/en/docs/manual/middle/states/defaultBehavior)s: `'drag-canvas'` and ` '``zoom-canvas' ` with default configurations.
## Switch Mode
The `default` mode takes effect by default. Users are allowed to drag and zoom the canvas. Swich the mode to edit mode by ` graph.``setMode('edit') ` to select a node by clicking.
```javascript
graph.setMode('edit');
```
Now, the graph supports clicking to select nodes. The `'drag-canvas'` and `'zoom-canvas'` behaviors in `default` do not take effect any more.
`setMode` calls the following operations inside:
- Unbind all the event listeners of current mode;
- Generate new Behaviors. Initialize the events;
- Bind event listeners to the new Behaviors.
## Edit Mode
If there are existing Behaviors ([Built-in Behavior](/en/docs/manual/middle/states/defaultBehavior) or [Custom Behavior](/en/docs/manual/middle/states/custom-behavior)), You can add them to a mode by `graph.addBehaviors`, and also remove some Behaviors by `graph.removeBehaviors`:
```javascript
// Add drag-canvas with configurations from default mode
graph.addBehaviors('drag-canvas', 'default');
// Remove drag-canvas from default mode
graph.removeBehaviors('drag-canvas', 'default');
// Add drag-canvas with configurations into edit mode
graph.addBehaviors(
{
type: 'drag-canvas',
direction: 'x',
},
'edit',
);
// Remove drag-canvas from edit mode
graph.removeBehaviors('drag-canvas', 'edit');
// Add multiple behaviors into default mode
graph.addBehaviors(['drag-canvas', 'zoom-canvas'], 'default');
// Remove multiple behaviors from default mode
graph.removeBehaviors(['drag-canvas', 'zoom-canvas'], 'default');
// --------
// Update the behavior 'zoom-canvas' from mode 'default'
graph.updateBehavior('zoom-canvas', { sensitivity: 1.5, enableOptimize: true}, 'default');
// update the behavior 'click-select' from mode 'select'
graph.updateBehavior('click-select', { trigger: 'ctrl' }, 'select');
```
## Related Reading
- [Built-in Behavior](/en/docs/manual/middle/states/defaultBehavior)
- [Custom Behavior](/en/docs/manual/middle/states/custom-behavior)