From 904c1b9d94f78066621c908565341aeab598060c Mon Sep 17 00:00:00 2001 From: Alexandre Nicolaie Date: Sat, 18 Oct 2025 10:34:48 +0200 Subject: [PATCH] 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 Signed-off-by: Alexandre Nicolaie --- README.md | 2 ++ icalendar.plug.yaml | 5 ++++ icalendar.ts | 57 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/README.md b/README.md index 98dce40..9a2c4de 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/icalendar.plug.yaml b/icalendar.plug.yaml index 78b2a3b..e16a9d8 100644 --- a/icalendar.plug.yaml +++ b/icalendar.plug.yaml @@ -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: diff --git a/icalendar.ts b/icalendar.ts index f752dc6..8d1b6d0 100644 --- a/icalendar.ts +++ b/icalendar.ts @@ -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 */