Skip to content

Commit 3f48c67

Browse files
committed
feat: implement deleting a single history entry and purging all history for a specific shortcut
1 parent c4ffa28 commit 3f48c67

9 files changed

Lines changed: 123 additions & 7 deletions

File tree

CmdPalWebSearchShortcut/WebSearchShortcut/History/HistoryService.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,40 @@ public static void Add(string shortcutName, string query)
5959
}
6060
}
6161

62+
public static void Remove(string shortcutName, string query)
63+
{
64+
lock (_lock)
65+
{
66+
if (!_cache.Data.TryGetValue(shortcutName, out var entries) || entries is null)
67+
{
68+
entries = [];
69+
_cache.Data[shortcutName] = entries;
70+
}
71+
72+
entries.RemoveAll(entry => string.Equals(entry.Query, query, StringComparison.Ordinal));
73+
74+
RebuildShortcutQueriesMap();
75+
76+
Save();
77+
78+
ExtensionHost.LogMessage($"[WebSearchShortcut] History: Delete Query shortcut=\"{shortcutName}\" query=\"{query}\"");
79+
}
80+
}
81+
82+
public static void RemoveAll(string shortcutName)
83+
{
84+
lock (_lock)
85+
{
86+
_cache.Data[shortcutName] = [];
87+
88+
RebuildShortcutQueriesMap();
89+
90+
Save();
91+
92+
ExtensionHost.LogMessage($"[WebSearchShortcut] History: Claer shortcut=\"{shortcutName}\"");
93+
}
94+
}
95+
6296
public static void Reload()
6397
{
6498
lock (_lock)

CmdPalWebSearchShortcut/WebSearchShortcut/Pages/SearchWebPage.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ private ListItem[] BuildPrimaryItems(string searchText)
184184
new ListItem(
185185
new SearchWebCommand(_shortcut, searchText)
186186
{
187-
Name = StringFormatter.Format(Resources.SearchQueryItem_NameTemplate, new() { ["shortcut"] = _shortcut.Name, ["query"] = searchText }),
187+
Icon = Icons.Search,
188+
Name = StringFormatter.Format(Resources.SearchQueryItem_NameTemplate, new() { ["shortcut"] = _shortcut.Name, ["query"] = searchText })
188189
}
189190
)
190191
{
@@ -206,15 +207,32 @@ private ListItem[] BuildHistoryItems()
206207
.. historyQueries.Select(historyQuery => new ListItem(
207208
new SearchWebCommand(_shortcut, historyQuery)
208209
{
209-
Name = StringFormatter.Format(Resources.SearchQueryItem_NameTemplate, new() { ["shortcut"] = _shortcut.Name, ["query"] = historyQuery }),
210+
Icon = Icons.Search,
211+
Name = StringFormatter.Format(Resources.SearchQueryItem_NameTemplate, new() { ["shortcut"] = _shortcut.Name, ["query"] = historyQuery })
210212
}
211213
)
212214
{
213215
Title = StringFormatter.Format(Resources.SearchQueryItem_TitleTemplate, new() { ["shortcut"] = _shortcut.Name, ["query"] = historyQuery }),
214216
Subtitle = StringFormatter.Format(Resources.SearchQueryItem_SubtitleTemplate, new() { ["shortcut"] = _shortcut.Name, ["query"] = historyQuery }),
215217
Icon = Icons.History,
216218
TextToSuggest = historyQuery,
217-
MoreCommands = [_openHomepageContextItem]
219+
MoreCommands = [
220+
_openHomepageContextItem,
221+
new CommandContextItem(
222+
title: StringFormatter.Format(Resources.DeleteHistory_TitleTemplate, new() { ["shortcut"] = _shortcut.Name, ["query"] = historyQuery }),
223+
name: $"[UNREACHABLE] DeleteHistory.Name - shortcut='{_shortcut.Name}', query='{historyQuery}'",
224+
action: () =>
225+
{
226+
HistoryService.Remove(_shortcut.Name, historyQuery);
227+
Rebuild();
228+
},
229+
result: CommandResult.KeepOpen()
230+
)
231+
{
232+
Icon = Icons.DeleteHistory,
233+
IsCritical = true
234+
}
235+
]
218236
})
219237
];
220238
}
@@ -231,6 +249,7 @@ private async Task<ListItem[]> FetchSuggestionItemsAsync(string searchText, Canc
231249
.. suggestions.Select(suggestion => new ListItem(
232250
new SearchWebCommand(_shortcut, suggestion.Title)
233251
{
252+
Icon = Icons.Search,
234253
Name = StringFormatter.Format(Resources.SearchQueryItem_NameTemplate, new() { ["shortcut"] = _shortcut.Name, ["query"] = suggestion.Title })
235254
}
236255
)

CmdPalWebSearchShortcut/WebSearchShortcut/Properties/Icons.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,9 @@ internal static class Icons
5555
/// History icon
5656
/// </summary>
5757
public static IconInfo History { get; } = new("\uE81C");
58+
59+
/// <summary>
60+
/// Delete History icon
61+
/// </summary>
62+
public static readonly IconInfo DeleteHistory = new("\uE894");
5863
}

CmdPalWebSearchShortcut/WebSearchShortcut/Properties/Resources.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CmdPalWebSearchShortcut/WebSearchShortcut/Properties/Resources.fr.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@
192192
<data name="ShortcutItem_NameTemplate" xml:space="preserve">
193193
<value>{shortcut}</value>
194194
</data>
195+
<data name="ClearHistory_TitleTemplate" xml:space="preserve">
196+
<value />
197+
</data>
195198
<data name="EditShortcutItem_TitleTemplate" xml:space="preserve">
196199
<value>Modifier le raccourci de recherche</value>
197200
</data>
@@ -219,4 +222,7 @@
219222
<data name="SearchQueryItem_NameTemplate" xml:space="preserve">
220223
<value>Rechercher "{query}"</value>
221224
</data>
225+
<data name="DeleteHistory_TitleTemplate" xml:space="preserve">
226+
<value />
227+
</data>
222228
</root>

CmdPalWebSearchShortcut/WebSearchShortcut/Properties/Resources.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@
192192
<data name="ShortcutItem_NameTemplate" xml:space="preserve">
193193
<value>{shortcut}</value>
194194
</data>
195+
<data name="ClearHistory_TitleTemplate" xml:space="preserve">
196+
<value>Clear {shortcut}'s History</value>
197+
</data>
195198
<data name="EditShortcutItem_TitleTemplate" xml:space="preserve">
196199
<value>Edit Search Shortcut</value>
197200
</data>
@@ -219,4 +222,7 @@
219222
<data name="SearchQueryItem_NameTemplate" xml:space="preserve">
220223
<value>Search for "{query}"</value>
221224
</data>
225+
<data name="DeleteHistory_TitleTemplate" xml:space="preserve">
226+
<value>Delete</value>
227+
</data>
222228
</root>

CmdPalWebSearchShortcut/WebSearchShortcut/Properties/Resources.zh-CN.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@
192192
<data name="ShortcutItem_NameTemplate" xml:space="preserve">
193193
<value>{shortcut}</value>
194194
</data>
195+
<data name="ClearHistory_TitleTemplate" xml:space="preserve">
196+
<value />
197+
</data>
195198
<data name="EditShortcutItem_TitleTemplate" xml:space="preserve">
196199
<value>编辑搜索捷径</value>
197200
</data>
@@ -219,4 +222,7 @@
219222
<data name="SearchQueryItem_NameTemplate" xml:space="preserve">
220223
<value>搜索“{query}”</value>
221224
</data>
225+
<data name="DeleteHistory_TitleTemplate" xml:space="preserve">
226+
<value />
227+
</data>
222228
</root>

CmdPalWebSearchShortcut/WebSearchShortcut/Properties/Resources.zh-TW.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@
192192
<data name="ShortcutItem_NameTemplate" xml:space="preserve">
193193
<value>{shortcut}</value>
194194
</data>
195+
<data name="ClearHistory_TitleTemplate" xml:space="preserve">
196+
<value>清除 {shortcut} 的歷史紀錄</value>
197+
</data>
195198
<data name="EditShortcutItem_TitleTemplate" xml:space="preserve">
196199
<value>編輯搜尋捷徑</value>
197200
</data>
@@ -219,4 +222,7 @@
219222
<data name="SearchQueryItem_NameTemplate" xml:space="preserve">
220223
<value>搜尋「{query}」</value>
221224
</data>
225+
<data name="DeleteHistory_TitleTemplate" xml:space="preserve">
226+
<value>將「{query}」從歷史紀錄刪除</value>
227+
</data>
222228
</root>

CmdPalWebSearchShortcut/WebSearchShortcut/WebSearchShortcutCommandsProvider.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.CommandPalette.Extensions;
1111
using Microsoft.CommandPalette.Extensions.Toolkit;
1212
using WebSearchShortcut.Helpers;
13+
using WebSearchShortcut.History;
1314
using WebSearchShortcut.Properties;
1415
using WebSearchShortcut.Services;
1516

@@ -131,6 +132,11 @@ private void LoadShortcutFromFile()
131132

132133
private CommandItem CreateCommandItem(WebSearchShortcutDataEntry shortcut)
133134
{
135+
var searchWebPage = new SearchWebPage(shortcut)
136+
{
137+
Name = StringFormatter.Format(Resources.ShortcutItem_NameTemplate, new() { ["shortcut"] = shortcut.Name })
138+
};
139+
134140
var editShortcutPage = new AddShortcutPage(shortcut)
135141
{
136142
Name = StringFormatter.Format(Resources.EditShortcutItem_NameTemplate, new() { ["shortcut"] = shortcut.Name }),
@@ -163,12 +169,22 @@ private CommandItem CreateCommandItem(WebSearchShortcutDataEntry shortcut)
163169
IsCritical = true
164170
};
165171

166-
var commandItem = new CommandItem(
167-
new SearchWebPage(shortcut)
172+
var clearHistoryCommand = new CommandContextItem(
173+
title: StringFormatter.Format(Resources.ClearHistory_TitleTemplate, new() { ["shortcut"] = shortcut.Name }),
174+
name: $"[UNREACHABLE] ClearHistory.Name - shortcut='{shortcut.Name}'",
175+
action: () =>
168176
{
169-
Name = StringFormatter.Format(Resources.ShortcutItem_NameTemplate, new() { ["shortcut"] = shortcut.Name })
170-
}
177+
HistoryService.RemoveAll(shortcut.Name);
178+
searchWebPage.Rebuild();
179+
},
180+
result: CommandResult.KeepOpen()
171181
)
182+
{
183+
Icon = Icons.DeleteHistory,
184+
IsCritical = true
185+
};
186+
187+
var commandItem = new CommandItem(searchWebPage)
172188
{
173189
Title = StringFormatter.Format(Resources.ShortcutItem_TitleTemplate, new() { ["shortcut"] = shortcut.Name }),
174190
Subtitle = StringFormatter.Format(Resources.ShortcutItem_SubtitleTemplate, new() { ["shortcut"] = shortcut.Name }),

0 commit comments

Comments
 (0)