create better structure inspired by ObsidianTweaks'
Node.js build / build (20.x) (push) Has been cancelled
Node.js build / build (22.x) (push) Has been cancelled

This commit is contained in:
olivier committed 2026-02-26 15:19:52 -05:00
1 parent 580f2d8c2b
commit 7022dadd83
12 files changed
+2105 -654

No files matched your search

+61
View File
@@ -0,0 +1,61 @@
import { App, Editor, MarkdownView, Notice } from 'obsidian'
import BindThemPlugin from './main'
/**
* FileHelper provides commands for file operations
*/
export class FileHelper {
public app: App
private plugin: BindThemPlugin
constructor(app: App, plugin: BindThemPlugin) {
this.app = app
this.plugin = plugin
}
/**
* Duplicate the current file
*/
public async duplicateFile(editor: Editor, view: MarkdownView): Promise<void> {
const activeFile = this.app.workspace.getActiveFile()
if (activeFile === null) {
return
}
const selections = editor.listSelections()
const parentPath = activeFile.parent?.path ?? ''
const newFilePath =
parentPath + '/' + activeFile.basename + ' (copy).' + activeFile.extension
try {
const newFile = await this.app.vault.copy(activeFile, newFilePath)
await view.leaf.openFile(newFile)
editor.setSelections(selections)
} catch (e) {
new Notice(String(e))
}
}
/**
* Create a new file in the same directory as the current file
*/
public async newAdjacentFile(editor: Editor, view: MarkdownView): Promise<void> {
const activeFile = this.app.workspace.getActiveFile()
if (activeFile === null) {
return
}
const parentPath = activeFile.parent?.path ?? ''
const newFilePath = parentPath + '/' + 'Untitled.md'
try {
const newFile = await this.app.vault.create(newFilePath, '')
await view.leaf.openFile(newFile)
} catch (e) {
new Notice(String(e))
}
}
}