Write a plugin script for the Obsidian knowledge base program called "File Versioning".…
- User
Write a plugin script for the Obsidian knowledge base program called "File Versioning". A sample plugin can be found here: https://github.com/obsidianmd/obsidian-sample-plugin
This is how the plugin should work:
- The plugin settings let the user specify the maximum number of versions to keep (or set no limit), the location of where the versions should be kept and the minimum time after which a new version may be created.
- Whenever Obsidian saves a note, and if the specified minimum duration is met, the plugin will create a version backup of that note in the specified folder before the file is saved, it will also delete the oldest file version depending on the specified maximum number of versions to keep.
- The filename of a versioned file must have a suffix with a unique timecode through which the plugin can figure out the order of the versions.
- The versions of a file will be shown in the sidebar where the user can select the version to be restored.
- When the user restores a version, the current file is backed up just like any other version after which it will be replaced by the selected version.
- Assistant
- User
FileExplorerView does not exist in Obsidian. Can you fix that?
- Assistant
- User
getAdjacentLeaf does not exist. Can you fix that?
- Assistant
Apologies for the confusion. Instead of using
getAdjacentLeaf, we can create a new leaf by splitting the active leaf.Here's the updated code to properly open the
FileVersionsViewwith the available Obsidian APIs:// ... previous code remains the same ... async showVersions(file: TFile) { const activeLeaf = this.app.workspace.activeLeaf; const fileVersionsLeaf = this.app.workspace.getLeavesOfType("file-versions-view")?.[0] || activeLeaf.split(activeLeaf.root.split, "file-versions-view"); await fileVersionsLeaf.open( { type: "file-versions-view", activeFile: file.path, }, { focus: true } ); } // ... rest of the code remains the same ...This code splits the active leaf and opens the
FileVersionsViewin the newly created leaf. If a leaf with theFileVersionsViewtype already exists, it reuses that leaf instead of creating a new one. - User
Failed to open view TypeError: e.open is not a function at t. (app.js:1:1673235) at app.js:1:235836 at Object.next (app.js:1:235941) at a (app.js:1:234680)
- Assistant
I apologize for the error in the code. Let's fix the issue by explicitly setting the view state of the leaf and update the view accordingly.
Here's the updated code for the
showVersionsfunction:// ... previous code remains the same ... async showVersions(file: TFile) { const activeLeaf = this.app.workspace.activeLeaf; const fileVersionsLeaf = this.app.workspace.getLeavesOfType("file-versions-view")?.[0] || activeLeaf.split(activeLeaf.root.split, "file-versions-view"); // Set the view state with the active file's path fileVersionsLeaf.setViewState({ type: "file-versions-view", activeFile: file.path, }); // If the view already exists, load the new data in the existing view const fileVersionsView = fileVersionsLeaf.view as FileVersionsView; if (fileVersionsView) { fileVersionsView.file = file; fileVersionsView.reloadData(); } // Focus the new leaf this.app.workspace.setActiveLeaf(fileVersionsLeaf); } // ... rest of the code remains the same ...This code sets the view state of the
fileVersionsLeafand loads the new data into the view accordingly. Afterward, it sets the focus on the new leaf. This should resolve the error and open the view correctly. - User
- Assistant
I apologize for the oversight in my previous response. Thank you for pointing out the issue. I've gone through the code again and made the necessary changes.
The following updated code should work without errors:
// ... imports and code above remains the same ... export default class FileVersioningPlugin extends Plugin { settings: FileVersioningSettings; async onload() { // ... onload code remains the same ... this.registerView( "file-versions-view", (leaf: WorkspaceLeaf) => new FileVersionsView(leaf, this) ); } // ... other functions remain the same ... async showVersions(file: TFile) { const existingLeaf = this.app.workspace.getLeavesOfType( "file-versions-view" )?.[0]; const leaf = existingLeaf || this.app.workspace.splitActiveLeaf("horizontal", false); leaf.setViewState({ type: "file-versions-view", activeFile: file.path }) .then((fileVersionsView) => { // Check if the view has been created before if (!fileVersionsView) { existingLeaf.setActive(true); return; } // Focus the leaf once it has been created. leaf.setActive(true); }) .catch((err) => { console.error("Failed to open view.", err); }); } // ... other methods remain the same ... } // ... rest of the code remains the same ...