@@ -21,7 +21,7 @@ import type { IPlugin } from 'packages/core/src/IPlugin';
2121import { MDLinkParser } from 'packages/core/src/parsers/MarkdownLinkParser' ;
2222import { ErrorLevel , MetaBindJsError , MetaBindParsingError } from 'packages/core/src/utils/errors/MetaBindErrors' ;
2323import { parseLiteral } from 'packages/core/src/utils/Literal' ;
24- import { expectType } from 'packages/core/src/utils/Utils' ;
24+ import { ensureFileExtension , expectType , joinPath } from 'packages/core/src/utils/Utils' ;
2525
2626export class ButtonActionRunner {
2727 plugin : IPlugin ;
@@ -108,6 +108,7 @@ export class ButtonActionRunner {
108108 folderPath : '/' ,
109109 fileName : '' ,
110110 openNote : true ,
111+ openIfAlreadyExists : false ,
111112 } satisfies TemplaterCreateNoteButtonAction ;
112113 } else if ( type === ButtonActionType . UPDATE_METADATA ) {
113114 return {
@@ -122,6 +123,7 @@ export class ButtonActionRunner {
122123 folderPath : '/' ,
123124 fileName : 'Untitled' ,
124125 openNote : true ,
126+ openIfAlreadyExists : false ,
125127 } satisfies CreateNoteButtonAction ;
126128 } else if ( type === ButtonActionType . REPLACE_IN_NOTE ) {
127129 return {
@@ -265,6 +267,15 @@ export class ButtonActionRunner {
265267 }
266268
267269 async runTemplaterCreateNoteAction ( action : TemplaterCreateNoteButtonAction ) : Promise < void > {
270+ if ( action . openIfAlreadyExists && action . fileName ) {
271+ const filePath = ensureFileExtension ( joinPath ( action . folderPath ?? '' , action . fileName ) , 'md' ) ;
272+ // if the file already exists, open it in the same tab
273+ if ( await this . plugin . internal . existsFilePath ( filePath ) ) {
274+ this . plugin . internal . openFile ( filePath , '' , false ) ;
275+ return ;
276+ }
277+ }
278+
268279 await this . plugin . internal . createNoteWithTemplater (
269280 action . templateFile ,
270281 action . folderPath ,
@@ -302,6 +313,15 @@ export class ButtonActionRunner {
302313 }
303314
304315 async runCreateNoteAction ( action : CreateNoteButtonAction ) : Promise < void > {
316+ if ( action . openIfAlreadyExists ) {
317+ const filePath = ensureFileExtension ( joinPath ( action . folderPath ?? '' , action . fileName ) , 'md' ) ;
318+ // if the file already exists, open it in the same tab
319+ if ( await this . plugin . internal . existsFilePath ( filePath ) ) {
320+ this . plugin . internal . openFile ( filePath , '' , false ) ;
321+ return ;
322+ }
323+ }
324+
305325 await this . plugin . internal . createFile ( action . folderPath ?? '' , action . fileName , 'md' , action . openNote ?? false ) ;
306326 }
307327
0 commit comments