Skip to content

Commit

Permalink
Add option to change trigger to file edit (#114)
Browse files Browse the repository at this point in the history
In the settings, a new option allows selecting "Changed" instead of "Opened" to control when the recent files list is updated.
  • Loading branch information
yoavg authored Jan 3, 2025
1 parent 677c1df commit eea1353
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface RecentFilesData {
omittedPaths: string[];
omittedTags: string[];
omitBookmarks: boolean;
updateOn: 'file-edit' | 'file-open';
maxLength?: number;
}

Expand All @@ -43,6 +44,7 @@ const DEFAULT_DATA: RecentFilesData = {
recentFiles: [],
omittedPaths: [],
omittedTags: [],
updateOn: 'file-open',
omitBookmarks: false,
};

Expand Down Expand Up @@ -313,7 +315,7 @@ export default class RecentFilesPlugin extends Plugin {

this.registerEvent(this.app.vault.on('rename', this.handleRename));
this.registerEvent(this.app.vault.on('delete', this.handleDelete));
this.registerEvent(this.app.workspace.on('file-open', this.update));
this.registerEvent(this.app.workspace.on('file-open', this.onFileOpen));

this.addSettingTab(new RecentFilesSettingTab(this.app, this));
}
Expand Down Expand Up @@ -458,6 +460,29 @@ export default class RecentFilesPlugin extends Plugin {
}
};

private readonly onFileOpen = async (openedFile: TFile): Promise<void> => {
if (!openedFile) {
return;
}
if (this.data.updateOn === 'file-edit') {
this.app.workspace.off('quick-preview', this.waitingForEdit);
this.registerEvent(this.app.workspace.on('quick-preview', this.waitingForEdit))
} else {
this.update(openedFile);
}
// Update the view if there is one.
// We redraw the leaf to handle active file highlighting.
const leaf = this.app.workspace.getLeavesOfType(RecentFilesListViewType).first();
if (leaf && leaf.view instanceof RecentFilesListView) {
leaf.view.redraw();
}
}

private readonly waitingForEdit = async (editedFile: TFile, data: string): Promise<void> => {
this.update(editedFile);
this.app.workspace.off('quick-preview', this.waitingForEdit);
}

private readonly updateData = async (file: TFile): Promise<void> => {
const lengthBefore = this.data.recentFiles.length;
this.data.recentFiles = this.data.recentFiles.filter(
Expand Down Expand Up @@ -585,6 +610,20 @@ class RecentFilesSettingTab extends PluginSettingTab {
});
});

new Setting(containerEl)
.setName('Update list when file is:')
.addDropdown((dropdown) => {
dropdown
.addOption('file-open', 'Opened')
.addOption('file-edit', 'Changed')
.setValue(this.plugin.data.updateOn)
.onChange((value: 'file-edit' | 'file-open') => {
this.plugin.data.updateOn = value;
this.plugin.pruneOmittedFiles();
this.plugin.view.redraw();
});
});

new Setting(containerEl)
.setName('List length')
.setDesc('Maximum number of filenames to keep in the list.')
Expand Down

0 comments on commit eea1353

Please sign in to comment.