Skip to content

Commit

Permalink
custom-editor: fix closing dirty custom text editor (#11593)
Browse files Browse the repository at this point in the history
The commit fixes an issue where closing dirty custom editors will just result in a save rather than prompt users to confirm if they wish to close the editor, cancel, or perform a save.
  • Loading branch information
safisa authored Sep 19, 2022
1 parent 3a5172a commit 5974d26
Showing 1 changed file with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class CustomEditorsMainImpl implements CustomEditorsMain, Disposable {

switch (modelType) {
case CustomEditorModelType.Text: {
const model = CustomTextEditorModel.create(viewType, resource, this.textModelService, this.fileService);
const model = CustomTextEditorModel.create(viewType, resource, this.textModelService, this.fileService, this.editorPreferences);
return this.customEditorService.models.add(resource, viewType, model);
}
case CustomEditorModelType.Custom: {
Expand Down Expand Up @@ -520,31 +520,49 @@ export class CustomTextEditorModel implements CustomEditorModel {
private readonly toDispose = new DisposableCollection();
private readonly onDirtyChangedEmitter = new Emitter<void>();
readonly onDirtyChanged = this.onDirtyChangedEmitter.event;
readonly autoSave: 'off' | 'afterDelay' | 'onFocusChange' | 'onWindowChange';
autoSave: 'off' | 'afterDelay' | 'onFocusChange' | 'onWindowChange';
autoSaveDelay: number;

static async create(
viewType: string,
resource: TheiaURI,
editorModelService: EditorModelService,
fileService: FileService
fileService: FileService,
editorPreferences: EditorPreferences,
): Promise<CustomTextEditorModel> {
const model = await editorModelService.createModelReference(resource);
model.object.suppressOpenEditorWhenDirty = true;
return new CustomTextEditorModel(viewType, resource, model, fileService);
return new CustomTextEditorModel(viewType, resource, model, fileService, editorPreferences);
}

constructor(
readonly viewType: string,
readonly editorResource: TheiaURI,
private readonly model: Reference<MonacoEditorModel>,
private readonly fileService: FileService
private readonly fileService: FileService,
private readonly editorPreferences: EditorPreferences
) {
this.toDispose.push(
this.editorTextModel.onDirtyChanged(e => {
this.onDirtyChangedEmitter.fire();
})
);
this.toDispose.push(this.onDirtyChangedEmitter);

this.autoSave = this.editorPreferences.get('files.autoSave', undefined, editorResource.toString());
this.autoSaveDelay = this.editorPreferences.get('files.autoSaveDelay', undefined, editorResource.toString());

this.toDispose.push(
this.editorPreferences.onPreferenceChanged(event => {
if (event.preferenceName === 'files.autoSave') {
this.autoSave = this.editorPreferences.get('files.autoSave', undefined, editorResource.toString());
}
if (event.preferenceName === 'files.autoSaveDelay') {
this.autoSaveDelay = this.editorPreferences.get('files.autoSaveDelay', undefined, editorResource.toString());
}
})
);
this.toDispose.push(this.onDirtyChangedEmitter);
}

dispose(): void {
Expand Down

0 comments on commit 5974d26

Please sign in to comment.