Skip to content

Commit

Permalink
fix: can't prevent close tab with confirm (DTStack#828)
Browse files Browse the repository at this point in the history
* fix: take default close tab into extensions

* chore: add confirm example in testPane
  • Loading branch information
mortalYoung authored Nov 21, 2022
1 parent d1cdbe1 commit 9418d40
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
5 changes: 0 additions & 5 deletions src/controller/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,29 +142,24 @@ export class EditorController extends Controller implements IEditorController {
};

public onCloseAll = (groupId: UniqueId) => {
this.editorService.closeAll(groupId);
this.emit(EditorEvent.OnCloseAll, groupId);
};

public onCloseTab = (tabId?: UniqueId, groupId?: UniqueId) => {
if (tabId && groupId) {
this.editorService.closeTab(tabId, groupId);
this.emit(EditorEvent.OnCloseTab, tabId, groupId);
}
};

public onCloseToRight = (tabItem: IEditorTab, groupId: UniqueId) => {
this.editorService.closeToRight(tabItem, groupId);
this.emit(EditorEvent.OnCloseToRight, tabItem, groupId);
};

public onCloseToLeft = (tabItem: IEditorTab, groupId: UniqueId) => {
this.editorService.closeToLeft(tabItem, groupId);
this.emit(EditorEvent.OnCloseToLeft, tabItem, groupId);
};

public onCloseOther = (tabItem: IEditorTab, groupId: UniqueId) => {
this.editorService.closeOther(tabItem, groupId);
this.emit(EditorEvent.OnCloseOther, tabItem, groupId);
};

Expand Down
39 changes: 39 additions & 0 deletions src/extensions/editor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import molecule from 'mo';
import { IExtension } from 'mo/model/extension';

export const ExtendsEditor: IExtension = {
id: 'ExtendsEditor',
name: 'Extends Editor',
dispose() {},
activate() {
molecule.editor.onCloseTab((tabId, groupId) => {
if (tabId !== undefined && groupId !== undefined) {
molecule.editor.closeTab(tabId, groupId);
}
});

molecule.editor.onCloseAll((groupId) => {
if (groupId !== undefined) {
molecule.editor.closeAll(groupId);
}
});

molecule.editor.onCloseOther((tabItem, groupId) => {
if (tabItem && groupId !== undefined) {
molecule.editor.closeOther(tabItem, groupId);
}
});

molecule.editor.onCloseToLeft((tabItem, groupId) => {
if (tabItem && groupId !== undefined) {
molecule.editor.closeToLeft(tabItem, groupId);
}
});

molecule.editor.onCloseToRight((tabItem, groupId) => {
if (tabItem && groupId !== undefined) {
molecule.editor.closeToRight(tabItem, groupId);
}
});
},
};
2 changes: 2 additions & 0 deletions src/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import { monokaiColorThemeExtension } from './theme-monokai';
import { paleNightColorThemeExtension } from './vscode-palenight-theme';
import { webStormIntelliJExtension } from './vscode-intellij-darcula-theme-master';
import { githubPlusExtension } from './github-plus-theme-master';
import { ExtendsEditor } from './editor';

/**
* Default extensions
*/
export const defaultExtensions = [
ExtendsPanel,
ExtendsEditor,
ExtendsActivityBar,
ExtendsExplorer,
ExtendsEditorTree,
Expand Down
22 changes: 22 additions & 0 deletions stories/extensions/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { FileTypes, IExtension, TreeNodeModel } from 'mo/model';

import TestPane from './testPane';
import { randomId } from 'mo/common/utils';
import { ListenerEventContext } from 'mo/common/event';
import { UniqueId } from 'mo/common/types';

export const ExtendsTestPane: IExtension = {
id: 'ExtendsTestPane',
Expand Down Expand Up @@ -179,5 +181,25 @@ export const ExtendsTestPane: IExtension = {
molecule.explorer.onCollapseAllFolders(() => {
molecule.folderTree.setExpandKeys([]);
});

function closeTabHandler(
this: ListenerEventContext,
tabId: UniqueId,
groupId?: UniqueId
) {
this.stopDelivery();
molecule.component.Modal.confirm({
title: '确认关闭 tab 吗',
content: '关闭后数据会丢失',
onOk() {
if (groupId !== undefined && tabId !== undefined) {
molecule.editor.closeTab(tabId, groupId);
}
},
onCancel() {},
});
}
molecule.editorTree.onClose(closeTabHandler);
molecule.editor.onCloseTab(closeTabHandler);
},
};

0 comments on commit 9418d40

Please sign in to comment.