Skip to content

Commit 6be5c8e

Browse files
authored
Merge pull request #758 from JayPritchet/master
fix: prevent creating new tab when file is already opened
2 parents 82ee14d + 1bf50d6 commit 6be5c8e

File tree

7 files changed

+178
-186
lines changed

7 files changed

+178
-186
lines changed

src/engine/CaptureChoiceEngine.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
isFolder,
1212
getMarkdownFilesInFolder,
1313
getMarkdownFilesWithTag,
14+
openExistingFileTab,
1415
} from "../utilityObsidian";
1516
import { VALUE_SYNTAX } from "../constants";
1617
import type QuickAdd from "../main";
@@ -88,13 +89,17 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
8889
appendToCurrentLine(markdownLink, this.app);
8990
}
9091

91-
if (this.choice?.openFile) {
92-
await openFile(this.app, file, {
93-
openInNewTab: this.choice.openFileInNewTab.enabled,
94-
direction: this.choice.openFileInNewTab.direction,
95-
focus: this.choice.openFileInNewTab.focus,
96-
mode: this.choice.openFileInMode,
97-
});
92+
if (this.choice.openFile && file) {
93+
const openExistingTab = await openExistingFileTab(this.app, file);
94+
95+
if (!openExistingTab) {
96+
await openFile(this.app, file, {
97+
openInNewTab: this.choice.openFileInNewTab.enabled,
98+
direction: this.choice.openFileInNewTab.direction,
99+
focus: this.choice.openFileInNewTab.focus,
100+
mode: this.choice.openFileInMode,
101+
});
102+
}
98103
}
99104
} catch (e) {
100105
log.logError(e as string);

src/engine/TemplateChoiceEngine.ts

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { TFile } from "obsidian";
44
import {
55
appendToCurrentLine,
66
getAllFolderPathsInVault,
7+
openExistingFileTab,
78
openFile,
89
} from "../utilityObsidian";
910
import {
@@ -29,7 +30,7 @@ export class TemplateChoiceEngine extends TemplateEngine {
2930
app: App,
3031
plugin: QuickAdd,
3132
choice: ITemplateChoice,
32-
choiceExecutor: IChoiceExecutor
33+
choiceExecutor: IChoiceExecutor,
3334
) {
3435
super(app, plugin, choiceExecutor);
3536
this.choice = choice;
@@ -51,19 +52,19 @@ export class TemplateChoiceEngine extends TemplateEngine {
5152
folderPath = await this.getFolderPath();
5253
}
5354

54-
let filePath;
55+
let filePath: string;
5556

5657
if (this.choice.fileNameFormat.enabled) {
5758
filePath = await this.getFormattedFilePath(
5859
folderPath,
5960
this.choice.fileNameFormat.format,
60-
this.choice.name
61+
this.choice.name,
6162
);
6263
} else {
6364
filePath = await this.getFormattedFilePath(
6465
folderPath,
6566
VALUE_SYNTAX,
66-
this.choice.name
67+
this.choice.name,
6768
);
6869
}
6970

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

83-
let userChoice: typeof fileExistsChoices[number] =
84+
let userChoice: (typeof fileExistsChoices)[number] =
8485
this.choice.fileExistsMode;
8586

8687
if (!this.choice.setFileExistsBehavior) {
8788
userChoice = await GenericSuggester.Suggest(
8889
this.app,
8990
[...fileExistsChoices],
90-
[...fileExistsChoices]
91+
[...fileExistsChoices],
9192
);
9293
}
9394

@@ -96,32 +97,30 @@ export class TemplateChoiceEngine extends TemplateEngine {
9697
createdFile = await this.appendToFileWithTemplate(
9798
file,
9899
this.choice.templatePath,
99-
"top"
100+
"top",
100101
);
101102
break;
102103
case fileExistsAppendToBottom:
103104
createdFile = await this.appendToFileWithTemplate(
104105
file,
105106
this.choice.templatePath,
106-
"bottom"
107+
"bottom",
107108
);
108109
break;
109110
case fileExistsOverwriteFile:
110111
createdFile = await this.overwriteFileWithTemplate(
111112
file,
112-
this.choice.templatePath
113+
this.choice.templatePath,
113114
);
114115
break;
115116
case fileExistsDoNothing:
116117
createdFile = file;
117118
break;
118119
case fileExistsIncrement: {
119-
const incrementFileName = await this.incrementFileName(
120-
filePath
121-
);
120+
const incrementFileName = await this.incrementFileName(filePath);
122121
createdFile = await this.createFileWithTemplate(
123122
incrementFileName,
124-
this.choice.templatePath
123+
this.choice.templatePath,
125124
);
126125
break;
127126
}
@@ -132,7 +131,7 @@ export class TemplateChoiceEngine extends TemplateEngine {
132131
} else {
133132
createdFile = await this.createFileWithTemplate(
134133
filePath,
135-
this.choice.templatePath
134+
this.choice.templatePath,
136135
);
137136
if (!createdFile) {
138137
log.logWarning(`Could not create file '${filePath}'.`);
@@ -143,17 +142,24 @@ export class TemplateChoiceEngine extends TemplateEngine {
143142
if (this.choice.appendLink && createdFile) {
144143
appendToCurrentLine(
145144
this.app.fileManager.generateMarkdownLink(createdFile, ""),
146-
this.app
145+
this.app,
147146
);
148147
}
149148

150149
if (this.choice.openFile && createdFile) {
151-
await openFile(this.app, createdFile, {
152-
openInNewTab: this.choice.openFileInNewTab.enabled,
153-
direction: this.choice.openFileInNewTab.direction,
154-
focus: this.choice.openFileInNewTab.focus,
155-
mode: this.choice.openFileInMode,
156-
});
150+
const openExistingTab = await openExistingFileTab(
151+
this.app,
152+
createdFile,
153+
);
154+
155+
if (!openExistingTab) {
156+
await openFile(this.app, createdFile, {
157+
openInNewTab: this.choice.openFileInNewTab.enabled,
158+
direction: this.choice.openFileInNewTab.direction,
159+
focus: this.choice.openFileInNewTab.focus,
160+
mode: this.choice.openFileInMode,
161+
});
162+
}
157163
}
158164
} catch (error) {
159165
log.logError(error as string);
@@ -164,7 +170,7 @@ export class TemplateChoiceEngine extends TemplateEngine {
164170
const folderPaths = await Promise.all(
165171
folders.map(async (folder) => {
166172
return await this.formatter.formatFolderPath(folder);
167-
})
173+
}),
168174
);
169175

170176
return folderPaths;
@@ -182,9 +188,7 @@ export class TemplateChoiceEngine extends TemplateEngine {
182188
this.choice.folder?.createInSameFolderAsActiveFile
183189
)
184190
) {
185-
const allFoldersInVault: string[] = getAllFolderPathsInVault(
186-
this.app
187-
);
191+
const allFoldersInVault: string[] = getAllFolderPathsInVault(this.app);
188192

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

196200
if (this.choice.folder?.chooseWhenCreatingNote) {
197-
const allFoldersInVault: string[] = getAllFolderPathsInVault(
198-
this.app
199-
);
201+
const allFoldersInVault: string[] = getAllFolderPathsInVault(this.app);
200202
return await this.getOrCreateFolder(allFoldersInVault);
201203
}
202204

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

206208
if (!activeFile || !activeFile.parent) {
207209
log.logWarning(
208-
"No active file or active file has no parent. Cannot create file in same folder as active file. Creating in root folder."
210+
"No active file or active file has no parent. Cannot create file in same folder as active file. Creating in root folder.",
209211
);
210212
return "";
211213
}
212-
214+
213215
return this.getOrCreateFolder([activeFile.parent.path]);
214216
}
215217

0 commit comments

Comments
 (0)