-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlashCommandHandler.ts
More file actions
168 lines (144 loc) · 4.71 KB
/
Copy pathSlashCommandHandler.ts
File metadata and controls
168 lines (144 loc) · 4.71 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
import { Notice } from "obsidian";
export interface SlashCommandCallbacks {
onClear: () => void;
onExpandAll: () => void;
onCollapseAll: () => void;
}
export class SlashCommandHandler {
private slashCommandBuffer = '';
private slashCommandTimeout: number | null = null;
private callbacks: SlashCommandCallbacks;
constructor(callbacks: SlashCommandCallbacks) {
this.callbacks = callbacks;
}
/**
* Start a slash command sequence
*/
startSlashCommand(searchInput: HTMLInputElement): void {
// Clear any existing buffer
this.slashCommandBuffer = '';
// Focus search and start slash command
searchInput.value = '/';
searchInput.setSelectionRange(1, 1); // Place cursor after /
// Set up timeout to clear buffer if no more typing
if (this.slashCommandTimeout) {
clearTimeout(this.slashCommandTimeout);
}
this.slashCommandTimeout = setTimeout(() => {
this.slashCommandBuffer = '';
}, 3000); // Clear after 3 seconds of inactivity
}
/**
* Check if currently typing a slash command
*/
isTypingSlashCommand(searchInput: HTMLInputElement): boolean {
// Check if we're in the middle of typing a slash command
if (this.slashCommandBuffer.startsWith('/')) {
return true;
}
// Check if search input has a slash command
if (searchInput && searchInput.value.startsWith('/')) {
return true;
}
return false;
}
/**
* Handle typing during slash command mode
*/
handleTyping(e: KeyboardEvent, searchInput: HTMLInputElement): boolean {
// Route all typing to search input when building slash command
if (e.key === 'Enter') {
e.preventDefault();
e.stopPropagation();
this.handleSlashCommand(searchInput.value.trim());
return true;
}
if (e.key === 'Escape') {
e.preventDefault();
e.stopPropagation();
this.clearSlashCommand(searchInput);
return true;
}
if (e.key === 'Backspace') {
e.preventDefault();
e.stopPropagation();
if (searchInput.value.length > 1) {
searchInput.value = searchInput.value.slice(0, -1);
this.updateSlashCommandHint(searchInput);
} else {
this.clearSlashCommand(searchInput);
}
return true;
}
// For other printable characters, add to search input
if (e.key.length === 1 && !e.ctrlKey && !e.metaKey) {
e.preventDefault();
e.stopPropagation();
searchInput.value += e.key;
this.updateSlashCommandHint(searchInput);
return true;
}
return false;
}
/**
* Clear slash command state
*/
clearSlashCommand(searchInput: HTMLInputElement): void {
searchInput.value = '';
searchInput.placeholder = 'Search tags...';
this.slashCommandBuffer = '';
if (this.slashCommandTimeout) {
clearTimeout(this.slashCommandTimeout);
this.slashCommandTimeout = null;
}
}
/**
* Execute a slash command
*/
private handleSlashCommand(command: string): boolean {
if (!command.startsWith('/')) return false;
const cmd = command.toLowerCase();
switch (cmd) {
case '/clear':
this.callbacks.onClear();
new Notice("Cleared all facets and search");
return true;
case '/expand':
this.callbacks.onExpandAll();
new Notice("Expanded all tags");
return true;
case '/collapse':
this.callbacks.onCollapseAll();
new Notice("Collapsed all tags");
return true;
default:
// Check for partial matches to provide helpful suggestions
const availableCommands = ['/clear', '/expand', '/collapse'];
const suggestions = availableCommands.filter(c => c.startsWith(cmd));
if (suggestions.length > 0) {
new Notice(`Did you mean: ${suggestions.join(', ')}?`);
} else {
new Notice(`Unknown command: ${command}. Available: /clear, /expand, /collapse`);
}
return true;
}
}
/**
* Update search input placeholder with command hints
*/
private updateSlashCommandHint(searchInput: HTMLInputElement): void {
const query = searchInput.value;
const availableCommands = ['/clear', '/expand', '/collapse'];
const matches = availableCommands.filter(cmd => cmd.startsWith(query.toLowerCase()));
if (matches.length === 1) {
// Show completion hint
searchInput.placeholder = `${matches[0]} - Press Enter to execute`;
} else if (matches.length > 1) {
// Show available options
searchInput.placeholder = `Available: ${matches.join(', ')}`;
} else {
// Show all commands if no matches
searchInput.placeholder = `Commands: ${availableCommands.join(', ')}`;
}
}
}