Skip to content

Commit

Permalink
Merge pull request #758 from JayPritchet/master
Browse files Browse the repository at this point in the history
fix: prevent creating new tab when file is already opened
  • Loading branch information
chhoumann authored Feb 26, 2025
2 parents 82ee14d + 1bf50d6 commit 6be5c8e
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 186 deletions.
19 changes: 12 additions & 7 deletions src/engine/CaptureChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isFolder,
getMarkdownFilesInFolder,
getMarkdownFilesWithTag,
openExistingFileTab,
} from "../utilityObsidian";
import { VALUE_SYNTAX } from "../constants";
import type QuickAdd from "../main";
Expand Down Expand Up @@ -88,13 +89,17 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
appendToCurrentLine(markdownLink, this.app);
}

if (this.choice?.openFile) {
await openFile(this.app, file, {
openInNewTab: this.choice.openFileInNewTab.enabled,
direction: this.choice.openFileInNewTab.direction,
focus: this.choice.openFileInNewTab.focus,
mode: this.choice.openFileInMode,
});
if (this.choice.openFile && file) {
const openExistingTab = await openExistingFileTab(this.app, file);

if (!openExistingTab) {
await openFile(this.app, file, {
openInNewTab: this.choice.openFileInNewTab.enabled,
direction: this.choice.openFileInNewTab.direction,
focus: this.choice.openFileInNewTab.focus,
mode: this.choice.openFileInMode,
});
}
}
} catch (e) {
log.logError(e as string);
Expand Down
64 changes: 33 additions & 31 deletions src/engine/TemplateChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TFile } from "obsidian";
import {
appendToCurrentLine,
getAllFolderPathsInVault,
openExistingFileTab,
openFile,
} from "../utilityObsidian";
import {
Expand All @@ -29,7 +30,7 @@ export class TemplateChoiceEngine extends TemplateEngine {
app: App,
plugin: QuickAdd,
choice: ITemplateChoice,
choiceExecutor: IChoiceExecutor
choiceExecutor: IChoiceExecutor,
) {
super(app, plugin, choiceExecutor);
this.choice = choice;
Expand All @@ -51,19 +52,19 @@ export class TemplateChoiceEngine extends TemplateEngine {
folderPath = await this.getFolderPath();
}

let filePath;
let filePath: string;

if (this.choice.fileNameFormat.enabled) {
filePath = await this.getFormattedFilePath(
folderPath,
this.choice.fileNameFormat.format,
this.choice.name
this.choice.name,
);
} else {
filePath = await this.getFormattedFilePath(
folderPath,
VALUE_SYNTAX,
this.choice.name
this.choice.name,
);
}

Expand All @@ -75,19 +76,19 @@ export class TemplateChoiceEngine extends TemplateEngine {
const file = this.app.vault.getAbstractFileByPath(filePath);
if (!(file instanceof TFile) || file.extension !== "md") {
log.logError(
`'${filePath}' already exists and is not a valid markdown file.`
`'${filePath}' already exists and is not a valid markdown file.`,
);
return;
}

let userChoice: typeof fileExistsChoices[number] =
let userChoice: (typeof fileExistsChoices)[number] =
this.choice.fileExistsMode;

if (!this.choice.setFileExistsBehavior) {
userChoice = await GenericSuggester.Suggest(
this.app,
[...fileExistsChoices],
[...fileExistsChoices]
[...fileExistsChoices],
);
}

Expand All @@ -96,32 +97,30 @@ export class TemplateChoiceEngine extends TemplateEngine {
createdFile = await this.appendToFileWithTemplate(
file,
this.choice.templatePath,
"top"
"top",
);
break;
case fileExistsAppendToBottom:
createdFile = await this.appendToFileWithTemplate(
file,
this.choice.templatePath,
"bottom"
"bottom",
);
break;
case fileExistsOverwriteFile:
createdFile = await this.overwriteFileWithTemplate(
file,
this.choice.templatePath
this.choice.templatePath,
);
break;
case fileExistsDoNothing:
createdFile = file;
break;
case fileExistsIncrement: {
const incrementFileName = await this.incrementFileName(
filePath
);
const incrementFileName = await this.incrementFileName(filePath);
createdFile = await this.createFileWithTemplate(
incrementFileName,
this.choice.templatePath
this.choice.templatePath,
);
break;
}
Expand All @@ -132,7 +131,7 @@ export class TemplateChoiceEngine extends TemplateEngine {
} else {
createdFile = await this.createFileWithTemplate(
filePath,
this.choice.templatePath
this.choice.templatePath,
);
if (!createdFile) {
log.logWarning(`Could not create file '${filePath}'.`);
Expand All @@ -143,17 +142,24 @@ export class TemplateChoiceEngine extends TemplateEngine {
if (this.choice.appendLink && createdFile) {
appendToCurrentLine(
this.app.fileManager.generateMarkdownLink(createdFile, ""),
this.app
this.app,
);
}

if (this.choice.openFile && createdFile) {
await openFile(this.app, createdFile, {
openInNewTab: this.choice.openFileInNewTab.enabled,
direction: this.choice.openFileInNewTab.direction,
focus: this.choice.openFileInNewTab.focus,
mode: this.choice.openFileInMode,
});
const openExistingTab = await openExistingFileTab(
this.app,
createdFile,
);

if (!openExistingTab) {
await openFile(this.app, createdFile, {
openInNewTab: this.choice.openFileInNewTab.enabled,
direction: this.choice.openFileInNewTab.direction,
focus: this.choice.openFileInNewTab.focus,
mode: this.choice.openFileInMode,
});
}
}
} catch (error) {
log.logError(error as string);
Expand All @@ -164,7 +170,7 @@ export class TemplateChoiceEngine extends TemplateEngine {
const folderPaths = await Promise.all(
folders.map(async (folder) => {
return await this.formatter.formatFolderPath(folder);
})
}),
);

return folderPaths;
Expand All @@ -182,9 +188,7 @@ export class TemplateChoiceEngine extends TemplateEngine {
this.choice.folder?.createInSameFolderAsActiveFile
)
) {
const allFoldersInVault: string[] = getAllFolderPathsInVault(
this.app
);
const allFoldersInVault: string[] = getAllFolderPathsInVault(this.app);

const subfolders = allFoldersInVault.filter((folder) => {
return folders.some((f) => folder.startsWith(f));
Expand All @@ -194,9 +198,7 @@ export class TemplateChoiceEngine extends TemplateEngine {
}

if (this.choice.folder?.chooseWhenCreatingNote) {
const allFoldersInVault: string[] = getAllFolderPathsInVault(
this.app
);
const allFoldersInVault: string[] = getAllFolderPathsInVault(this.app);
return await this.getOrCreateFolder(allFoldersInVault);
}

Expand All @@ -205,11 +207,11 @@ export class TemplateChoiceEngine extends TemplateEngine {

if (!activeFile || !activeFile.parent) {
log.logWarning(
"No active file or active file has no parent. Cannot create file in same folder as active file. Creating in root folder."
"No active file or active file has no parent. Cannot create file in same folder as active file. Creating in root folder.",
);
return "";
}

return this.getOrCreateFolder([activeFile.parent.path]);
}

Expand Down
Loading

0 comments on commit 6be5c8e

Please sign in to comment.