Is your feature request related to a problem? Please describe.
KeyHandler is a standalone object: it is created with new KeyHandler(graph), and the graph knows nothing about it.
This is fine when the handler is created to bind keys, since the instance is then stored and configured. But a KeyHandler is also needed without any key binding: it is the only caller of AbstractGraph.escape, so it is what makes the Escape key cancel the gesture in progress (moving cells, creating a connection, resizing a cell). To get this default behavior, applications and stories create an instance that they do not even store:
This code looks useless. Static analysis reports it (SonarCloud rule S1848 "useless object instantiation"), and the instances of the Storybook stories were about to be removed for this reason, see #910. RubberBandHandler had the same problem before it became a plugin: it was also instantiated without being stored.
Independently of how it is instantiated, the lifecycle of the handler is left to the application. The handler installs a keydown listener on the document element, which KeyHandler.onDestroy removes, but AbstractGraph.destroy does not call it, since the graph does not know the handler. So the application has to keep each instance somewhere, only to remember to destroy it along with the graph, and there is no way to retrieve the instance from the graph, for instance to bind a key in another part of the application.
Describe the solution you'd like
Provide the key handling as a graph plugin, so that the graph is the single place managing it:
- the graph creates it when the plugin is registered, so getting the Escape handling no longer needs an instance created on its own;
- the graph destroys it, through
GraphPlugin.onDestroy, so the document listener is removed with the graph without the application keeping the instance;
- the instance can be retrieved at any time with
graph.getPlugin(...), to bind keys.
The plugin is not meant to be part of the default plugins of Graph: no KeyHandler is created by default today, and RubberBandHandler was not added to the default plugins either when it became a plugin. An application opts in by adding the plugin to the plugins option of the graph.
To decide at implementation time:
- whether
KeyHandler itself becomes the plugin, or a new plugin class extends or wraps the current implementation;
- how to configure it, for instance its event target, which probably needs the configuration of plugins at graph construction time, see Configure plugins at graph construction time #1149;
- how
EditorKeyHandler, which creates its own KeyHandler, uses the plugin.
KeyHandler reads the deprecated KeyboardEvent.keyCode. A handler based on KeyboardEvent.key is planned in #1317, so the name and the id of the plugin should leave room for that new implementation, rather than tie the plugin to the current KeyHandler class.
Describe alternatives you've considered
- Keep the standalone class and document why an instance without key binding is needed. This is the current state, see the JSDoc of
KeyHandler: it explains the code, but keeps the unstored instance, the static analysis warning, and leaves the destruction of the handler to the application.
- Store the instance in a variable that is never used. It does not make the intent clearer, and the application still has to destroy the handler itself.
Additional context