Skip to content

Commit 9418d40

Browse files
authored
fix: can't prevent close tab with confirm (#828)
* fix: take default close tab into extensions * chore: add confirm example in testPane
1 parent d1cdbe1 commit 9418d40

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

src/controller/editor.tsx

-5
Original file line numberDiff line numberDiff line change
@@ -142,29 +142,24 @@ export class EditorController extends Controller implements IEditorController {
142142
};
143143

144144
public onCloseAll = (groupId: UniqueId) => {
145-
this.editorService.closeAll(groupId);
146145
this.emit(EditorEvent.OnCloseAll, groupId);
147146
};
148147

149148
public onCloseTab = (tabId?: UniqueId, groupId?: UniqueId) => {
150149
if (tabId && groupId) {
151-
this.editorService.closeTab(tabId, groupId);
152150
this.emit(EditorEvent.OnCloseTab, tabId, groupId);
153151
}
154152
};
155153

156154
public onCloseToRight = (tabItem: IEditorTab, groupId: UniqueId) => {
157-
this.editorService.closeToRight(tabItem, groupId);
158155
this.emit(EditorEvent.OnCloseToRight, tabItem, groupId);
159156
};
160157

161158
public onCloseToLeft = (tabItem: IEditorTab, groupId: UniqueId) => {
162-
this.editorService.closeToLeft(tabItem, groupId);
163159
this.emit(EditorEvent.OnCloseToLeft, tabItem, groupId);
164160
};
165161

166162
public onCloseOther = (tabItem: IEditorTab, groupId: UniqueId) => {
167-
this.editorService.closeOther(tabItem, groupId);
168163
this.emit(EditorEvent.OnCloseOther, tabItem, groupId);
169164
};
170165

src/extensions/editor/index.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import molecule from 'mo';
2+
import { IExtension } from 'mo/model/extension';
3+
4+
export const ExtendsEditor: IExtension = {
5+
id: 'ExtendsEditor',
6+
name: 'Extends Editor',
7+
dispose() {},
8+
activate() {
9+
molecule.editor.onCloseTab((tabId, groupId) => {
10+
if (tabId !== undefined && groupId !== undefined) {
11+
molecule.editor.closeTab(tabId, groupId);
12+
}
13+
});
14+
15+
molecule.editor.onCloseAll((groupId) => {
16+
if (groupId !== undefined) {
17+
molecule.editor.closeAll(groupId);
18+
}
19+
});
20+
21+
molecule.editor.onCloseOther((tabItem, groupId) => {
22+
if (tabItem && groupId !== undefined) {
23+
molecule.editor.closeOther(tabItem, groupId);
24+
}
25+
});
26+
27+
molecule.editor.onCloseToLeft((tabItem, groupId) => {
28+
if (tabItem && groupId !== undefined) {
29+
molecule.editor.closeToLeft(tabItem, groupId);
30+
}
31+
});
32+
33+
molecule.editor.onCloseToRight((tabItem, groupId) => {
34+
if (tabItem && groupId !== undefined) {
35+
molecule.editor.closeToRight(tabItem, groupId);
36+
}
37+
});
38+
},
39+
};

src/extensions/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import { monokaiColorThemeExtension } from './theme-monokai';
1010
import { paleNightColorThemeExtension } from './vscode-palenight-theme';
1111
import { webStormIntelliJExtension } from './vscode-intellij-darcula-theme-master';
1212
import { githubPlusExtension } from './github-plus-theme-master';
13+
import { ExtendsEditor } from './editor';
1314

1415
/**
1516
* Default extensions
1617
*/
1718
export const defaultExtensions = [
1819
ExtendsPanel,
20+
ExtendsEditor,
1921
ExtendsActivityBar,
2022
ExtendsExplorer,
2123
ExtendsEditorTree,

stories/extensions/test/index.tsx

+22
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { FileTypes, IExtension, TreeNodeModel } from 'mo/model';
55

66
import TestPane from './testPane';
77
import { randomId } from 'mo/common/utils';
8+
import { ListenerEventContext } from 'mo/common/event';
9+
import { UniqueId } from 'mo/common/types';
810

911
export const ExtendsTestPane: IExtension = {
1012
id: 'ExtendsTestPane',
@@ -179,5 +181,25 @@ export const ExtendsTestPane: IExtension = {
179181
molecule.explorer.onCollapseAllFolders(() => {
180182
molecule.folderTree.setExpandKeys([]);
181183
});
184+
185+
function closeTabHandler(
186+
this: ListenerEventContext,
187+
tabId: UniqueId,
188+
groupId?: UniqueId
189+
) {
190+
this.stopDelivery();
191+
molecule.component.Modal.confirm({
192+
title: '确认关闭 tab 吗',
193+
content: '关闭后数据会丢失',
194+
onOk() {
195+
if (groupId !== undefined && tabId !== undefined) {
196+
molecule.editor.closeTab(tabId, groupId);
197+
}
198+
},
199+
onCancel() {},
200+
});
201+
}
202+
molecule.editorTree.onClose(closeTabHandler);
203+
molecule.editor.onCloseTab(closeTabHandler);
182204
},
183205
};

0 commit comments

Comments
 (0)