@@ -50,7 +50,6 @@ export class WorkspaceService extends TheiaWorkspaceService {
50
50
protected readonly boardsServiceProvider : BoardsServiceProvider ;
51
51
52
52
private version ?: string ;
53
- private optionsToAppendToURI ?: WorkspaceOptions ;
54
53
55
54
async onStart ( application : FrontendApplication ) : Promise < void > {
56
55
const info = await this . applicationServer . getApplicationInfo ( ) ;
@@ -91,25 +90,68 @@ export class WorkspaceService extends TheiaWorkspaceService {
91
90
}
92
91
}
93
92
94
- override open ( uri : URI , options ?: WorkspaceOptions ) : void {
95
- this . optionsToAppendToURI = options ;
96
- super . doOpen ( uri ) ;
93
+ /*
94
+ This method mostly duplicates super.doOpen and super.openWindow because they didn't let pass any custom
95
+ option to openNewWindow
96
+ */
97
+ async openWithCommands ( uri : URI , options ?: WorkspaceOptions ) : Promise < void > {
98
+ const stat = await this . toFileStat ( uri ) ;
99
+ if ( stat ) {
100
+ if ( ! stat . isDirectory && ! this . isWorkspaceFile ( stat ) ) {
101
+ const message = `Not a valid workspace: ${ uri . path . toString ( ) } ` ;
102
+ this . messageService . error ( message ) ;
103
+ throw new Error ( message ) ;
104
+ }
105
+ // The same window has to be preserved too (instead of opening a new one), if the workspace root is not yet available and we are setting it for the first time.
106
+ // Option passed as parameter has the highest priority (for api developers), then the preference, then the default.
107
+ await this . roots ;
108
+ const { preserveWindow } = {
109
+ preserveWindow :
110
+ this . preferences [ 'workspace.preserveWindow' ] || ! this . opened ,
111
+ ...options ,
112
+ } ;
113
+ await this . server . setMostRecentlyUsedWorkspace ( uri . toString ( ) ) ;
114
+ if ( preserveWindow ) {
115
+ this . _workspace = stat ;
116
+ }
117
+
118
+ const workspacePath = stat . resource . path . toString ( ) ;
119
+
120
+ if ( this . shouldPreserveWindow ( options ) ) {
121
+ this . reloadWindow ( ) ;
122
+ } else {
123
+ try {
124
+ this . openNewWindow ( workspacePath , options ) ;
125
+ return ;
126
+ } catch ( error ) {
127
+ // Fall back to reloading the current window in case the browser has blocked the new window
128
+ this . _workspace = stat ;
129
+ this . logger . error ( error . toString ( ) ) . then ( ( ) => this . reloadWindow ( ) ) ;
130
+ }
131
+ }
132
+ }
133
+ throw new Error (
134
+ 'Invalid workspace root URI. Expected an existing directory or workspace file.'
135
+ ) ;
97
136
}
98
137
99
- protected override openNewWindow ( workspacePath : string ) : void {
138
+ protected override openNewWindow (
139
+ workspacePath : string ,
140
+ options ?: WorkspaceOptions
141
+ ) : void {
100
142
const { boardsConfig } = this . boardsServiceProvider ;
101
143
const url = BoardsConfig . Config . setConfig (
102
144
boardsConfig ,
103
145
new URL ( window . location . href )
104
146
) ; // Set the current boards config for the new browser window.
105
147
url . hash = workspacePath ;
106
- if ( this . optionsToAppendToURI ) {
148
+ if ( options ?. commands ) {
107
149
url . searchParams . set (
108
150
'commands' ,
109
- encodeURIComponent ( JSON . stringify ( this . optionsToAppendToURI ? .commands ) )
151
+ encodeURIComponent ( JSON . stringify ( options . commands ) )
110
152
) ;
111
- this . optionsToAppendToURI = undefined ;
112
153
}
154
+
113
155
this . windowService . openNewWindow ( url . toString ( ) ) ;
114
156
}
115
157
0 commit comments