-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
file-dialog-service.ts
96 lines (84 loc) · 4.26 KB
/
file-dialog-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/********************************************************************************
* Copyright (C) 2018 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { injectable, inject } from 'inversify';
import URI from '@theia/core/lib/common/uri';
import { MaybeArray } from '@theia/core/lib/common';
import { LabelProvider } from '@theia/core/lib/browser';
import { FileStat } from '../../common/files';
import { DirNode } from '../file-tree';
import { OpenFileDialogFactory, OpenFileDialogProps, SaveFileDialogFactory, SaveFileDialogProps } from './file-dialog';
import { FileService } from '../file-service';
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
export const FileDialogService = Symbol('FileDialogService');
export interface FileDialogService {
showOpenDialog(props: OpenFileDialogProps & { canSelectMany: true }, folder?: FileStat): Promise<MaybeArray<URI> | undefined>;
showOpenDialog(props: OpenFileDialogProps, folder?: FileStat): Promise<URI | undefined>;
showOpenDialog(props: OpenFileDialogProps, folder?: FileStat): Promise<MaybeArray<URI> | undefined>;
showSaveDialog(props: SaveFileDialogProps, folder?: FileStat): Promise<URI | undefined>
}
@injectable()
export class DefaultFileDialogService implements FileDialogService {
@inject(EnvVariablesServer)
protected readonly environments: EnvVariablesServer;
@inject(FileService)
protected readonly fileService: FileService;
@inject(OpenFileDialogFactory) protected readonly openFileDialogFactory: OpenFileDialogFactory;
@inject(LabelProvider) protected readonly labelProvider: LabelProvider;
@inject(SaveFileDialogFactory) protected readonly saveFileDialogFactory: SaveFileDialogFactory;
async showOpenDialog(props: OpenFileDialogProps & { canSelectMany: true }, folder?: FileStat): Promise<MaybeArray<URI> | undefined>;
async showOpenDialog(props: OpenFileDialogProps, folder?: FileStat): Promise<URI | undefined>;
async showOpenDialog(props: OpenFileDialogProps, folder?: FileStat): Promise<MaybeArray<URI> | undefined> {
const title = props.title || 'Open';
const rootNode = await this.getRootNode(folder);
if (rootNode) {
const dialog = this.openFileDialogFactory(Object.assign(props, { title }));
await dialog.model.navigateTo(rootNode);
const value = await dialog.open();
if (value) {
if (!Array.isArray(value)) {
return value.uri;
}
return value.map(node => node.uri);
}
}
return undefined;
}
async showSaveDialog(props: SaveFileDialogProps, folder?: FileStat): Promise<URI | undefined> {
const title = props.title || 'Save';
const rootNode = await this.getRootNode(folder);
if (rootNode) {
const dialog = this.saveFileDialogFactory(Object.assign(props, { title }));
await dialog.model.navigateTo(rootNode);
return dialog.open();
}
return undefined;
}
protected async getRootNode(folderToOpen?: FileStat): Promise<DirNode | undefined> {
const folder = folderToOpen || {
resource: new URI(await this.environments.getHomeDirUri()),
isDirectory: true
};
if (folder) {
const folderUri = folder.resource;
const rootUri = folder.isDirectory ? folderUri : folderUri.parent;
try {
const rootStat = await this.fileService.resolve(rootUri);
return DirNode.createRoot(rootStat);
} catch { }
}
return undefined;
}
}