@@ -2,6 +2,8 @@ import type { App } from "obsidian";
22import { Modal , Setting , ButtonComponent } from "obsidian" ;
33import type { IOpenFileCommand } from "../../types/macros/QuickCommands/IOpenFileCommand" ;
44import { NewTabDirection } from "../../types/newTabDirection" ;
5+ import type { OpenLocation } from "../../types/fileOpening" ;
6+ import { CommandType } from "../../types/macros/CommandType" ;
57
68export class OpenFileCommandSettingsModal extends Modal {
79 public waitForClose : Promise < IOpenFileCommand | null > ;
@@ -13,7 +15,11 @@ export class OpenFileCommandSettingsModal extends Modal {
1315 constructor ( app : App , command : IOpenFileCommand ) {
1416 super ( app ) ;
1517 this . originalCommand = command ;
16- this . command = { ...command } ; // Create a copy to avoid mutating original
18+ this . command = { ...command , type : CommandType . OpenFile } ; // copy and ensure type
19+
20+ // Backfill defaults for legacy commands
21+ this . command . focus = this . command . focus ?? true ;
22+ this . command . location = this . command . location ?? this . deriveLocation ( ) ;
1723
1824 this . waitForClose = new Promise < IOpenFileCommand | null > ( ( resolve ) => {
1925 this . resolvePromise = resolve ;
@@ -47,12 +53,9 @@ export class OpenFileCommandSettingsModal extends Modal {
4753 headerEl . style . textAlign = "center" ;
4854
4955 this . addFilePathSetting ( ) ;
50- this . addOpenInNewTabSetting ( ) ;
51-
52- if ( this . command . openInNewTab ) {
53- this . addDirectionSetting ( ) ;
54- }
55-
56+ this . addOpenLocationSetting ( ) ;
57+ this . addFocusSetting ( ) ;
58+
5659 this . addButtonBar ( ) ;
5760 }
5861
@@ -72,56 +75,80 @@ export class OpenFileCommandSettingsModal extends Modal {
7275
7376
7477
75- private addOpenInNewTabSetting ( ) {
78+ private addOpenLocationSetting ( ) {
7679 new Setting ( this . contentEl )
77- . setName ( "Open in new tab" )
78- . setDesc ( "Open the file in a new tab" )
79- . addToggle ( toggle => toggle
80- . setValue ( this . command . openInNewTab || false )
81- . onChange ( value => {
82- this . command . openInNewTab = value ;
83- // Clear direction when new tab is disabled
84- if ( ! value ) {
85- this . command . direction = undefined ;
86- }
87- this . reload ( ) ;
88- } )
89- ) ;
80+ . setName ( "Where to open" )
81+ . setDesc ( "Choose tab, split, window, or sidebar" )
82+ . addDropdown ( ( dropdown ) => {
83+ dropdown
84+ . addOption ( "reuse" , "Reuse active tab" )
85+ . addOption ( "tab" , "New tab" )
86+ . addOption ( "split" , "Split" )
87+ . addOption ( "window" , "New window" )
88+ . addOption ( "left-sidebar" , "Left sidebar" )
89+ . addOption ( "right-sidebar" , "Right sidebar" )
90+ . setValue ( ( this . command . location ?? this . deriveLocation ( ) ) . toString ( ) )
91+ . onChange ( ( value ) => {
92+ this . command . location = value as OpenLocation ;
93+ if ( value !== "split" ) {
94+ this . command . direction = undefined ;
95+ } else if ( ! this . command . direction ) {
96+ this . command . direction = NewTabDirection . vertical ;
97+ }
98+ this . reload ( ) ;
99+ } ) ;
100+ } ) ;
101+
102+ if ( ( this . command . location ?? this . deriveLocation ( ) ) === "split" ) {
103+ this . addDirectionSetting ( ) ;
104+ }
90105 }
91106
92107 private addDirectionSetting ( ) {
93108 new Setting ( this . contentEl )
94109 . setName ( "Split direction" )
95- . setDesc ( "Which direction to split when opening in new tab " )
96- . addDropdown ( dropdown => {
110+ . setDesc ( "Which direction to split when opening" )
111+ . addDropdown ( ( dropdown ) => {
97112 dropdown
98- . addOption ( "" , "No split" )
99- . addOption ( NewTabDirection . horizontal , "Horizontal" )
100113 . addOption ( NewTabDirection . vertical , "Vertical" )
101- . setValue ( this . command . direction || "" )
102- . onChange ( value => {
103- this . command . direction = value as NewTabDirection || undefined ;
114+ . addOption ( NewTabDirection . horizontal , "Horizontal" )
115+ . setValue ( this . command . direction ?? NewTabDirection . vertical )
116+ . onChange ( ( value ) => {
117+ this . command . direction = value as NewTabDirection ;
104118 } ) ;
105119 } ) ;
106120 }
107121
122+ private addFocusSetting ( ) {
123+ new Setting ( this . contentEl )
124+ . setName ( "Focus opened tab" )
125+ . setDesc ( "Bring the opened file to the foreground" )
126+ . addToggle ( ( toggle ) =>
127+ toggle
128+ . setValue ( this . command . focus ?? true )
129+ . onChange ( ( value ) => {
130+ this . command . focus = value ;
131+ } )
132+ ) ;
133+ }
134+
135+ private deriveLocation ( ) : OpenLocation {
136+ if ( this . command . location ) return this . command . location ;
137+ if ( this . command . openInNewTab ) {
138+ return this . command . direction ? "split" : "tab" ;
139+ }
140+ return "reuse" ;
141+ }
142+
108143
109144
110145 private addButtonBar ( ) {
111146 const buttonContainer = this . contentEl . createDiv ( ) ;
112147 buttonContainer . style . display = "flex" ;
113- buttonContainer . style . justifyContent = "space-between" ;
148+ buttonContainer . style . justifyContent = "flex-end" ;
149+ buttonContainer . style . gap = "8px" ;
114150 buttonContainer . style . marginTop = "20px" ;
115151
116- const saveButton = new ButtonComponent ( buttonContainer ) ;
117- saveButton
118- . setButtonText ( "Save" )
119- . setCta ( )
120- . onClick ( ( ) => {
121- this . resolveWithGuard ( this . command ) ;
122- this . close ( ) ;
123- } ) ;
124-
125152 const cancelButton = new ButtonComponent ( buttonContainer ) ;
126153 cancelButton
127154 . setButtonText ( "Cancel" )
@@ -130,6 +157,15 @@ export class OpenFileCommandSettingsModal extends Modal {
130157 this . resolveWithGuard ( null ) ;
131158 this . close ( ) ;
132159 } ) ;
160+
161+ const saveButton = new ButtonComponent ( buttonContainer ) ;
162+ saveButton
163+ . setButtonText ( "Save" )
164+ . setCta ( )
165+ . onClick ( ( ) => {
166+ this . resolveWithGuard ( this . command ) ;
167+ this . close ( ) ;
168+ } ) ;
133169 }
134170
135171 private reload ( ) {
0 commit comments