Skip to content

Commit

Permalink
add ability to disable hotkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
amcdnl committed Aug 23, 2021
1 parent 9fe8bf0 commit b670d4a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
5 changes: 5 additions & 0 deletions docs/Helpers/Selection.stories.mdx
Expand Up @@ -33,6 +33,11 @@ export interface SelectionProps {
*/
edges?: EdgeData[];

/**
* Hotkey types.
*/
hotkeys?: HotkeyTypes[];

/**
* Disabled or not.
*/
Expand Down
23 changes: 17 additions & 6 deletions src/helpers/useSelection.ts
Expand Up @@ -3,6 +3,8 @@ import { useHotkeys } from 'reakeys';
import { EdgeData, NodeData } from '../types';
import { removeNode } from './crudHelpers';

export type HotkeyTypes = 'selectAll' | 'deselect' | 'delete';

export interface SelectionProps {
/**
* Current selections.
Expand All @@ -26,6 +28,11 @@ export interface SelectionProps {
*/
disabled?: boolean;

/**
* Hotkey types
*/
hotkeys?: HotkeyTypes[];

/**
* On selection change.
*/
Expand Down Expand Up @@ -91,6 +98,7 @@ export const useSelection = ({
selections = [],
nodes = [],
edges = [],
hotkeys = ['selectAll', 'deselect', 'delete'],
disabled,
onSelection,
onDataChange
Expand All @@ -115,7 +123,7 @@ export const useSelection = ({
if (!disabled) {
const has = internalSelections.includes(item);
if (has) {
const next = internalSelections.filter((i) => i !== item);
const next = internalSelections.filter(i => i !== item);
onSelection?.(next);
setInternalSelections(next);
}
Expand Down Expand Up @@ -151,7 +159,7 @@ export const useSelection = ({
setMetaKeyDown(false);
};

const onKeyDown = (event) => {
const onKeyDown = event => {
event.preventDefault();
setMetaKeyDown(event.metaKey || event.ctrlKey);
};
Expand All @@ -165,13 +173,14 @@ export const useSelection = ({
{
name: 'Select All',
keys: 'mod+a',
disabled: !hotkeys.includes('selectAll'),
category: 'Canvas',
description: 'Select all nodes and edges',
callback: (event) => {
callback: event => {
event.preventDefault();

if (!disabled) {
const next = nodes.map((n) => n.id);
const next = nodes.map(n => n.id);
onDataChange?.(nodes, edges);
onSelection?.(next);
setInternalSelections(next);
Expand All @@ -181,9 +190,10 @@ export const useSelection = ({
{
name: 'Delete Selections',
category: 'Canvas',
disabled: !hotkeys.includes('delete'),
description: 'Delete selected nodes and edges',
keys: 'backspace',
callback: (event) => {
callback: event => {
if (!disabled) {
event.preventDefault();
const result = removeNode(nodes, edges, internalSelections);
Expand All @@ -196,9 +206,10 @@ export const useSelection = ({
{
name: 'Deselect Selections',
category: 'Canvas',
disabled: !hotkeys.includes('deselect'),
description: 'Deselect selected nodes and edges',
keys: 'escape',
callback: (event) => {
callback: event => {
if (!disabled) {
event.preventDefault();
onSelection?.([]);
Expand Down

0 comments on commit b670d4a

Please sign in to comment.