Add clear all events functionality

Add 'iCalendar: Clear All Events' command to completely remove
all indexed calendar events and cache. Useful for maintenance
and troubleshooting.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Alexandre Nicolaie <xunleii@users.noreply.github.com>
This commit is contained in:
Alexandre Nicolaie
2025-10-18 10:34:48 +02:00
parent 34bbe69569
commit 904c1b9d94
3 changed files with 64 additions and 0 deletions

View File

@@ -48,6 +48,8 @@ After configuration, run the `{[iCalendar: Sync]}` command to synchronize calend
To bypass the cache and force an immediate sync, use the `{[iCalendar: Force Sync]}` command.
To completely clear all indexed events and cache (useful for troubleshooting), use the `{[iCalendar: Clear All Events]}` command.
Events are indexed with the tag `ical-event` and can be queried using Lua Integrated Query (LIQ).
### Examples

View File

@@ -14,6 +14,11 @@ functions:
command:
name: "iCalendar: Force Sync"
priority: -1
clearCache:
path: ./icalendar.ts:clearCache
command:
name: "iCalendar: Clear All Events"
priority: -1
showVersion:
path: ./icalendar.ts:showVersion
command:

View File

@@ -218,6 +218,63 @@ export async function forceSync() {
await syncCalendars();
}
/**
* Clears the calendar cache by removing the indexed events page
* Implementation based on SilverBullet's clearFileIndex:
* https://github.com/silverbulletmd/silverbullet/blob/main/plugs/index/api.ts#L49-L69
*/
export async function clearCache() {
// Ask for confirmation before clearing the cache
if (!await editor.confirm(
"Are you sure you want to clear all calendar events and cache? This will remove all indexed calendar data."
)) {
return;
}
try {
const fileName = "$icalendar";
console.log("[iCalendar] Clearing index for", fileName);
// Implementation based on SilverBullet's clearFileIndex function
// https://github.com/silverbulletmd/silverbullet/blob/main/plugs/index/api.ts#L49-L69
const indexKey = "idx";
const pageKey = "ridx";
// Query all keys for this file
const allKeys: any[] = [];
// Get all page keys for this file: [pageKey, $icalendar, ...key]
const pageKeys = await datastore.query({
prefix: [pageKey, fileName],
});
for (const { key } of pageKeys) {
allKeys.push(key);
// Also add corresponding index keys: [indexKey, tag, ref, $icalendar]
// where tag is "ical-event" and ref is the event reference
allKeys.push([indexKey, ...key.slice(2), fileName]);
}
// Batch delete all found keys
if (allKeys.length > 0) {
await datastore.batchDel(allKeys);
console.log("[iCalendar] Deleted", allKeys.length, "events");
}
// Also clear the sync timestamp cache
await clientStore.del(CACHE_KEY);
console.log("[iCalendar] Calendar index and cache cleared");
await editor.flashNotification("Calendar index and cache cleared", "info");
} catch (err) {
console.error("[iCalendar] Failed to clear cache:", err);
await editor.flashNotification(
`Failed to clear cache: ${err instanceof Error ? err.message : String(err)}`,
"error"
);
}
}
/**
* Shows the plugin version
*/