Skip to content
Open
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
19 changes: 15 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
PluginSettingTab,
Setting,
TAbstractFile,
TextFileView,
TFile,
} from 'obsidian';

Expand Down Expand Up @@ -172,15 +173,25 @@ export default class PasteImageRenamePlugin extends Plugin {
// in case fileManager.renameFile may not update the internal link in the active file,
// we manually replace the current line by manipulating the editor

const newLinkText = this.app.fileManager.generateMarkdownLink(file, sourcePath)
debugLog('replace text', linkText, newLinkText)

const editor = this.getActiveEditor()
if (!editor) {
const view = this.app.workspace.getActiveViewOfType(TextFileView)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding a null check for this.app.workspace.getActiveViewOfType(TextFileView) before accessing its properties to avoid potential errors if no view is active.

const view = this.app.workspace.getActiveViewOfType(TextFileView);
			if (view && view.file && view.file.extension === "canvas") {

if (view && view.file.extension === "canvas") {
debugLog('Canvas file view detected', view.file.path)
if (!this.settings.disableRenameNotice) {
new Notice(`Renamed ${originName} to ${newName}`)
}
return
Comment on lines 177 to +184

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider extracting this canvas file check into a separate function for better readability and maintainability. This would also make it easier to test this specific logic.

Also, it might be useful to add a more specific check to ensure that the view is actually a canvas view, and not just a TextFileView with a .canvas extension. This could prevent unexpected behavior if other TextFileView types are introduced in the future.

if (!editor) {
			if (this.isCanvasFileView(this.app.workspace.getActiveViewOfType(TextFileView))) {
				debugLog('Canvas file view detected', view.file.path)
				if (!this.settings.disableRenameNotice) {
					new Notice(`Renamed ${originName} to ${newName}`)
				}
				return
			}
			debugLog('No active editor found')
			new Notice(`Failed to rename ${newName}: no active editor`)
			return
		}

}
debugLog('No active editor found')
new Notice(`Failed to rename ${newName}: no active editor`)
return
}

debugLog('Active editor found, replacing link text')
const newLinkText = this.app.fileManager.generateMarkdownLink(file, sourcePath)
debugLog('replace text', {old: linkText, new: newLinkText})

const cursor = editor.getCursor()
const line = editor.getLine(cursor.line)
const replacedLine = line.replace(linkText, newLinkText)
Expand Down Expand Up @@ -353,7 +364,7 @@ export default class PasteImageRenamePlugin extends Plugin {
}

getActiveFile() {
const view = this.app.workspace.getActiveViewOfType(MarkdownView)
const view = this.app.workspace.getActiveViewOfType(MarkdownView) ?? this.app.workspace.getActiveViewOfType(TextFileView)
const file = view?.file
debugLog('active file', file?.path)
return file
Expand Down