vg-coder-cli 2.0.34 → 2.0.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,333 @@
1
+ /**
2
+ * VG Coder Keyboard Shortcuts Module (Shadow DOM Compatible)
3
+ *
4
+ * Global keyboard shortcuts that work when VG Coder is injected via extension.
5
+ * Uses document-level listeners and composedPath() to check Shadow DOM isolation.
6
+ */
7
+
8
+ import { getById, getRoot } from '../utils.js';
9
+
10
+ /**
11
+ * Centralized keyboard shortcuts configuration
12
+ * Single source of truth for all shortcuts
13
+ */
14
+ const SHORTCUTS_CONFIG = [
15
+ {
16
+ key: '3',
17
+ action: 'toggle-dashboard',
18
+ description: 'Toggle VG Coder Dashboard (Open/Close)',
19
+ },
20
+ {
21
+ key: '4',
22
+ action: 'open-git-panel',
23
+ description: 'Open VG Coder & Activate Git Panel',
24
+ },
25
+ {
26
+ key: '/',
27
+ action: 'help-shortcuts',
28
+ description: 'Show/Hide Keyboard Shortcuts Help',
29
+ },
30
+ ];
31
+
32
+ /**
33
+ * Generate shortcuts registry from config
34
+ * Automatically creates CMD+key and CTRL+key variants
35
+ */
36
+ const SHORTCUTS = {};
37
+ SHORTCUTS_CONFIG.forEach(({ key, action }) => {
38
+ SHORTCUTS[`CMD+${key.toUpperCase()}`] = action;
39
+ SHORTCUTS[`CTRL+${key.toUpperCase()}`] = action;
40
+ });
41
+
42
+ // Platform detection
43
+ const isMac = typeof navigator !== 'undefined' && /Mac|iPhone|iPod|iPad/.test(navigator.platform);
44
+
45
+ /**
46
+ * Convert keyboard event to shortcut key string
47
+ * @param {KeyboardEvent} event - Keyboard event
48
+ * @returns {string|null} Shortcut key string (e.g., "CMD+4")
49
+ */
50
+ function getShortcutKey(event) {
51
+ const parts = [];
52
+
53
+ // Add modifier keys
54
+ if (event.metaKey) parts.push('CMD');
55
+ if (event.ctrlKey) parts.push('CTRL');
56
+ if (event.altKey) parts.push('ALT');
57
+ if (event.shiftKey) parts.push('SHIFT');
58
+
59
+ // Add main key (use event.key for number keys)
60
+ const key = event.key;
61
+
62
+ // Skip if only modifiers were pressed
63
+ if (['Meta', 'Control', 'Alt', 'Shift'].includes(key)) {
64
+ return null;
65
+ }
66
+
67
+ parts.push(key.toUpperCase());
68
+
69
+ return parts.join('+');
70
+ }
71
+
72
+ /**
73
+ * Check if event originated from VG Coder Shadow DOM
74
+ * @param {KeyboardEvent} event - Keyboard event
75
+ * @returns {boolean} True if event is from VG Coder
76
+ */
77
+ function isFromVGCoder(event) {
78
+ try {
79
+ // SIMPLIFIED APPROACH: Since we're in shadow DOM context,
80
+ // just check if VG Coder elements exist (we're always "in" VG Coder)
81
+ // This works because the bundle is only loaded when VG Coder is active
82
+ const appRoot = getById('vg-app-root');
83
+ const bubble = getById('vg-bubble');
84
+
85
+ // If VG Coder is loaded, we should handle ALL keyboard events
86
+ // (We're in isolated shadow DOM, so we won't interfere with page events)
87
+ const vgCoderExists = !!(appRoot || bubble);
88
+
89
+ if (!vgCoderExists) {
90
+ console.warn('[Keyboard] VG Coder elements not found');
91
+ return false;
92
+ }
93
+
94
+ return true;
95
+ } catch (err) {
96
+ console.warn('[Keyboard] Error checking VG Coder:', err);
97
+ return false;
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Toggle VG Coder dashboard visibility
103
+ */
104
+ function toggleDashboard() {
105
+ const appRoot = getById('vg-app-root');
106
+ if (!appRoot) {
107
+ console.warn('[Keyboard] Cannot toggle dashboard: vg-app-root not found');
108
+ return;
109
+ }
110
+
111
+ const isVisible = appRoot.classList.contains('visible');
112
+
113
+ if (isVisible) {
114
+ appRoot.classList.remove('visible');
115
+ console.log('[Keyboard] Dashboard hidden');
116
+ } else {
117
+ appRoot.classList.add('visible');
118
+ console.log('[Keyboard] Dashboard shown');
119
+ }
120
+ }
121
+
122
+ /**
123
+ * Open VG Coder and activate Git panel
124
+ */
125
+ function openGitPanel() {
126
+ const appRoot = getById('vg-app-root');
127
+ if (!appRoot) {
128
+ console.warn('[Keyboard] Cannot open Git panel: vg-app-root not found');
129
+ return;
130
+ }
131
+
132
+ // First, make sure dashboard is visible
133
+ if (!appRoot.classList.contains('visible')) {
134
+ appRoot.classList.add('visible');
135
+ console.log('[Keyboard] Dashboard opened');
136
+ }
137
+
138
+ // Then activate Git panel using getRoot() to query in Shadow DOM
139
+ const root = getRoot();
140
+ const gitButton = root.querySelector('.tool-window-icon[data-panel="git"]');
141
+ if (gitButton) {
142
+ gitButton.click();
143
+ console.log('[Keyboard] Git panel activated');
144
+ } else {
145
+ console.warn('[Keyboard] Git panel button not found');
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Create new terminal
151
+ */
152
+ function createNewTerminal() {
153
+ if (typeof window.createNewTerminal === 'function') {
154
+ window.createNewTerminal();
155
+ console.log('[Keyboard] New terminal created');
156
+ } else {
157
+ console.warn('[Keyboard] Terminal feature not available');
158
+ }
159
+ }
160
+
161
+ /**
162
+ * Handle keyboard event
163
+ * @param {KeyboardEvent} event - Keyboard event
164
+ */
165
+ function handleKeyDown(event) {
166
+ // Debug: Log all keyboard events
167
+ const key = event.key;
168
+ const meta = event.metaKey;
169
+ const ctrl = event.ctrlKey;
170
+
171
+ // Only log if Cmd or Ctrl is pressed (to reduce noise)
172
+ if (meta || ctrl) {
173
+ console.log('[Keyboard] Key event:', { key, meta, ctrl, alt: event.altKey, shift: event.shiftKey });
174
+ }
175
+
176
+ // Check if event is from VG Coder shadow DOM
177
+ const fromVGCoder = isFromVGCoder(event);
178
+ if (!fromVGCoder) {
179
+ if (meta || ctrl) {
180
+ console.log('[Keyboard] Event ignored - not from VG Coder');
181
+ }
182
+ return;
183
+ }
184
+
185
+ const shortcutKey = getShortcutKey(event);
186
+ console.log('[Keyboard] Shortcut key detected:', shortcutKey);
187
+
188
+ if (!shortcutKey) return;
189
+
190
+ // Check if shortcut is registered
191
+ const action = SHORTCUTS[shortcutKey];
192
+
193
+ if (!action) {
194
+ console.log('[Keyboard] No action registered for:', shortcutKey);
195
+ return;
196
+ }
197
+
198
+ // Prevent default browser behavior
199
+ event.preventDefault();
200
+ event.stopPropagation();
201
+
202
+ console.log(`[Keyboard] Shortcut triggered: ${shortcutKey} -> ${action}`);
203
+
204
+ // Execute action
205
+ switch (action) {
206
+ case 'toggle-dashboard':
207
+ toggleDashboard();
208
+ break;
209
+ case 'open-git-panel':
210
+ openGitPanel();
211
+ break;
212
+ case 'help-shortcuts':
213
+ toggleShortcutsHelp();
214
+ break;
215
+ default:
216
+ console.warn(`[Keyboard] Unknown action: ${action}`);
217
+ }
218
+ }
219
+
220
+ /**
221
+ * Toggle shortcuts help panel
222
+ */
223
+ function toggleShortcutsHelp() {
224
+ const helpPanel = getById('shortcuts-help');
225
+ if (!helpPanel) {
226
+ console.warn('[Keyboard] Help panel not found');
227
+ return;
228
+ }
229
+
230
+ const isVisible = helpPanel.classList.contains('visible');
231
+
232
+ if (isVisible) {
233
+ helpPanel.classList.remove('visible');
234
+ console.log('[Keyboard] Help panel hidden');
235
+ } else {
236
+ // Render shortcuts before showing
237
+ renderHelpPanel();
238
+ helpPanel.classList.add('visible');
239
+ console.log('[Keyboard] Help panel shown');
240
+ }
241
+ }
242
+
243
+ /**
244
+ * Render shortcuts in help panel
245
+ */
246
+ function renderHelpPanel() {
247
+ const container = getById('shortcuts-list');
248
+ if (!container) return;
249
+
250
+ // Auto-generate shortcuts list from config
251
+ const prefix = isMac ? 'Cmd' : 'Ctrl';
252
+ const shortcuts = SHORTCUTS_CONFIG.map(({ key, description }) => ({
253
+ key: `${prefix}+${key}`,
254
+ desc: description,
255
+ }));
256
+
257
+ container.innerHTML = shortcuts.map(({ key, desc }) => {
258
+ const keys = key.split('+').map(k => `<span class="vg-shortcut-key">${k}</span>`);
259
+ const keysHtml = keys.join('<span class="vg-shortcut-plus">+</span>');
260
+
261
+ return `
262
+ <div class="vg-shortcut-item">
263
+ <div class="vg-shortcut-desc">${desc}</div>
264
+ <div class="vg-shortcut-keys">${keysHtml}</div>
265
+ </div>
266
+ `;
267
+ }).join('');
268
+ }
269
+
270
+ /**
271
+ * Create new terminal (continued from above)
272
+ */
273
+ function continueCreateNewTerminal() {
274
+ // This function placeholder ensures proper code flow
275
+ // The actual implementation is below
276
+ }
277
+
278
+ // Fix the switch statement continuation
279
+ function handleKeyDownContinued(action) {
280
+ switch (action) {
281
+ case 'terminal-new':
282
+ createNewTerminal();
283
+ break;
284
+ default:
285
+ console.warn(`[Keyboard] Unknown action: ${action}`);
286
+ }
287
+ }
288
+
289
+ /**
290
+ * Initialize keyboard shortcuts
291
+ */
292
+ export function initKeyboardShortcuts() {
293
+ console.log('[Keyboard] Initializing keyboard shortcuts...');
294
+ console.log(`[Keyboard] Platform: ${isMac ? 'macOS' : 'Windows/Linux'}`);
295
+
296
+ // Register global keydown listener at document level
297
+ // Using capture phase (true) to catch events before they reach target
298
+ document.addEventListener('keydown', handleKeyDown, true);
299
+
300
+ // Log registered shortcuts
301
+ const shortcuts = Object.keys(SHORTCUTS)
302
+ .filter(key => isMac ? key.startsWith('CMD') : key.startsWith('CTRL'))
303
+ .map(key => `${key} → ${SHORTCUTS[key]}`);
304
+
305
+ console.log('[Keyboard] Registered shortcuts:', shortcuts);
306
+ console.log('[Keyboard] Keyboard shortcuts initialized ✓');
307
+
308
+ // Make available for debugging and HTML onclick handlers
309
+ if (typeof window !== 'undefined') {
310
+ window.__VG_KEYBOARD_SHORTCUTS__ = {
311
+ shortcuts: SHORTCUTS,
312
+ platform: isMac ? 'mac' : 'win',
313
+ toggle: toggleDashboard,
314
+ terminal: createNewTerminal,
315
+ help: toggleShortcutsHelp,
316
+ };
317
+
318
+ // Expose for HTML onclick handler
319
+ window.toggleShortcutsHelp = toggleShortcutsHelp;
320
+ }
321
+ }
322
+
323
+ /**
324
+ * Get help text for shortcuts (for future help UI)
325
+ * @returns {Array} Array of shortcut descriptions
326
+ */
327
+ export function getShortcutsHelp() {
328
+ const prefix = isMac ? 'Cmd' : 'Ctrl';
329
+ return [
330
+ { key: `${prefix}+4`, description: 'Toggle VG Coder Dashboard (Open/Close)' },
331
+ { key: `${prefix}+5`, description: 'Create New Terminal' },
332
+ ];
333
+ }