-
Notifications
You must be signed in to change notification settings - Fork 51
added canvas support for pasted image in canvas #110
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import { | |
PluginSettingTab, | ||
Setting, | ||
TAbstractFile, | ||
TextFileView, | ||
TFile, | ||
} from 'obsidian'; | ||
|
||
|
@@ -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) | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 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) | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a null check for
this.app.workspace.getActiveViewOfType(TextFileView)
before accessing its properties to avoid potential errors if no view is active.