feat: persistent recent files history + fix limit enforcement + fix settings data loss
- Recent files are now persisted to disk and survive restarts - Max items limit is applied on load (in case setting was reduced) - Omitted paths regex filtering is now wired up in addToRecentFiles - Fixed saveAndRefresh() overwriting entire plugin data on settings change - Debounced disk writes (300ms) to avoid excessive saves on rapid opens
This commit is contained in:
+38
-1
@@ -16,12 +16,14 @@ import { BookmarkItem, WaypointData } from 'src/models/bookmark';
|
||||
|
||||
const DEFAULT_DATA: WaypointData = {
|
||||
bookmarks: [],
|
||||
recentFiles: [],
|
||||
};
|
||||
|
||||
export default class WaypointPlugin extends Plugin {
|
||||
public settings: WaypointSettings;
|
||||
public waypointData: WaypointData;
|
||||
public recentFiles: { path: string; basename: string }[] = [];
|
||||
private recentFilesSaveTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
async onload(): Promise<void> {
|
||||
console.debug('Waypoint: loading plugin v' + this.manifest.version);
|
||||
@@ -206,14 +208,36 @@ export default class WaypointPlugin extends Plugin {
|
||||
const saved = await this.loadData() as Record<string, unknown> | null;
|
||||
const d = (saved?.waypointData || {}) as Partial<WaypointData>;
|
||||
this.waypointData = Object.assign({}, DEFAULT_DATA, d);
|
||||
|
||||
// Load persisted recent files
|
||||
this.recentFiles = this.waypointData.recentFiles || [];
|
||||
|
||||
// Apply current limit (in case maxItems was reduced since last save)
|
||||
if (this.recentFiles.length > this.settings.recentFiles.maxItems) {
|
||||
this.recentFiles = this.recentFiles.slice(0, this.settings.recentFiles.maxItems);
|
||||
this.waypointData.recentFiles = this.recentFiles;
|
||||
}
|
||||
}
|
||||
|
||||
async saveWaypointData(): Promise<void> {
|
||||
// Sync recentFiles into waypointData before saving
|
||||
this.waypointData.recentFiles = this.recentFiles;
|
||||
const all = (await this.loadData()) as Record<string, unknown> || {};
|
||||
all.waypointData = this.waypointData;
|
||||
await this.saveData(all);
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist recent files to disk (debounced to avoid excessive writes on rapid opens).
|
||||
*/
|
||||
persistRecentFiles(): void {
|
||||
this.waypointData.recentFiles = this.recentFiles;
|
||||
if (this.recentFilesSaveTimer) clearTimeout(this.recentFilesSaveTimer);
|
||||
this.recentFilesSaveTimer = setTimeout(() => {
|
||||
this.saveWaypointData();
|
||||
}, 300);
|
||||
}
|
||||
|
||||
// ── Recent Files ──
|
||||
|
||||
private onFileOpen(file: TFile): void {
|
||||
@@ -225,14 +249,26 @@ export default class WaypointPlugin extends Plugin {
|
||||
}
|
||||
|
||||
addToRecentFiles(file: TFile): void {
|
||||
// Apply omitted paths filter
|
||||
if (this.settings.recentFiles.omittedPaths.length > 0) {
|
||||
for (const pattern of this.settings.recentFiles.omittedPaths) {
|
||||
try {
|
||||
if (new RegExp(pattern).test(file.path)) return;
|
||||
} catch {
|
||||
// Invalid regex, skip
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.recentFiles = this.recentFiles.filter(f => f.path !== file.path);
|
||||
this.recentFiles.unshift({ path: file.path, basename: file.basename });
|
||||
|
||||
// Prune
|
||||
// Apply max items limit
|
||||
if (this.recentFiles.length > this.settings.recentFiles.maxItems) {
|
||||
this.recentFiles = this.recentFiles.slice(0, this.settings.recentFiles.maxItems);
|
||||
}
|
||||
|
||||
this.persistRecentFiles();
|
||||
this.broadcastRedraw();
|
||||
}
|
||||
|
||||
@@ -241,6 +277,7 @@ export default class WaypointPlugin extends Plugin {
|
||||
if (entry) {
|
||||
entry.path = file.path;
|
||||
entry.basename = (file as TFile).basename || file.name.replace(/\.[^/.]+$/, '');
|
||||
this.persistRecentFiles();
|
||||
this.broadcastRedraw();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user