diff --git a/src/controller/editor.tsx b/src/controller/editor.tsx index 7a72d20dd..b06727e03 100644 --- a/src/controller/editor.tsx +++ b/src/controller/editor.tsx @@ -217,19 +217,19 @@ export class EditorController extends Controller implements IEditorController { const newValue = editorInstance.getModel()?.getValue(); const { current } = this.editorService.getState(); const tab = current?.tab; + const originValue = tab?.data?.value; if (!tab) return; - const notSave = newValue !== tab?.data?.value; this.editorService.updateTab( { id: tab.id, data: { ...tab.data, - modified: notSave, value: newValue, }, }, groupId ); + this.emit(EditorEvent.OnUpdateTab, newValue, groupId, originValue); this.emit( FolderTreeEvent.onUpdateFileContent, current?.tab?.id as any, diff --git a/src/model/workbench/editor.ts b/src/model/workbench/editor.ts index a12d86e2b..f79974b38 100644 --- a/src/model/workbench/editor.ts +++ b/src/model/workbench/editor.ts @@ -12,6 +12,7 @@ export enum EditorEvent { OnMoveTab = 'editor.moveTab', OpenTab = 'editor.openTab', OnSelectTab = 'editor.selectTab', + OnUpdateTab = 'editor.updateTab', OnSplitEditorRight = 'editor.splitEditorRight', } interface BuiltInEditorTabDataType { diff --git a/src/services/workbench/editorService.ts b/src/services/workbench/editorService.ts index b4b88f89c..52a8c1749 100644 --- a/src/services/workbench/editorService.ts +++ b/src/services/workbench/editorService.ts @@ -4,6 +4,7 @@ import * as monaco from 'monaco-editor'; import { Component } from 'mo/react'; import { + EditorEvent, EditorModel, EditorGroupModel, IEditor, @@ -30,6 +31,13 @@ export interface IEditorService extends Component { closeAll(groupId: number): void; getGroupById(groupId: number): IEditorGroup | undefined; cloneGroup(groupId?: number): IEditorGroup; + onUpdateTab( + callback: ( + newValue: string, + groupId: number, + originValue?: string + ) => void + ); /** * Set active group and tab * @param groupId Target group ID @@ -280,4 +288,14 @@ export class EditorService }); return cloneGroup; } + + public onUpdateTab( + callback: ( + newValue: string, + groupId: number, + originValue?: string + ) => void + ) { + this.subscribe(EditorEvent.OnUpdateTab, callback); + } } diff --git a/stories/extensions/test/testPane.tsx b/stories/extensions/test/testPane.tsx index c70f46dca..3af43bceb 100644 --- a/stories/extensions/test/testPane.tsx +++ b/stories/extensions/test/testPane.tsx @@ -1,4 +1,5 @@ import * as React from 'react'; +import * as monaco from 'monaco-editor'; import { activityBarService, colorThemeService, @@ -92,6 +93,39 @@ export type GenericClassDecorator = (target: T) => void;`, editorService.open(tabData); }; + editorService.onUpdateTab( + (newValue: string, groupId: number, originValue?: string) => { + const { current } = editorService.getState(); + const tab = current?.tab!; + const notSave = newValue !== originValue; + editorService.updateTab( + { + id: tab.id, + data: { + ...tab.data, + modified: notSave, + }, + }, + groupId + ); + current?.editorInstance.addCommand( + monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S, + () => { + // ctrl + s + editorService.updateTab( + { + id: tab.id, + data: { + ...tab.data, + modified: false, + }, + }, + groupId + ); + } + ); + } + ); let notify; const addANotification = function () { notify = notificationService.addNotification({