diff --git a/packages/editor/src/browser/workbench-editor.service.ts b/packages/editor/src/browser/workbench-editor.service.ts index 7548ca03ae..3c363b7a89 100644 --- a/packages/editor/src/browser/workbench-editor.service.ts +++ b/packages/editor/src/browser/workbench-editor.service.ts @@ -748,6 +748,8 @@ export class EditorGroup extends WithEventBus implements IGridEditorGroup { private _editorLangIDContextKey: IContextKey; + private _activeEditorIsDirtyContextKey: IContextKey; + private _isInDiffEditorContextKey: IContextKey; private _diffResourceContextKey: ResourceContextKey; @@ -905,6 +907,7 @@ export class EditorGroup extends WithEventBus implements IGridEditorGroup { ); this._editorLangIDContextKey = this.contextKeyService.createKey('editorLangId', ''); this._isInDiffEditorContextKey = this.contextKeyService.createKey('isInDiffEditor', false); + this._activeEditorIsDirtyContextKey = this.contextKeyService.createKey('activeEditorIsDirty', false); this._isInDiffRightEditorContextKey = this.contextKeyService.createKey('isInDiffRightEditor', false); this._isInEditorComponentContextKey = this.contextKeyService.createKey('inEditorComponent', false); } @@ -926,6 +929,7 @@ export class EditorGroup extends WithEventBus implements IGridEditorGroup { } this._editorLangIDContextKey.reset(); } + this._activeEditorIsDirtyContextKey.set(this.activeEditorIsDirty()); this._isInDiffEditorContextKey.set(this.isDiffEditorMode()); // 没有 focus 的时候默认添加在 RightDiffEditor this._isInDiffRightEditorContextKey.set(this.isDiffEditorMode()); @@ -1176,6 +1180,12 @@ export class EditorGroup extends WithEventBus implements IGridEditorGroup { editorId: this.codeEditor.getId(), }), ); + + this.toDispose.push( + this.codeEditor.monacoEditor.onDidChangeModelContent(() => { + this._activeEditorIsDirtyContextKey.set(this.activeEditorIsDirty()); + }), + ); this.codeEditorReady.ready(); } @@ -2239,6 +2249,10 @@ export class EditorGroup extends WithEventBus implements IGridEditorGroup { return !!this.currentOpenType && this.currentOpenType.type === EditorOpenType.diff; } + activeEditorIsDirty() { + return this.hasDirty() && this.workbenchEditorService.currentEditorGroup === this; + } + isComponentMode() { return !!this.currentOpenType && this.currentOpenType.type === EditorOpenType.component; }