Skip to content

Commit

Permalink
feat: extract editor modified logic to extensions (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
mumiao authored Apr 25, 2021
1 parent 4afce3e commit 70aafcf
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/controller/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/model/workbench/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum EditorEvent {
OnMoveTab = 'editor.moveTab',
OpenTab = 'editor.openTab',
OnSelectTab = 'editor.selectTab',
OnUpdateTab = 'editor.updateTab',
OnSplitEditorRight = 'editor.splitEditorRight',
}
interface BuiltInEditorTabDataType {
Expand Down
18 changes: 18 additions & 0 deletions src/services/workbench/editorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as monaco from 'monaco-editor';

import { Component } from 'mo/react';
import {
EditorEvent,
EditorModel,
EditorGroupModel,
IEditor,
Expand All @@ -30,6 +31,13 @@ export interface IEditorService extends Component<IEditor> {
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
Expand Down Expand Up @@ -280,4 +288,14 @@ export class EditorService
});
return cloneGroup;
}

public onUpdateTab(
callback: (
newValue: string,
groupId: number,
originValue?: string
) => void
) {
this.subscribe(EditorEvent.OnUpdateTab, callback);
}
}
34 changes: 34 additions & 0 deletions stories/extensions/test/testPane.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import * as monaco from 'monaco-editor';
import {
activityBarService,
colorThemeService,
Expand Down Expand Up @@ -92,6 +93,39 @@ export type GenericClassDecorator<T> = (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<string>({
Expand Down

0 comments on commit 70aafcf

Please sign in to comment.