-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyboard.js
More file actions
204 lines (178 loc) · 8.48 KB
/
keyboard.js
File metadata and controls
204 lines (178 loc) · 8.48 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* Keyboard shortcuts and input handling for the dashboard
*/
class KeyboardManager {
constructor(dashboard) {
this.dashboard = dashboard;
this.setupKeyboardShortcuts();
}
handleArrowKeys(e, id) {
if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
e.preventDefault();
const increment = e.shiftKey ? 10 : 1;
const direction = e.key === 'ArrowUp' ? 1 : -1;
this.dashboard[id] = Math.max(this.dashboard.config[id].min, Math.min(this.dashboard.config[id].max, this.dashboard[id] + (increment * direction)));
e.target.value = this.dashboard[id];
this.dashboard.updateGrid();
}
}
setupKeyboardShortcuts() {
document.addEventListener('keydown', (e) => {
const active = document.activeElement;
const inputFocused = active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || active.contentEditable === 'true');
if (!inputFocused) {
// Shift+G to toggle grid visibility
if (e.shiftKey && e.key === 'G') {
e.preventDefault();
this.dashboard.toggleGridVisibility();
return;
}
// Shift+A to open add widget menu at cursor (edit mode only)
if (e.shiftKey && e.key === 'A' && this.dashboard.gridMode) {
e.preventDefault();
this.openWidgetMenuAtCursor();
return;
}
// Shift+N to create new dashboard (edit mode only)
if (e.shiftKey && e.key === 'N' && this.dashboard.gridMode) {
e.preventDefault();
this.dashboard.dashboardManager.createNewDashboard();
return;
}
}
if (!this.dashboard.gridMode) return; // Guard clause for grid mode
// Handle widget menu navigation
const widgetMenuOverlay = document.getElementById('widgetMenuOverlay');
if (widgetMenuOverlay && widgetMenuOverlay.classList.contains('show')) {
if (e.key === 'Escape') {
this.dashboard.closeWidgetMenu();
} else if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
e.preventDefault();
this.navigateWidgetMenu(e.key === 'ArrowDown' ? 1 : -1);
} else if (e.key === 'Enter') {
e.preventDefault();
this.selectFocusedWidget();
}
} else {
// ESC key to deselect widgets when menu is closed
if (e.key === 'Escape') {
this.dashboard.widgetManager.deselectAllWidgets();
}
if (!inputFocused && this.dashboard.gridMode) {
// Delete selected widget
if (e.key === 'Delete' || e.key === 'Backspace') {
const selected = document.querySelector('.widget.selected');
if (selected) {
e.preventDefault();
this.dashboard.widgetManager.deleteWidget(selected.id);
}
}
// Arrow keys to navigate between widgets
if (e.key === 'ArrowRight' || e.key === 'ArrowDown' || e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
const widgets = Array.from(document.querySelectorAll('.widget'));
if (widgets.length === 0) return;
e.preventDefault();
const selected = document.querySelector('.widget.selected');
const currentIdx = selected ? widgets.indexOf(selected) : -1;
const forward = e.key === 'ArrowRight' || e.key === 'ArrowDown';
let nextIdx;
if (currentIdx === -1) {
nextIdx = 0;
} else {
nextIdx = forward
? (currentIdx + 1) % widgets.length
: (currentIdx - 1 + widgets.length) % widgets.length;
}
this.dashboard.widgetManager.selectWidget(widgets[nextIdx]);
}
// [ send backward, ] bring forward
if (e.key === '[') {
const selected = document.querySelector('.widget.selected');
if (selected) {
e.preventDefault();
this.dashboard.widgetManager.sendBackward(selected.id);
}
}
if (e.key === ']') {
const selected = document.querySelector('.widget.selected');
if (selected) {
e.preventDefault();
this.dashboard.widgetManager.bringForward(selected.id);
}
}
}
}
});
// Click outside to deselect or close menu
document.addEventListener('click', (e) => {
// Close widget menu if clicking outside
const widgetMenuOverlay = document.getElementById('widgetMenuOverlay');
if (widgetMenuOverlay && widgetMenuOverlay.classList.contains('show')) {
if (!e.target.closest('.widget-menu') && !e.target.closest('#lpAddWidget')) {
this.dashboard.closeWidgetMenu();
}
}
// Deselect widgets if clicking on empty canvas (not clicking on widget, controls, config panel, left panel, or widget menu)
if (!e.target.closest('.widget') && !e.target.closest('.footer-bar') && !e.target.closest('.config-panel') && !e.target.closest('#leftPanel') && !e.target.closest('.widget-menu')) {
if (this.dashboard.widgetManager._justResized) {
this.dashboard.widgetManager._justResized = false;
} else {
this.dashboard.widgetManager.deselectAllWidgets();
}
}
});
// Window resize to update widget positions
window.addEventListener('resize', () => {
this.dashboard.updateWidgetPositions();
});
}
navigateWidgetMenu(direction) {
const widgetMenuOverlay = document.getElementById('widgetMenuOverlay');
if (!widgetMenuOverlay) return;
const options = widgetMenuOverlay.querySelectorAll('.widget-option');
const currentFocused = widgetMenuOverlay.querySelector('.widget-option.focused');
if (!currentFocused) {
// If no option is focused, focus the first one
if (options.length > 0) {
options[0].classList.add('focused');
options[0].focus();
}
return;
}
const currentIndex = Array.from(options).indexOf(currentFocused);
let newIndex = currentIndex + direction;
// Wrap around
if (newIndex < 0) {
newIndex = options.length - 1;
} else if (newIndex >= options.length) {
newIndex = 0;
}
// Remove focus from current option
currentFocused.classList.remove('focused');
// Add focus to new option
const newFocused = options[newIndex];
newFocused.classList.add('focused');
newFocused.focus();
}
selectFocusedWidget() {
const widgetMenuOverlay = document.getElementById('widgetMenuOverlay');
if (!widgetMenuOverlay) return;
const focusedOption = widgetMenuOverlay.querySelector('.widget-option.focused');
if (focusedOption) {
const widgetType = focusedOption.dataset.widgetType;
this.dashboard.selectWidgetType(widgetType);
}
}
openWidgetMenuAtCursor() {
const widgetMenuOverlay = document.getElementById('widgetMenuOverlay');
if (!widgetMenuOverlay) return;
if (widgetMenuOverlay.classList.contains('show')) {
this.dashboard.closeWidgetMenu();
} else {
const x = this.dashboard._lastMouseX;
const y = this.dashboard._lastMouseY;
this.dashboard.openWidgetMenu(x, y);
}
}
}
export { KeyboardManager };