Skip to content

Commit 70aafcf

Browse files
authored
feat: extract editor modified logic to extensions (#138)
1 parent 4afce3e commit 70aafcf

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

src/controller/editor.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,19 @@ export class EditorController extends Controller implements IEditorController {
217217
const newValue = editorInstance.getModel()?.getValue();
218218
const { current } = this.editorService.getState();
219219
const tab = current?.tab;
220+
const originValue = tab?.data?.value;
220221
if (!tab) return;
221-
const notSave = newValue !== tab?.data?.value;
222222
this.editorService.updateTab(
223223
{
224224
id: tab.id,
225225
data: {
226226
...tab.data,
227-
modified: notSave,
228227
value: newValue,
229228
},
230229
},
231230
groupId
232231
);
232+
this.emit(EditorEvent.OnUpdateTab, newValue, groupId, originValue);
233233
this.emit(
234234
FolderTreeEvent.onUpdateFileContent,
235235
current?.tab?.id as any,

src/model/workbench/editor.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export enum EditorEvent {
1212
OnMoveTab = 'editor.moveTab',
1313
OpenTab = 'editor.openTab',
1414
OnSelectTab = 'editor.selectTab',
15+
OnUpdateTab = 'editor.updateTab',
1516
OnSplitEditorRight = 'editor.splitEditorRight',
1617
}
1718
interface BuiltInEditorTabDataType {

src/services/workbench/editorService.ts

+18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as monaco from 'monaco-editor';
44

55
import { Component } from 'mo/react';
66
import {
7+
EditorEvent,
78
EditorModel,
89
EditorGroupModel,
910
IEditor,
@@ -30,6 +31,13 @@ export interface IEditorService extends Component<IEditor> {
3031
closeAll(groupId: number): void;
3132
getGroupById(groupId: number): IEditorGroup | undefined;
3233
cloneGroup(groupId?: number): IEditorGroup;
34+
onUpdateTab(
35+
callback: (
36+
newValue: string,
37+
groupId: number,
38+
originValue?: string
39+
) => void
40+
);
3341
/**
3442
* Set active group and tab
3543
* @param groupId Target group ID
@@ -280,4 +288,14 @@ export class EditorService
280288
});
281289
return cloneGroup;
282290
}
291+
292+
public onUpdateTab(
293+
callback: (
294+
newValue: string,
295+
groupId: number,
296+
originValue?: string
297+
) => void
298+
) {
299+
this.subscribe(EditorEvent.OnUpdateTab, callback);
300+
}
283301
}

stories/extensions/test/testPane.tsx

+34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
import * as monaco from 'monaco-editor';
23
import {
34
activityBarService,
45
colorThemeService,
@@ -92,6 +93,39 @@ export type GenericClassDecorator<T> = (target: T) => void;`,
9293
editorService.open(tabData);
9394
};
9495

96+
editorService.onUpdateTab(
97+
(newValue: string, groupId: number, originValue?: string) => {
98+
const { current } = editorService.getState();
99+
const tab = current?.tab!;
100+
const notSave = newValue !== originValue;
101+
editorService.updateTab(
102+
{
103+
id: tab.id,
104+
data: {
105+
...tab.data,
106+
modified: notSave,
107+
},
108+
},
109+
groupId
110+
);
111+
current?.editorInstance.addCommand(
112+
monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S,
113+
() => {
114+
// ctrl + s
115+
editorService.updateTab(
116+
{
117+
id: tab.id,
118+
data: {
119+
...tab.data,
120+
modified: false,
121+
},
122+
},
123+
groupId
124+
);
125+
}
126+
);
127+
}
128+
);
95129
let notify;
96130
const addANotification = function () {
97131
notify = notificationService.addNotification<string>({

0 commit comments

Comments
 (0)