Skip to content

Commit 396020d

Browse files
committed
Hook up basic save prompt for custom editors
Saving itself not implemented :)
1 parent 4753340 commit 396020d

3 files changed

Lines changed: 47 additions & 30 deletions

File tree

src/vs/platform/dialogs/common/dialogs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export interface IFileDialogService {
239239
}
240240

241241
const MAX_CONFIRM_FILES = 10;
242-
export function getConfirmMessage(start: string, resourcesToConfirm: URI[]): string {
242+
export function getConfirmMessage(start: string, resourcesToConfirm: readonly URI[]): string {
243243
const message = [start];
244244
message.push('');
245245
message.push(...resourcesToConfirm.slice(0, MAX_CONFIRM_FILES).map(r => basename(r)));

src/vs/workbench/contrib/customEditor/browser/customEditorInput.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import { memoize } from 'vs/base/common/decorators';
77
import { UnownedDisposable } from 'vs/base/common/lifecycle';
88
import { basename } from 'vs/base/common/path';
99
import { URI } from 'vs/base/common/uri';
10+
import { WebviewEditorState } from 'vs/editor/common/modes';
11+
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
1012
import { IEditorModel } from 'vs/platform/editor/common/editor';
1113
import { ILabelService } from 'vs/platform/label/common/label';
12-
import { IEditorInput, Verbosity } from 'vs/workbench/common/editor';
14+
import { ConfirmResult, IEditorInput, Verbosity } from 'vs/workbench/common/editor';
1315
import { WebviewEditorOverlay } from 'vs/workbench/contrib/webview/browser/webview';
1416
import { WebviewInput } from 'vs/workbench/contrib/webview/browser/webviewEditorInput';
1517
import { IWebviewEditorService } from 'vs/workbench/contrib/webview/browser/webviewEditorService';
1618
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
17-
import { WebviewEditorState } from 'vs/editor/common/modes';
19+
import { promptSave } from 'vs/workbench/services/textfile/common/textFileService';
1820

1921
export class CustomFileEditorInput extends WebviewInput {
2022

@@ -30,12 +32,10 @@ export class CustomFileEditorInput extends WebviewInput {
3032
viewType: string,
3133
id: string,
3234
webview: UnownedDisposable<WebviewEditorOverlay>,
33-
@ILabelService
34-
private readonly labelService: ILabelService,
35-
@IWebviewEditorService
36-
private readonly _webviewEditorService: IWebviewEditorService,
37-
@IExtensionService
38-
private readonly _extensionService: IExtensionService
35+
@ILabelService private readonly labelService: ILabelService,
36+
@IWebviewEditorService private readonly _webviewEditorService: IWebviewEditorService,
37+
@IExtensionService private readonly _extensionService: IExtensionService,
38+
@IDialogService private readonly dialogService: IDialogService,
3939
) {
4040
super(id, viewType, '', undefined, webview);
4141
this._editorResource = resource;
@@ -77,7 +77,7 @@ export class CustomFileEditorInput extends WebviewInput {
7777
return this.labelService.getUriLabel(this.getResource());
7878
}
7979

80-
getTitle(verbosity?: Verbosity): string {
80+
public getTitle(verbosity?: Verbosity): string {
8181
switch (verbosity) {
8282
case Verbosity.SHORT:
8383
return this.shortTitle;
@@ -106,4 +106,16 @@ export class CustomFileEditorInput extends WebviewInput {
106106
public isDirty() {
107107
return this._state === WebviewEditorState.Dirty;
108108
}
109+
110+
public async confirmSave(): Promise<ConfirmResult> {
111+
if (!this.isDirty()) {
112+
return ConfirmResult.DONT_SAVE;
113+
}
114+
return promptSave(this.dialogService, [this.getResource()]);
115+
}
116+
117+
public async save(): Promise<boolean> {
118+
// TODO
119+
return true;
120+
}
109121
}

src/vs/workbench/services/textfile/common/textFileService.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -578,26 +578,7 @@ export abstract class TextFileService extends Disposable implements ITextFileSer
578578
if (resourcesToConfirm.length === 0) {
579579
return ConfirmResult.DONT_SAVE;
580580
}
581-
582-
const message = resourcesToConfirm.length === 1 ? nls.localize('saveChangesMessage', "Do you want to save the changes you made to {0}?", basename(resourcesToConfirm[0]))
583-
: getConfirmMessage(nls.localize('saveChangesMessages', "Do you want to save the changes to the following {0} files?", resourcesToConfirm.length), resourcesToConfirm);
584-
585-
const buttons: string[] = [
586-
resourcesToConfirm.length > 1 ? nls.localize({ key: 'saveAll', comment: ['&& denotes a mnemonic'] }, "&&Save All") : nls.localize({ key: 'save', comment: ['&& denotes a mnemonic'] }, "&&Save"),
587-
nls.localize({ key: 'dontSave', comment: ['&& denotes a mnemonic'] }, "Do&&n't Save"),
588-
nls.localize('cancel', "Cancel")
589-
];
590-
591-
const { choice } = await this.dialogService.show(Severity.Warning, message, buttons, {
592-
cancelId: 2,
593-
detail: nls.localize('saveChangesDetail', "Your changes will be lost if you don't save them.")
594-
});
595-
596-
switch (choice) {
597-
case 0: return ConfirmResult.SAVE;
598-
case 1: return ConfirmResult.DONT_SAVE;
599-
default: return ConfirmResult.CANCEL;
600-
}
581+
return promptSave(this.dialogService, resourcesToConfirm);
601582
}
602583

603584
async confirmOverwrite(resource: URI): Promise<boolean> {
@@ -1062,3 +1043,27 @@ export abstract class TextFileService extends Disposable implements ITextFileSer
10621043
super.dispose();
10631044
}
10641045
}
1046+
1047+
export async function promptSave(dialogService: IDialogService, resourcesToConfirm: readonly URI[]) {
1048+
const message = resourcesToConfirm.length === 1
1049+
? nls.localize('saveChangesMessage', "Do you want to save the changes you made to {0}?", basename(resourcesToConfirm[0]))
1050+
: getConfirmMessage(nls.localize('saveChangesMessages', "Do you want to save the changes to the following {0} files?", resourcesToConfirm.length), resourcesToConfirm);
1051+
1052+
const buttons: string[] = [
1053+
resourcesToConfirm.length > 1 ? nls.localize({ key: 'saveAll', comment: ['&& denotes a mnemonic'] }, "&&Save All") : nls.localize({ key: 'save', comment: ['&& denotes a mnemonic'] }, "&&Save"),
1054+
nls.localize({ key: 'dontSave', comment: ['&& denotes a mnemonic'] }, "Do&&n't Save"),
1055+
nls.localize('cancel', "Cancel")
1056+
];
1057+
1058+
const { choice } = await dialogService.show(Severity.Warning, message, buttons, {
1059+
cancelId: 2,
1060+
detail: nls.localize('saveChangesDetail', "Your changes will be lost if you don't save them.")
1061+
});
1062+
1063+
switch (choice) {
1064+
case 0: return ConfirmResult.SAVE;
1065+
case 1: return ConfirmResult.DONT_SAVE;
1066+
default: return ConfirmResult.CANCEL;
1067+
}
1068+
}
1069+

0 commit comments

Comments
 (0)