Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option to update on edit rather than open #114

Merged
merged 4 commits into from
Jan 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
tgrosinger marked this conversation as resolved.
Show resolved Hide resolved
} 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();
tgrosinger marked this conversation as resolved.
Show resolved Hide resolved
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