/
index.tsx
84 lines (71 loc) · 2.5 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import {
useShortcut,
store as keyboardShortcutsStore,
} from '@wordpress/keyboard-shortcuts';
import { __ } from '@wordpress/i18n';
import { stepSidebarKey, storeName, automationSidebarKey } from '../../store';
// See:
// https://github.com/WordPress/gutenberg/blob/9601a33e30ba41bac98579c8d822af63dd961488/packages/edit-post/src/components/keyboard-shortcuts/index.js
// https://github.com/WordPress/gutenberg/blob/0ee78b1bbe9c6f3e6df99f3b967132fa12bef77d/packages/edit-site/src/components/keyboard-shortcuts/index.js
export function KeyboardShortcuts(): null {
const { isSidebarOpened, selectedStep, savedState } = useSelect((select) => ({
isSidebarOpened: select(storeName).isSidebarOpened,
selectedStep: select(storeName).getSelectedStep,
savedState: select(storeName).getSavedState(),
}));
const { openSidebar, closeSidebar, save, toggleFeature } =
useDispatch(storeName);
const { registerShortcut } = useDispatch(keyboardShortcutsStore);
useEffect(() => {
void registerShortcut({
name: 'mailpoet/automation-editor/toggle-fullscreen',
category: 'global',
description: __('Toggle fullscreen mode.', 'mailpoet'),
keyCombination: {
modifier: 'secondary',
character: 'f',
},
});
void registerShortcut({
name: 'mailpoet/automation-editor/toggle-sidebar',
category: 'global',
description: __('Show or hide the settings sidebar.', 'mailpoet'),
keyCombination: {
modifier: 'primaryShift',
character: ',',
},
});
void registerShortcut({
name: 'mailpoet/automation-editor/save',
category: 'global',
description: __('Save your changes.', 'mailpoet'),
keyCombination: {
modifier: 'primary',
character: 's',
},
});
}, [registerShortcut]);
useShortcut('mailpoet/automation-editor/toggle-fullscreen', () => {
void toggleFeature('fullscreenMode');
});
useShortcut('mailpoet/automation-editor/toggle-sidebar', (event) => {
event.preventDefault();
if (isSidebarOpened()) {
void closeSidebar();
} else {
const sidebarToOpen = selectedStep()
? stepSidebarKey
: automationSidebarKey;
void openSidebar(sidebarToOpen);
}
});
useShortcut('mailpoet/automation-editor/save', (event) => {
event.preventDefault();
if (savedState === 'unsaved') {
void save();
}
});
return null;
}